How it works
The explanation, with examples you can run.
What the set looks like
rs.initiate({ _id: "rs0", members: [ { _id: 0, host: "mongo1:27017" }, { _id: 1, host: "mongo2:27017" }, { _id: 2, host: "mongo3:27017" }] })rs.status().members.map(m => m.name + " " + m.stateStr)[ 'mongo1:27017 PRIMARY', 'mongo2:27017 SECONDARY', 'mongo3:27017 SECONDARY' ]A set can have up to 50 members, of which at most 7 vote. Use an odd number of voters so that a majority exists on one side of any split.
Elections
Members send each other heartbeats every two seconds. When a secondary has not heard from the primary for electionTimeoutMillis (10,000 ms by default), it raises the term and asks for votes. The first candidate to win a majority becomes primary; members prefer candidates with the most recent oplog and higher priority. MongoDB's documentation puts the median time to elect a new primary at no more than about 12 seconds with default settings.
Two failovers on the test set show the difference between a clean shutdown and a crash:
docker stop (clean shutdown) 14:05:17 stop sent -> 14:05:18 new primary, term 2docker kill (crash) 14:05:35 kill sent -> 14:05:44 new primary, term 3A primary shutting down cleanly steps down and lets a caught-up secondary take over at once. A crashed one has to be noticed first, which costs most of the election timeout. A primary that can see only a minority of voters steps down by itself, so a partitioned old primary stops accepting writes.
What applications see
During those seconds, writes fail with errors such as NotWritablePrimary and reads that require the primary fail too. Drivers since the 4.2 generation enable retryable writes: they retry an eligible write once after a network error or failover, and the server uses a transaction number to avoid applying it twice. Set your timeouts so a 10 to 15 second gap degrades gracefully instead of cascading.
Rollback
If the old primary accepted writes that no other member received, the new primary's history does not include them. When the old member rejoins, it rolls those writes back to match, saving them to rollback files for manual inspection. The client was told they succeeded. Writing with w: "majority" (the next topic) means an acknowledged write is on a majority and survives any election.
Check yourself
3 questions. Get them all right to finish this topic.
0 of 3 right so far. Get all three to finish this topic.