Write Concerns
Write concern, read concern and read preference — the three settings that decide MongoDB's durability and consistency per operation.
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.
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.
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.
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
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.