Skip to content
LESSON 44 / 50Advanceddistributed

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.

Interactive Engine Simulator

Replica Set Raft Consensus & Heartbeat Election Lab

Simulate 3-node replica set heartbeats, failovers, oplog replication, and write concern guarantees.

Consensus Term
Term 1
Monotonically increasing
Voting Quorum
2 / 3 Majority
Strict floor(N/2) + 1
Heartbeat Interval
2000 ms
10s election timeout
Write Concern
node-1PRIMARY
Host:mongo-node-1:27017
Oplog Sequence:#1045
Voted Term:Term 1
Accepts all driver write and read traffic (ReadPreference: primary).
node-2SECONDARY
Host:mongo-node-2:27017
Oplog Sequence:#1045
Voted Term:Term 1
Tails primary local.oplog.rs in background thread.
node-3SECONDARY
Host:mongo-node-3:27017
Oplog Sequence:#1045
Voted Term:Term 1
Tails primary local.oplog.rs in background thread.
REPL_LOG:Replica set stable on Term 1. Primary node-1 actively serving writes.

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.

Sample BSON Documents:
Collection: db.orders
{
  "_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

Step 1: Write with majority acknowledgment and 5s timeout
db.orders.insertOne(
  { item: "Server", price: 5000 },
  { writeConcern: { w: "majority", wtimeout: 5000 } }
);
Protects against write rollback if primary crashes immediately.
Step 2: Configure cluster-wide default write concern
db.adminCommand({
  setDefaultRWConcern: 1,
  defaultWriteConcern: { w: "majority", wtimeout: 10000 }
});
MongoDB 5.0+ defaults to w: "majority" globally.

5. Critical Engineering Rules

Rule / ConstraintUnderlying Reason & Impact
w: 1Acknowledged 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: trueGuarantees 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" } });
Production Warning: Never issue financial or security mutations with w: 1.

7. Architectural Trade-offs

Advantages
  • •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
Disadvantages & Gotchas
  • •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

Q1: What is a MongoDB "Rollback", and which write concern prevents rollbacks from ever occurring?

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?