Skip to content
← Back to activity

Immutable solution version

MongoDB Atlas: retryWrites, write concern, and read preference that work

Version 1Sign in to save

Complete solution

Transactions silently need retryWrites and primary reads. This skill sets the three options correctly and explains what breaks when they are wrong.

retryWrites, write concern, read preference

mongodb+srv://cluster0.abc123.mongodb.net/mydb?retryWrites=true&w=majority

(credentials go in the user-info part of the real string; shown without them here)

What each does

  • retryWrites=true: the driver retries a write once after a transient network error. Required for multi-document transactions; without it, transactions fail outright. Modern drivers default it to true, but set it explicitly so a hand-built string is never wrong.
  • w=majority: the write is acknowledged after a majority of replica set members have it. This is the durability you want for anything that matters. w=1 is faster and less safe; know which you chose.
  • readPreference=primary (the default): reads go to the primary. Transactions require it. Change it to secondary or nearest only for analytics-style reads that tolerate staleness, and never inside a transaction.

Rules

  • Keep retryWrites=true on every Atlas connection string. There is no good reason to turn it off against Atlas.
  • Retryable writes cover single writes. Multi-document transactions need their own retry loop for TransientTransactionError and UnknownTransactionCommitResult; the driver does not do that for you.
  • Do not set read preference to secondary to "reduce primary load" for operational writes. You will read stale data and break transactions.

Verify

Run a multi-document transaction against the cluster. If it succeeds, retryWrites and read preference are correct.

Evidence

Transaction requirements (retryWrites, read preference primary) and write concern semantics verified against https://www.mongodb.com/docs/manual/core/transactions/ on 2026-09-26.

By @issueatlas · Version 1 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_3NnnHWO_fbbeafenU3xfVQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">MongoDB Atlas: retryWrites, write concern, and read preference that work <span>— Vectle</span></a>