Lesson 8 of 9 · 26m
Replication, Write Concern, and Failover Reality
Replica sets are a correctness system, not just uptime plumbing — write concern, read concern, elections, rollbacks, and what `majority` actually buys.
A replica set exists to survive machine failure, but the part engineers underestimate is consistency. The default write concern has changed over time, drivers have defaults, and read preferences hide subtle staleness. If you cannot explain which writes can be lost during failover, you do not yet understand your production database.
The minimum mental model
A replica set has one primary and multiple secondaries replicating the oplog. Clients write to the primary; secondaries apply operations asynchronously. On primary failure, members elect a new primary. During that window writes fail or retry. That is normal. Systems that require zero write interruption are lying or very expensive.
A common connection string looks like:
mongodb://app:secret@mongo1.prod.internal:27017,mongo2.prod.internal:27017,mongo3.prod.internal:27017/appdb?replicaSet=rs0&retryWrites=true&w=majorityWrite concern decides what "committed" means
db.orders.insertOne(order, { writeConcern: { w: "majority", wtimeoutMS: 5000 } })w: 1means the primary acknowledged it. A failover can lose it if it had not replicated yet.w: "majority"means a majority of voting members have it. A properly elected new primary cannot forget it.wtimeoutMSprevents the client from waiting forever when replication is impaired.
For money, inventory, identity, and audit data, majority writes are the baseline. For ephemeral metrics or presence heartbeats, weaker durability may be a rational trade.
Read concern and read preference are separate knobs
Read concern controls what kind of data a query can see:
local: fast, may include data not yet majority committed.majority: only majority-committed data, requires storage support and correct configuration.linearizable: strongest single-document read semantics, expensive.
Read preference controls where the query goes:
db.products.find({ active: true })
.readPref("secondaryPreferred")
.readConcern("majority")Reading from secondaries can reduce primary load, but secondary lag means users may not see the write they just performed. Read-your-own-writes requires care: stickiness, majority reads after majority writes, or routing the post-write read to the primary.
Failover is a behavior test, not a diagram
Drivers retry many errors when retryWrites=true, but retries do not absolve application design. Insert is not naturally idempotent unless you control a unique key. A retried insert can duplicate logical operations if there is no uniqueness boundary. Use idempotency keys, unique constraints, or transaction-aware design.
Rollbacks are rarer but real: if a primary accepts writes that never replicate and then loses election, those writes can be moved aside as rollback data. That is exactly the hole w: "majority" closes.
Field notes
- An arbiter votes but stores no data. PSA (primary, secondary, arbiter) saves money but creates dangerous durability and availability tradeoffs; prefer three data-bearing nodes when the data matters.
- Monitor replication lag, election counts, and majority commit point, not just CPU.
- Hidden and delayed members are operational tools for backups or analytics, not magic read scaling.
- Test failover before launch. Kill the primary in staging and watch real application behavior.
wtimeoutMSexpiring means the client stopped waiting; it does not prove the write was rolled back.- Use causally consistent sessions when read-your-own-writes must survive routing across primaries and secondaries.
Was this lesson helpful?