112 — WriteConflict

writetransactionconcurrency

Two concurrent operations attempted to modify the same document

What is happening?

A WriteConflict occurs when WiredTiger detects that two concurrent operations are trying to modify the same document. For single-document operations outside transactions, MongoDB automatically retries. Inside multi-document transactions, a WriteConflict may cause the transaction to abort, requiring the application to retry the entire transaction.

Common causes

  1. High write contention on the same document
  2. Long-running transactions holding write locks on frequently updated documents
  3. Hot documents receiving concurrent updates from multiple connections
  4. Missing transaction retry logic in the application

Source code references

ErrorCodes::WriteConflict found in 34 locations in the MongoDB server source.

Diagnostic Steps

Related Issues

SERVER-139295Closed
Retry WriteConflict when acquiring/promoting/releasing the critical section
*Issue* During a DSC step down, DDLs can bubble up a WriteConflict error to the user (instead of the critical section code internally retrying it).   *Analysis* [Acquiring, promoting or releasing the critical section writes via DBDirectClient with locks held|https://github.com/mongodb/mongo/blob/b2b94807a4d84159532979b97387cc806f691e0e/src/mongo/db/global_catalog/ddl/sharding_recovery_service.cpp#L282-L364]. In a DSC cluster, during stepdown we may hit a WriteConflict there due to
SERVER-138765Backlog
Integrate ConflictDetector into the staged-write commit path
h2. Overview Wire the conflict detector into commit for staged writes, following design note section 7, so that write-write conflicts are decided by the detector rather than WiredTiger. h2. Scope of Work * Commit ordering: take the {{WriteSet}} accumulated by the write helpers; call {{ConflictDetector::tryCommit(writeSet, readTs, reserveSlot)}}; on conflict throw {{WriteConflictException}} (non-transactional writes retry, transactions abort); apply staged writes to WiredTiger at the reserved ti
SERVER-130408Backlog
setUserWriteBlockMode can fail with WriteConflict error
It appears that the {{setUserWriteBlockMode}} command does not internally handle/retry write conflicts, meaning that it can return a {{WriteConflict}} error back to the user.
SERVER-132375Closed
Lower writeConflict probability in list_collections_and_indexes_write_conflicts.js
With the current failure probability, some tests can execute for a very long time, especially debug builds run on slower variants. The test still will server its purpose with a much lower probability, and this will prevent us from needing so many hits of the WriteConflictRetry in order for the test to pass.
SERVER-131540Closed
Add write conflict retry to listIndexes and listCollections
Both listIndexes and listCollections do shard role acquisitions (autoGet in listCollections) which may result in calls to wiredTiger. In suites which enable WiredTiger failpoints that cause reads to throw write conflict exceptions, this can cause these operations to bubble up WriteConlicts to the user. In practice, this should not actually be a production issue because read only transactions do not throw WriteConflictExceptions. However, the general guidance is to act as though any access to