WriteConflict data runtime_error ai_generated true

WriteConflict: write conflict due to concurrent update on the same document

ID: data/mongodb-write-conflict-retry

Also available as: JSON · Markdown · 中文
78%Fix Rate
84%Confidence
1Evidence
2023-09-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
MongoDB 4.4 active
MongoDB 5.0 active
MongoDB 6.0 active
MongoDB 7.0 active

Root Cause

MongoDB's WiredTiger storage engine detects a write-write conflict when two operations try to modify the same document concurrently, and one of them must retry.

generic

中文

MongoDB 的 WiredTiger 存储引擎检测到写入-写入冲突,当两个操作试图同时修改同一文档时,其中一个必须重试。

Official Documentation

https://www.mongodb.com/docs/manual/core/transactions-production-consideration/

Workarounds

  1. 85% success Implement retry logic in the application with exponential backoff. Example in Node.js: `async function updateWithRetry(collection, filter, update, retries=3) { for (let i = 0; i < retries; i++) { try { return await collection.updateOne(filter, update); } catch (e) { if (e.code !== 112) throw e; await new Promise(r => setTimeout(r, 100 * Math.pow(2, i))); } } throw new Error('Max retries exceeded'); }`
    Implement retry logic in the application with exponential backoff. Example in Node.js: `async function updateWithRetry(collection, filter, update, retries=3) { for (let i = 0; i < retries; i++) { try { return await collection.updateOne(filter, update); } catch (e) { if (e.code !== 112) throw e; await new Promise(r => setTimeout(r, 100 * Math.pow(2, i))); } } throw new Error('Max retries exceeded'); }`
  2. 80% success Use optimistic concurrency control with a version field: add a `__v` field to the document and increment it on each update; the update condition includes the current version.
    Use optimistic concurrency control with a version field: add a `__v` field to the document and increment it on each update; the update condition includes the current version.
  3. 70% success Reduce the likelihood of conflicts by sharding the collection on a field that distributes writes evenly across documents.
    Reduce the likelihood of conflicts by sharding the collection on a field that distributes writes evenly across documents.

中文步骤

  1. Implement retry logic in the application with exponential backoff. Example in Node.js: `async function updateWithRetry(collection, filter, update, retries=3) { for (let i = 0; i < retries; i++) { try { return await collection.updateOne(filter, update); } catch (e) { if (e.code !== 112) throw e; await new Promise(r => setTimeout(r, 100 * Math.pow(2, i))); } } throw new Error('Max retries exceeded'); }`
  2. Use optimistic concurrency control with a version field: add a `__v` field to the document and increment it on each update; the update condition includes the current version.
  3. Reduce the likelihood of conflicts by sharding the collection on a field that distributes writes evenly across documents.

Dead Ends

Common approaches that don't work:

  1. 75% fail

    Unacknowledged writes can silently lose data and still cause conflicts at the storage engine level.

  2. 50% fail

    The operation is atomic but still subject to WiredTiger's conflict detection; a retry is necessary.

  3. 60% fail

    The conflict is not a timeout issue; it's a concurrent modification that must be retried or avoided.