Skip to content
Navigation

Type at least two characters. Search covers page titles, headings, tags and database names.

↑ ↓ to navigateEnter to openEsc to close0 pages
MongoDBadvanced

Write Concerns

Write concern, read concern and read preference — the three settings that decide MongoDB's durability and consistency per operation.

3 min readAdvancedUpdated Edit this page

MongoDB's consistency is configured per operation through three independent settings. Getting them right is more consequential than most tuning.

Write concern

How many members must acknowledge a write before the driver returns.

SettingMeaningLoss on failover
w: 0No acknowledgement at allAnything
w: 1The primary onlyWrites not yet replicated
w: "majority"A majority of voting membersNone acknowledged
j: trueWritten to the journal on diskAdds crash durability
wtimeoutMilliseconds to wait before returning an error
db.orders.insertOne(doc, { writeConcern: { w: "majority", j: true, wtimeout: 5000 } });

wtimeout deserves care: on timeout the driver returns an error, but the write may still have been applied. Retry logic must therefore be idempotent.

Read Concerns

What the read is allowed to observe.

LevelReturns
localThe node's most recent data, which may later be rolled back
availableLike local, without waiting on shard metadata; can return orphaned documents
majorityOnly data acknowledged by a majority — never rolled back
linearizableReflects all writes acknowledged before the read began (primary only)
snapshotA consistent snapshot across the operation, used in transactions
db.orders.find({ _id: id }).readConcern("majority");

majority is what you want when a read informs a decision that cannot be undone. linearizable is stronger and much slower — it waits to confirm the node is still primary — and applies to single-document reads only.

Read Preferences

Which member serves the read.

PreferenceReads from
primaryPrimary only (default)
primaryPreferredPrimary, or a secondary if none exists
secondarySecondaries only
secondaryPreferredSecondaries, or the primary if none available
nearestLowest network latency, primary or secondary
db.orders.find(query).readPref("secondaryPreferred", [{ region: "eu" }]);

Use maxStalenessSeconds to stop reads going to a badly lagging member:

db.orders.find(query).readPref("secondary", null, { maxStalenessSeconds: 90 });

Choosing per operation

OperationReasonable setting
Payment or order state changew: "majority", j: true, read from primary
User profile update then displayw: "majority", causally consistent session
Product list, catalogue browsew: "majority" on write, secondaryPreferred read
Analytics and reportingsecondary with maxStalenessSeconds
High-volume telemetryw: 1 on write, if losing recent points is acceptable

Set the strict values where correctness depends on them and the relaxed ones elsewhere, rather than choosing one global setting and hoping it suits everything.