# MongoServerError: Transaction aborted due to shard chunk migration in progress

- **ID:** `mongodb/transaction-abort-on-shard-migration`
- **Domain:** mongodb
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

A multi-document transaction accessed a chunk that was being migrated between shards, causing the balancer to abort the transaction.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| MongoDB 5.0 | active | — | — |
| MongoDB 6.0 | active | — | — |
| MongoDB 7.0 | active | — | — |

## Workarounds

1. **Implement a retry loop with exponential backoff to re-run the transaction after a delay: while (true) { try { session.startTransaction(); ... session.commitTransaction(); break; } catch (e) { if (e.code === 251) { sleep(1000 * Math.pow(2, retries)); retries++; } else throw e; } }** (90% success)
   ```
   Implement a retry loop with exponential backoff to re-run the transaction after a delay: while (true) { try { session.startTransaction(); ... session.commitTransaction(); break; } catch (e) { if (e.code === 251) { sleep(1000 * Math.pow(2, retries)); retries++; } else throw e; } }
   ```
2. **Temporarily disable the balancer during critical transaction windows: sh.stopBalancer(); // run transactions; sh.startBalancer()** (85% success)
   ```
   Temporarily disable the balancer during critical transaction windows: sh.stopBalancer(); // run transactions; sh.startBalancer()
   ```
3. **Avoid accessing chunks that are being migrated by querying the config.changelog collection to check for recent migrations and routing transactions to stable chunks: use config.changelog to identify migration patterns** (75% success)
   ```
   Avoid accessing chunks that are being migrated by querying the config.changelog collection to check for recent migrations and routing transactions to stable chunks: use config.changelog to identify migration patterns
   ```

## Dead Ends

- **** — Stopping the balancer is not a fix for the transaction; it only avoids the symptom but can cause performance issues. (50% fail)
- **** — The abort is triggered by the migration process, not by timeout. (90% fail)
- **** — The migration is asynchronous and may take seconds to minutes to complete. (70% fail)
