# MongoServerError: Transaction 1234 was aborted due to a write conflict

- **ID:** `mongodb/transaction-abort-due-to-write-conflict`
- **Domain:** mongodb
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

Two concurrent transactions attempted to modify the same document, causing one to abort due to optimistic concurrency control.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| mongodb-5.0 | active | — | — |
| mongodb-6.0 | active | — | — |
| mongodb-7.0 | active | — | — |
| mongodb-8.0 | active | — | — |

## Workarounds

1. **Implement a retry loop in the application: catch the write conflict error and re-run the transaction up to 3 times with exponential backoff.** (85% success)
   ```
   Implement a retry loop in the application: catch the write conflict error and re-run the transaction up to 3 times with exponential backoff.
   ```
2. **Reduce transaction scope by accessing fewer documents or using smaller batch sizes to minimize contention.** (75% success)
   ```
   Reduce transaction scope by accessing fewer documents or using smaller batch sizes to minimize contention.
   ```
3. **Use a deterministic ordering for document updates within transactions to prevent deadlocks and conflicts.** (80% success)
   ```
   Use a deterministic ordering for document updates within transactions to prevent deadlocks and conflicts.
   ```

## Dead Ends

- **** — Disabling transactions entirely removes atomicity guarantees for multi-document operations. (50% fail)
- **** — Increasing wtimeoutMS does not resolve write conflicts; it only affects write concern timeouts. (50% fail)
- **** — Setting retryWrites=true does not apply to transactions; transactions must be retried manually. (50% fail)
