Skip to content

Production MongoDB

Read Preference, Write Concern, Read Concern

Where a read goes, how many nodes must ack a write, and what 'majority' actually means.

AdvancedAbout 6 minutes

Three knobs people mix up. Write concern — how many replica set members must ack the write before the driver reports success. Read preferencewhich members are allowed to serve the read. Read concern — how committed the data you read must be (local vs majority vs snapshot).

await orders.insertOne(doc, { writeConcern: { w: "majority", wtimeout: 5000 } })

await orders.find({ userId }).toArray() // default: primary, readConcern local
Majority write — survives the old primary dying before it replicated
KnobTypical choice
w: 1Primary has it. Fast. Can be rolled back on failover
w: "majority"Majority of voting members. Default on modern servers. The interview answer for durability
w: 0Unacknowledged. Not for user checkout
readPreference: primaryRead-after-write (Rahul just placed an order)
readPreference: secondaryStale OK — reports, not the confirmation page
readConcern: majorityYou only see majority-committed data; pairs with majority writes

Replication is asynchronous. A secondary read can miss the write you just got w: 1 for. Causal sessions (driver) make 'read your writes' work across concerns if you opt in — do not assume it. Transaction snapshot isolation is read concern snapshot inside the txn. j: true waits for journal; with majority you already care about committed replica data more than this flag in interviews.

Interview question

What do write concern and read preference control?

Think about it first.