liveBook · Manning

Write concern

As explained in MongoDB 8.0 in Action, Third Edition

Write concern is a MongoDB configuration that specifies the acknowledgment requirements a write must satisfy before it is reported as successful.

Write concern sets the acknowledgment a MongoDB write must receive before it is reported as successful. In a replica set, requiring more members to acknowledge a write improves durability but can increase response time while MongoDB waits for confirmation. The default is w: "majority". section 9.5.1

Choose the w value to match the cost of losing a write:

  • w: 0 requires no acknowledgment. It favors speed when losing some entries, such as logs, is acceptable. section 9.5.1
  • w: 1 requires acknowledgment only from the primary. It responds quickly, but a write may be lost if the primary fails before replication. section 9.5.1
  • w: "majority" requires acknowledgment from a majority of voting members. It suits writes such as orders that must remain recoverable after a primary failure. section 9.5.1
  • w: "all" requires every replica set member to acknowledge the write. It offers the strongest acknowledgment requirement here, potentially with a substantial delay. section 9.5.1
db.routes.insertOne(
  { src_airport: 'MUC'},
  { writeConcern: { w: "majority", j: true, wtimeout: 5000 } }
)

This insert supplies write concern on the operation: w: "majority", alongside the j and wtimeout options. Write-concern settings use the form { w: <value>, j: <boolean>, wtimeout: <number> }. section 9.5.1

Write concern works alongside read concern and read preference. For an application that reads its own writes from the primary, pair w: "majority" with "majority" read concern. For multidocument transactions, configure appropriate read and write concerns as part of the reliability plan, along with error handling and retries for transient failures. section 9.5 section 8.5

Table 10.7 Atlas custom Write Concerns for multiregion clusters

Write concern

Tags

Description

twoRegions

{ region: 2 }

Write operations must receive acknowledgment from at least two regions in your cluster.

threeRegions

{ region: 3 }

Write operations must receive acknowledgment from at least three regions in your cluster.

twoProviders

{ provider: 2 }

Write operations must be acknowledged by at least two different regions in your cluster, each hosted by a distinct cloud provider.

For Atlas multiregion clusters, custom write concerns can require acknowledgment across two or three regions, or across two regions hosted by distinct cloud providers. section 10.6

db.routes.insertOne(
  {
    airline: { id: 410, name: 'Lufthansa', alias: 'LH', iata: 'DLH' },
    src_airport: 'MUC',
    dst_airport: 'JFK'
  },
  { writeConcern: { w: "threeRegions" } }
)

To require propagation to three regions, place w: "threeRegions" in the insert operation’s writeConcern document. section 10.6

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines acknowledgment, compares four w values, and shows operation syntax and Atlas regional options without elaborating j or wtimeout behavior.
Examples
Uses logging, social-media likes, e-commerce orders, and insertOne operations with majority or threeRegions acknowledgment.
What it assumes
Introduces acknowledgment choices while assuming familiarity with MongoDB writes and basic primary–secondary replica-set roles.
What you can do after
Apply it via MongoDB insertOne writeConcern options to choose member acknowledgment or an Atlas multiregion requirement.
How it connects
Connects write concern to replica sets, read concern, read preference, multidocument transactions, and Atlas multiregion clusters.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.