I am new to MongoDB and trying to understand replica sets. Specifically, I am trying to understand how MongoDB would handle the following situation:
- A record is written to a table with a unique index on the primary Mongo member (Mongo 1).
- Mongo #1 crashes before the new record is propagated to the secondary Mongo member (Mongo 2).
- Mongo 2 and the arbiter elect Mongo 2 to be the primary.
- The application attempts to create a duplicate record in the table. Because Mongo 2 is not aware of the record written to Mongo 1, the unique constraint is not enforced.
- Mongo 1 becomes available again and replication resumes.
What happens next? Does Mongo 1 attempt to replicate the record to Mongo 2? Or Mongo 2 to Mongo 1? Or both simultaneously? How is the conflict resolved?
Best Answer
In this case, the following will happen:
Please read more on this at Rollbacks During Replica Set Failover and Resync a Member of a Replica Set in the docs.