Read and Write Concerns
What you'll learn: How to configure consistency guarantees in MongoDB by controlling how many replicas must acknowledge your reads and writes.
The Control Dial for Consistency
In a MongoDB replica set, data is copied across multiple servers. But when you write data or read it back, how many of those replicas need to respond before you consider the operation successful? That's what read and write concerns let you control.
Think of it like a group voting system: you can require unanimous agreement (all replicas), majority approval (most replicas), or just one vote (fastest replica). Each choice trades off between consistency, latency, and availability.
Write Concern
Write concern specifies how many replicas must acknowledge a write before MongoDB reports success:
w: 1– Only the primary acknowledges (fastest, but data might not be replicated yet)w: "majority"– Most replicas acknowledge (balanced durability)w: <number>– Specific number of replicas must acknowledge
You can also add j: true to require journaling to disk, or set a timeout with wtimeout.
Read Concern
Read concern controls what data you see when reading:
"local"– Returns data from the queried replica, even if not replicated (fastest, possibly stale)"majority"– Returns only data acknowledged by a majority (safer, potentially slower)"linearizable"– Strongest guarantee, ensures you read the most recent committed data
Balancing the Tradeoff
Higher concerns mean stronger consistency but slower operations. Lower concerns mean faster responses but risk reading stale data or losing unacknowledged writes during failures.
Key Takeaway: Read and write concerns let you tune the consistency-performance tradeoff in MongoDB by controlling how many replicas participate in each operation—stronger concerns provide better guarantees but add latency.