Lesson 7 of 9 · 26m
Replica Sets, Elections, and Failover Reality
Understand primaries, secondaries, elections, oplog lag, rollback, and operational drills before trusting replication as a availability strategy.
A replica set is not a magic uptime spell. It is a distributed system that agrees on a primary, copies an oplog, elects a new primary when needed, and occasionally forces you to confront what your application did during those few confusing seconds. Teams that understand this design operate MongoDB calmly. Teams that think replication is invisible learn otherwise at 2:00 a.m.
Know the moving parts
A common three-node replica set has one primary and two secondaries:
app driver
|
primary -------- replication oplog -------- secondary A
| |
+---------------- replication oplog -------- secondary BWrites go to the primary. Secondaries apply the oplog. If the primary becomes unreachable, eligible members elect a new primary. The driver discovers topology changes and retries operations according to its configuration.
Use real replica set configuration
A minimal production-shaped configuration is not one node with replication stapled on later:
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "mongo-1.internal:27017", priority: 2 },
{ _id: 1, host: "mongo-2.internal:27017" },
{ _id: 2, host: "mongo-3.internal:27017" }
]
})Check status with context, not panic:
rs.status()
rs.printReplicationInfo()
rs.printSecondaryReplicationInfo()Look at member state, election times, replication lag, and oplog window. "Secondary is behind" is not a diagnosis. How far behind, for how long, and relative to what write volume?
Elections are short but not invisible
During failover, writes can fail or retry, and applications may briefly see not primary or no primary errors. Modern drivers handle much of this, but only if operations are retryable and timeouts are sane. A health check that merely connects to one node is not proof that the set is writable.
normal write path primary accepts write
majority durability enough members replicate it
failure detection heartbeat timeout
election eligible member becomes primary
driver reconnection topology update and retryYou need drills. Step down a primary in staging and watch application errors, retry behavior, and user-facing behavior.
Understand rollback and majority writes
If a primary accepts writes that were not replicated to a majority and then loses an election, those writes can roll back. This is why w: "majority" matters for important data. Rollback files are not a business continuity plan; they are evidence that durability policy and application expectations did not meet.
Arbiter arithmetic matters
An arbiter votes but holds no data. It can help elections in small deployments, but it does not provide durability or capacity. A two-data-node plus arbiter topology can elect a primary during certain failures, yet it cannot provide majority write acknowledgement if one data-bearing node is down. Know what you bought.
Field notes
- Put replica set members across failure domains, but keep latency low enough for replication health.
- Monitor oplog window, replication lag, elections, and member state transitions, not just CPU.
- Hidden and delayed members have specialized uses; they are not casual read-scaling nodes.
- Backup and replication solve different problems. Replication copies mistakes just as faithfully as data.
Was this lesson helpful?