Write operation failed to satisfy the write concern
This error occurs when a write operation does not receive acknowledgment from the required number of replica set members within the `wtimeout` period. The write operation itself has likely succeeded on the primary; the error is about the failure to *confirm* replication.
- 1Network issues preventing the primary from communicating with secondaries
- 2A secondary member is down, so it cannot acknowledge the write
- 3A secondary is experiencing high load or disk latency, delaying its ability to apply the write from the oplog
- 4A `wtimeout` value that is too short for the network latency or replication load
A write operation is sent with a write concern of `{w: 3}` to a 3-member replica set, but one secondary is offline.
// Connect to a 3-member replica set, then shut down one secondary.
// The following write will time out waiting for the 3rd member's acknowledgement.
db.data.insertOne(
{ message: "important" },
{ writeConcern: { w: 3, wtimeout: 5000 } }
);expected output
MongoServerError: Write concern timed out
Fix 1
Check Replica Set Health
WHEN This error occurs.
// Run this on the primary to check the status of all members. rs.status()
Why this works
Use `rs.status()` to check the state of all replica set members. Ensure they are all in a healthy state (e.g., `PRIMARY` or `SECONDARY`) and are not lagging far behind the primary.
Fix 2
Adjust Write Concern or `wtimeout`
WHEN The replica set is healthy but under high load or high latency.
// Option 1: Lower the write concern to be more flexible.
db.data.insertOne(
{ message: "important" },
{ writeConcern: { w: "majority" } } // Often a better choice than a fixed number
);
// Option 2: Increase the timeout.
db.data.insertOne(
{ message: "important" },
{ writeConcern: { w: 3, wtimeout: 15000 } }
);Why this works
If network latency is unavoidable, you can increase `wtimeout`. Alternatively, consider if a less strict write concern, like `'majority'`, is acceptable for your application's durability requirements.
✕ Immediately assume the write failed and retry the insert
The write most likely succeeded on the primary. A simple retry will often lead to a duplicate document. The error is about acknowledgment, not necessarily failure. Your application should query for the data to check if the write succeeded before deciding to retry.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev