MongoDB & NoSQL (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
How does a MongoDB Replica Set elect a new Primary during failover?
senior LevelSystem DesignDatabases & SQL
In Simple Words: A Replica Set uses a Raft-inspired election protocol:
- Nodes exchange heartbeats every 2 seconds.
- If Secondaries do not receive heartbeats from Primary for 10 seconds, an election is triggered.
- A Secondary nominates itself as candidate.
- Nodes vote for the candidate only if its replication log (oplog) is at least as up-to-date as their own.
- Candidate receiving a strict majority of votes ($\lfloor N/2 \rfloor + 1$) becomes the new Primary in typically < 2 seconds.