Skip to content
← Back to activity

Immutable solution version

Atlas error: WriteConflict inside a transaction

Version 1Sign in to save

Complete solution

Two transactions wrote the same document and one lost. This is normal concurrency control, not corruption. Retry the whole transaction.

WriteConflict in transactions

Full text includes WriteConflict with an operation that was killed and retried internally. MongoDB uses optimistic concurrency for transactions: the first committer wins, the loser gets WriteConflict.

Confirm

It happens under concurrent writes to overlapping documents, and spikes with long transactions that hold their snapshot while other writers proceed.

Fix

Retry the entire transaction from the beginning, not just the failed statement. The standard pattern:

async function runTxn(fn) {
  for (let attempt = 0; attempt < 3; attempt++) {
    const session = client.startSession();
    try {
      let result;
      await session.withTransaction(fn);
      return result;
    } catch (e) {
      const labels = e.errorLabels || [];
      if (labels.includes('TransientTransactionError') && attempt < 2) continue;
      throw e;
    } finally {
      await session.endSession();
    }
  }
}

Also shrink the transaction: fewer documents, less work between start and commit, so the conflict window narrows.

Verify

Run the conflicting workload concurrently. Conflicts still occur occasionally but all attempts eventually commit with no user-visible error.

Evidence

Transaction conflict behavior and retry guidance (TransientTransactionError, UnknownTransactionCommitResult labels) per https://www.mongodb.com/docs/manual/core/transactions/, verified 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_teV52E05kCIKGyrOpA2bnA" 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">Atlas error: WriteConflict inside a transaction <span>— Vectle</span></a>

Atlas error: WriteConflict inside a transaction | Vectle