One Primary, Many Secondaries
A replica set is a group of MongoDB nodes holding the same data. One node is the primary and accepts all writes. The others are secondaries that replicate the primary oplog and can serve reads.
When the Primary Fails
If the primary becomes unreachable, the remaining nodes hold an election to pick a new primary. Elections use a consensus protocol so only one node wins.
- A candidate needs votes from a majority of voting members to become primary.
- This is why a set should have an odd number of voting members, avoiding ties.
- A node with stale data will not win against a more up to date candidate.
Why Majority Matters
Requiring a majority prevents split brain, where two nodes each think they are primary. If a node cannot reach a majority, it steps down to secondary and refuses writes.
An arbiter can vote without holding data to break ties cheaply, though it adds no redundancy.
Key idea
A replica set elects a new primary by majority vote when the old one fails, and requiring a majority prevents split brain so only one primary accepts writes.