Write Concerns: w:1 vs w:majority vs j:true vs wtimeout
Write Concerns govern the level of acknowledgment requested from MongoDB before confirming a write operation: balancing latency against durability and rollback protection.
Replica Set Raft Consensus & Heartbeat Election Lab
Simulate 3-node replica set heartbeats, failovers, oplog replication, and write concern guarantees.
1. Formal Definition & Architecture Intent
A Write Concern describes the level of acknowledgment requested from MongoDB for write operations. It consists of three parameters: w (number of nodes that must acknowledge the write), j (whether writes must be flushed to the on-disk journal), and wtimeout (maximum time to wait before throwing a timeout error).
2. Explain Like I'm 5 (Mental Model)
Ordering coffee: w:1 is the barista nodding they took your order. w:majority is three baristas confirming they have your coffee cup in hand. j:true is printing an inked receipt on paper. wtimeout is: "If it takes longer than 5 seconds, let me know."
3. Real-World Architecture Example
Executing an enterprise payment write with zero rollback guarantee.
{
"_id": "o99",
"total": 2500,
"writeConcern": {
"w": "majority",
"j": true,
"wtimeout": 5000
}
}The write is acknowledged only after being committed to disk journals across a majority of replica set nodes.
4. MQL Implementation Steps
db.orders.insertOne(
{ item: "Server", price: 5000 },
{ writeConcern: { w: "majority", wtimeout: 5000 } }
);db.adminCommand({
setDefaultRWConcern: 1,
defaultWriteConcern: { w: "majority", wtimeout: 10000 }
});5. Critical Engineering Rules
| Rule / Constraint | Underlying Reason & Impact |
|---|---|
| w: 1 | Acknowledged as soon as Primary memory cache is updated. Fastest, but vulnerable to rollback if primary crashes before replicating. |
| w: "majority" | Acknowledged only after replicating to a majority of voting members. Immune to election rollbacks. |
| j: true | Guarantees on-disk journal fsync before acknowledgment. Protects against simultaneous power failure. |
6. Deep Dive: Rollbacks on Failover (The w:1 Trap)
If you write with { w: 1 }, the primary confirms success as soon as it updates its local memory. If the primary suddenly loses power 2 milliseconds later before transmitting the oplog to secondaries, the remaining secondaries elect a new primary. The un-replicated write is permanently lost from the active database and dumped into a .rollback BSON file on the old primary's disk.
// ⚠️ Dangerous write vulnerable to rollback:
db.orders.insertOne({ id: 1 }, { writeConcern: { w: 1 } });
// ✅ Enterprise durable write:
db.orders.insertOne({ id: 1 }, { writeConcern: { w: "majority" } });7. Architectural Trade-offs
- •w: "majority" guarantees zero data loss (zero rollback) upon primary failover
- •j: true guarantees crash-resilience against sudden simultaneous datacenter power cuts
- •wtimeout prevents client application threads from hanging indefinitely during network partitions
- •w: "majority" increases write latency by network RTT between primary and secondaries
- •Omitting wtimeout can cause client connection starvation if secondary nodes become unreachable
8. Technical Interview Questions
A rollback occurs when a primary accepts a write with { w: 1 } and crashes before replicating it to secondaries. When a secondary is elected as the new primary, it lacks that write. When the old primary rejoins, it rolls back the un-replicated write into a rollback BSON file. Writing with { w: "majority" } completely prevents rollbacks because a write is only acknowledged once it exists on a majority of nodes.
9. Staff-Level Ready Answer
"Write Concerns define write durability guarantees. w: 1 acknowledges after the local primary commits (fast, but susceptible to rollback on primary crash). w: "majority" acknowledges after replicating to a majority of voting nodes, guaranteeing zero rollback. j: true enforces an on-disk journal fsync. Always specify wtimeout."
10. Quick Knowledge Check
Which write concern configuration completely prevents write operations from being rolled back if the primary crashes?