Multiple concurrent invocation of createIndex() operation for the same unique index contract


I have a question related to concurrent invocation of createIndex() method by multiple clients for the same unique index (java mongodb-driver-sync 4.5.0)

Q1: Is it guaranteed that this method always blocks until the index build is complete? It looks like it’s blocking but it’s not mentioned in the java driver documentation (similar story with corresponding mongosh command).
Q2: If method is invoked while index build was already triggered and it’s in progress – is it guaranteed that this method always blocks in this scenario as well?

I performed following test:

  1. Local docker Mongo 5.0.5 Community standalone (so latency between java app and oms is very low)
  2. There is a collection with populated data 4 million documents
  3. I’m planning to create index with unique=true that takes about 10 seconds for this collection.
  4. There are 30 concurrent MongoClients (java mongodb-driver-sync 4.5.0) and each of them invokes createIndex operation for the same unique index.


  1. It was observed that behaviour is indeterministic and varies from run to run but at least couple of createIndex() finished successfully almost immediately whereas majority od clients is blocked for ~10 seconds as expected
  2. If number of clients is lower - like 5 then it’s hard to reproduce this issue.
  3. The same test but with small delay (like 500ms) after first createIndex() causes that remaining 29 clients always blocks for ~10 seconds.
  4. The same test but using MongoDB instance @Atlas blocks all clients – maybe is related to additional network overhead that hides the timing/synchronisation issue?

What do you think - is it a bug in client/server or the answer for Q2 is no - it’s not guaranteed?


1 Like

I’m attaching sequence diagrams that show expected (if answer for Q2 is: yes, it’s guaranteed) and observed behaviour: