Read Concerns: local vs majority vs linearizable vs snapshot
Read Concerns control the consistency and isolation of data read from MongoDB: from fast uncommitted local reads to strictly serializable linearizable queries.
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 Read Concern specifies the consistency and isolation guarantees of read operations across a replica set. The five levels are: "local" (default: returns node's latest data, may be rolled back), "available" (same as local, fastest in sharding), "majority" (returns data committed to a majority, immune to rollbacks), "linearizable" (guarantees real-time serializability), and "snapshot" (point-in-time ACID snapshot).
2. Explain Like I'm 5 (Mental Model)
"local" is listening to rumors in the hallway: usually true, but might turn out false. "majority" is waiting for the official school newspaper to confirm it. "linearizable" is calling the principal on the phone right now to verify before taking another step.
3. Real-World Architecture Example
Financial ledger reading an account balance that must never be rolled back.
{
"_id": "acc_01",
"balance": 50000,
"readConcern": {
"level": "majority"
}
}Reading with level: "majority" guarantees the balance cannot be reverted by a primary failover.
4. MQL Implementation Steps
db.accounts.find({ _id: "acc_01" }).readConcern("majority");db.accounts.find({ _id: "acc_01" }).readConcern("linearizable");5. Critical Engineering Rules
| Rule / Constraint | Underlying Reason & Impact |
|---|---|
| local | Fastest; returns current node data. Vulnerable to reading data that gets rolled back. |
| majority | Reads from in-memory WiredTiger snapshot representing majority-committed state. Immune to rollbacks. |
| linearizable | Executes quorum check; blocks until current primary verifies it has not been deposed by a split-brain. |
| snapshot | Only usable inside multi-document transactions. Reads consistent point-in-time snapshot across all collections. |
6. Deep Dive: How "linearizable" Prevents Phantom Reads from Deposed Primaries
Suppose a primary is partitioned off from the network. A new primary is elected, but the old primary does not know it yet. A client reading from the old primary with readConcern: "local" would read stale data. With readConcern: "linearizable", the primary MUST successfully communicate with a majority of members BEFORE returning the read result. If it is deposed, the read fails, preventing stale reads.
// Linearizable read syntax:
db.locks.find({ resource: "engine_mutex" }).readConcern("linearizable");7. Architectural Trade-offs
- •majority read concern guarantees that data read by the application will never be rolled back
- •linearizable read concern provides true recency guarantees even during active network partitions
- •snapshot read concern delivers ACID snapshot isolation across collections in multi-doc transactions
- •linearizable reads incur quorum network round-trip latency on every read operation
- •majority reads require WiredTiger to maintain in-memory historical snapshot versions
8. Technical Interview Questions
With readConcern: "local", the query returns the latest data from the primary's memory, which may include writes that have not yet been replicated to secondaries (issued with w: 1). If the primary crashes before replicating those writes, the data is rolled back, meaning the client read "dirty" data that ultimately disappeared from the database.
9. Staff-Level Ready Answer
"Read Concerns govern read isolation. "local" returns the node's latest data but risks reading un-replicated writes that could roll back. "majority" reads from WiredTiger's majority-committed snapshot, guaranteeing the data will never be rolled back. "linearizable" verifies primary quorum in real-time. "snapshot" provides ACID point-in-time isolation inside transactions."
10. Quick Knowledge Check
Which Read Concern level is required inside multi-document transactions to guarantee consistent point-in-time reads across collections?