MongoDB Read Concern
What Is Read Concern?
Read concern controls how up to date and how durable the data you read must be. In a replica set, a write may be visible on the primary before it has been copied to a majority of members. If the primary fails, such a write can be rolled back. Higher read concern levels only return data that is safe from rollback, at the cost of some latency.
Why It Matters
| Reason | Benefit |
|---|---|
| Avoid rolled-back data | majority prevents your application from acting on writes that later disappear. |
| Choose your tradeoff | Fast reads with local, safer reads with majority. |
| Consistent transactions | snapshot gives all reads in a transaction one consistent view. |
| Predictable behavior | Knowing the level helps you reason about stale or missing data. |
How To Use It
- Connect to a replica set and note that the default read concern is local.
- Run a query with .readConcern("majority") and compare results in a failover test if possible.
- Use majority for data that must not be rolled back, such as financial records.
- Use linearizable only for single-document reads that need the strongest guarantee, and add a maxTimeMS.
- Use snapshot inside transactions to get a consistent view.
- Combine with write concern majority so your writes are visible to majority reads.
- Set the default for a connection through the connection string if most reads need it.
mongosh Examples
// Default (local)
db.accounts.find({ _id: "A" })
// Only data acknowledged by a majority of members
db.accounts.find({ _id: "A" }).readConcern("majority")
// Strongest guarantee, primary only; set a timeout
db.runCommand({
find: "accounts",
filter: { _id: "A" },
readConcern: { level: "linearizable" },
maxTimeMS: 10000
})
// Write with majority so a majority read can see it
db.accounts.insertOne({ _id: "C", balance: 10 }, { writeConcern: { w: "majority" } })
// In a transaction
// session.startTransaction({ readConcern: { level: "snapshot" }, writeConcern: { w: "majority" } }) Edge Cases & Pitfalls
local can read rolled-back writes
After a failover, data visible with local might disappear if it was not replicated to a majority.
Higher levels add latency
majority and linearizable may wait for replication, so reads are slower than local.
linearizable limits
It applies to reads of a single document on the primary and can block, so always set maxTimeMS.
available is for sharded clusters
It can return orphaned documents; do not use it where correctness matters.
Standalone servers
Stronger levels require a replica set; a standalone does not support majority in the same way.
Cheat Sheet
| Topic | Detail |
|---|---|
| local | Default for reads on the primary; returns the node's most recent data, which could be rolled back |
| available | For secondaries in a sharded cluster; lowest latency, may return orphaned documents |
| majority | Returns data acknowledged by a majority of replica set members |
| linearizable | Reflects all majority-acknowledged writes that completed before the read; primary only |
| snapshot | Used in multi-document transactions for a consistent point-in-time view |
| Per operation | Set it in find options or the session; mongosh uses .readConcern() on cursors |
| Connection string | ?readConcernLevel=majority |
| Paired with write concern | Write w: majority plus read majority gives you read-your-writes guarantees |
Key Takeaways
local is fast and may return data that can later be rolled back. majority returns only data acknowledged by a majority. linearizable is the strictest and is for single-document reads on the primary.
Frequently Asked Questions
What is the default read concern?
local for reads on the primary and on secondaries. Inside a transaction, the read concern is set at the transaction level.
What does majority read concern do?
It returns only data that has been acknowledged by a majority of replica set members, so it cannot be rolled back.
When should I use linearizable?
When a read must reflect every majority-acknowledged write that completed before it, for a single document. It is slow and only on the primary.
How does read concern differ from read preference?
Read preference picks which member serves the read. Read concern defines how consistent and durable the data returned must be.
Do I need write concern majority too?
For read-your-own-write behavior with majority reads, yes. Pair w: majority writes with majority reads.
Is snapshot only for transactions?
It is mainly used in multi-document transactions, where it gives all reads one consistent point-in-time view.
Next: MongoDB Write Concern
Continue the MongoDB learning path.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
8 people found this page helpful
