MongoDB Read Concern and Write Concern Explained
On this page
Read concern vs write concern
Every write and read on a replica set answers two questions:
- Write concern: how many members must have this write before I'm told it succeeded?
- Read concern: what guarantee must the data I read have: any version, or one that can never be rolled back?
Stronger settings mean safer data and higher latency. MongoDB lets you choose per operation.
Write concern levels
| Write concern | Acknowledged when | Can be rolled back? |
|---|---|---|
w: 0 | Not acknowledged at all | You won't even know it failed |
w: 1 | The primary applied it | Yes, if the primary fails before replicating |
w: "majority" (default since 5.0) | A majority of voting members have it | No |
w: <n> | n members have it | Depends on n |
Options:
j: true: members also wrote it to their on-disk journal before acknowledging. With majority write concern, journaling is the default behavior.wtimeout: stop waiting after N milliseconds and return an error. The write may still complete later; a timeout doesn't undo it.
db.orders.insertOne({ _id: 991, total: NumberDecimal("48.00") }, { writeConcern: { w: "majority", wtimeout: 5000 } })Read concern levels
| Read concern | Returns |
|---|---|
local (default for most reads) | The member's most recent data, which might later be rolled back |
available | Like local; on sharded clusters may return orphaned documents during migrations |
majority | Only data acknowledged by a majority: never rolled back |
linearizable | Majority data that reflects all writes acknowledged before the read began; primary only, slow |
snapshot | A consistent point-in-time view; used in transactions (Day 18) |
Reading your own writes with causal consistency
A user updates their profile (majority write) and the next request reads from a secondary that hasn't caught up: they see the old profile. Causally consistent sessions fix this: within a session, MongoDB guarantees each read reflects the session's earlier writes, even on secondaries, by waiting for the member to catch up:
with client.start_session(causal_consistency=True) as s:
profiles.update_one({"_id": 42}, {"$set": {"name": "Sam"}}, session=s)
profiles.with_options(read_preference=ReadPreference.SECONDARY_PREFERRED,
read_concern=ReadConcern("majority")).find_one({"_id": 42}, session=s)Causal guarantees require majority read and write concerns.
Choosing read and write concerns per data type
| Data | Write | Read |
|---|---|---|
| Orders, payments, accounts | majority | majority for anything that triggers actions |
| User profile edits | majority | Causal session, or primary |
| Click and log events | w: 1 acceptable for throughput | local |
| Analytics on secondaries | (n/a) | local or majority, stale reads acceptable |
Keep the default majority write concern unless you've measured a need and accepted the loss risk. Deliberately lowering it for bulk, low-value data is fine; lowering it globally is how teams lose orders during a failover.
Check yourself
A majority-acknowledged write exists on most members and survives any failover; a w: 1 write can be rolled back if the primary fails before replicating it.
No. The server stopped waiting for replication, but the write was applied on the primary and may still replicate and become durable. Treat the outcome as unknown, and check or retry idempotently instead of assuming it failed.
Use a causally consistent session with majority read and write concerns, so a read waits until the secondary has caught up with that session's writes, or read that user's data from the primary right after they change it.
The read returns only data acknowledged by a majority of members, which can't be rolled back by a failover. It may be slightly older than the primary's latest writes.
Often w: 1, since losing a few events in a rare failover is acceptable and latency matters. Orders and payments should use w: "majority".