# 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:
```js
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.