How do write concern and read concern work in MongoDB?
Learn how MongoDB write concern and read concern control durability and consistency, what w:majority and j:true guarantee, and how to tune latency vs safety.
Expected Interview Answer
Write concern controls how many replica-set members must acknowledge a write before MongoDB confirms it, while read concern controls the consistency and durability guarantee of the data a read returns.
Write concern is set with { w, j, wtimeout }: w defines the acknowledgment level (a number, or "majority"), j forces the write to be committed to the on-disk journal, and wtimeout bounds how long the primary waits. Read concern ("local", "available", "majority", "linearizable", "snapshot") decides whether a read can see data that might later be rolled back or only data acknowledged by a majority. Together they trade latency for durability and let you tune the consistency of each operation.
- Tune the durability-versus-latency tradeoff per operation
- Guarantee writes survive a primary failover with w: majority
- Avoid reading data that could be rolled back
- Enable causal and snapshot consistency in transactions
- Bound waiting with wtimeout to prevent stuck writes
AI Mentor Explanation
Write concern is like insisting the third umpire and both on-field umpires all signal before a wicket is entered in the official record; w:majority means most officials must agree. Read concern is the reporter deciding whether to quote a score that is still under review or only the score already ratified by the panel, so the printed scorecard never contradicts the final ruling.
Step-by-Step Explanation
Step 1
Set the write concern
Pass writeConcern: { w, j, wtimeout } on the operation or configure it at the connection level; w can be a count or "majority".
Step 2
Understand journaling
j: true forces the write to be flushed to the on-disk journal before acknowledgment, guarding against a crash before the next checkpoint.
Step 3
Choose a read concern
Select "local", "available", "majority", "linearizable", or "snapshot" based on how tolerant the read is of rollbacks.
Step 4
Match the pair for a use case
For strong guarantees combine w: "majority" writes with readConcern: "majority" reads so acknowledged data is never rolled back.
Step 5
Bound the wait
Use wtimeout so a write does not block indefinitely if not enough members are reachable; a timeout does not undo the write.
What Interviewer Expects
- Distinguishing acknowledgment (write concern) from read guarantee (read concern)
- Knowing what w: majority and j: true actually guarantee
- Awareness of the latency-versus-durability tradeoff
- Understanding rollbacks on a replica set
- Familiarity with read concern levels and when to use each
Common Mistakes
- Thinking write concern controls what reads return
- Assuming w: 1 survives a primary failover
- Believing wtimeout rolls back the write on timeout
- Confusing read concern with read preference (which member to read from)
- Ignoring the extra latency of majority guarantees
Best Answer (HR Friendly)
“Write concern decides how many database servers must confirm a save before MongoDB calls it done, and read concern decides how trustworthy the data you read back is. Together they let you choose between faster responses and stronger guarantees that data will not be lost or contradicted.”
Code Example
// Insert acknowledged by a majority and flushed to the journal
await db.collection('orders').insertOne(
{ orderId: 'A-1001', total: 250 },
{ writeConcern: { w: 'majority', j: true, wtimeout: 5000 } }
);
// Read only data acknowledged by a majority (never rolled back)
const order = await db.collection('orders').findOne(
{ orderId: 'A-1001' },
{ readConcern: { level: 'majority' } }
);
console.log(order);Follow-up Questions
- What is a rollback on a replica set and how does w: majority prevent reading rolled-back data?
- What is the difference between read concern and read preference?
- When would you use readConcern: "linearizable" and what is its cost?
- What happens to a write when wtimeout is exceeded?
- How do write concern and read concern interact inside a multi-document transaction?
MCQ Practice
1. What does a write concern of w: "majority" guarantee?
w: "majority" waits until a majority of voting members acknowledge the write, so it survives a failover and will not be rolled back.
2. Which read concern guarantees the returned data will not be rolled back?
readConcern: "majority" returns only data acknowledged by a majority of members, which cannot be rolled back.
3. What does j: true add to a write?
j: true requires the write to be flushed to the journal before acknowledgment, protecting against a crash before the next checkpoint.
Flash Cards
What does write concern control? — How many replica-set members must acknowledge a write (and whether it is journaled) before MongoDB confirms it.
What does read concern control? — The consistency and durability guarantee of the data a read returns, such as whether it can be rolled back.
w: majority vs w: 1 — majority survives a failover and cannot be rolled back; w: 1 is only acknowledged by the primary and may be lost on failover.
Read concern vs read preference — Read concern is the durability guarantee of the data; read preference is which member (primary/secondary) the read is routed to.
Does wtimeout roll back a write? — No. A timeout only stops waiting for acknowledgment; the write may still have been applied.