What happens in case of a conflict during replication?

my current setup is, I have a single replica set with a single master and two more replicas. Currently, it has a large collection and I need to add an unique index. I estimate this would take couple of hours and I will be following the instructions given here - https://docs.mongodb.com/v4.0/tutorial/build-indexes-on-replica-sets/

now my question is, lets say I add the unique index in follower 2 and add it back to the replica set. In the meantime, what if primary has added a document which conflicts with this index.

  1. can this replica added back to the replica set?
  2. will it sync with primary successfully?
  3. what happens when it encounters the document which violates the unique constraint?

Hi @V_N_A,

Please note that unique indexes have a specific section in that guide:

To create unique indexes using the following procedure, you must stop all writes to the collection during this procedure.

If you cannot stop all writes to the collection during this procedure, do not use the procedure on this page. Instead, build your unique index on the collection by issuing db.collection.createIndex() on the primary for a replica set.

Thanks,
Pavel

2 Likes

oh! I got confused by this line. I thought I’d have to stop writes to the collection in the replica. Does this mean, I would have stop the writes in primary too?

Yes. Stop all writes throughout the replica… Meaning on Primary which is the one who doing writes.