# MongoServerError：事务对于快照来说太旧：读取时间戳早于存储引擎中最旧的时间戳

- **ID:** `mongodb/transaction-snapshot-too-old`
- **领域:** mongodb
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

多文档事务尝试读取已被存储引擎由于缓存压力或写入冲突修剪的快照。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| mongodb-4.0 | active | — | — |
| mongodb-4.2 | active | — | — |
| mongodb-4.4 | active | — | — |
| mongodb-5.0 | active | — | — |
| mongodb-6.0 | active | — | — |
| mongodb-7.0 | active | — | — |

## 解决方案

1. ```
   Reduce transaction duration by committing quickly. Keep transactions as short as possible; avoid long-running transactions with many operations. Example: session.startTransaction(); await collection1.insertOne(a); await collection2.updateOne(b); await session.commitTransaction();
   ```
2. ```
   Increase the WiredTiger cache size to retain older snapshots: storage.wiredTiger.engineConfig.cacheSizeGB: 8. Restart mongod.
   ```
3. ```
   If the transaction conflicts with writes, use retry logic with exponential backoff. Example in Python: for i in range(3): try: with client.start_session() as session: session.start_transaction(); ...; session.commit_transaction(); break except pymongo.errors.OperationFailure as e: if 'TransactionTooOldForSnapshot' in str(e): time.sleep(0.5 * (2**i)); continue
   ```

## 无效尝试

- **** — This does not affect snapshot pruning; the error is about the read timestamp being too old, not the transaction timing out. (80% 失败率)
- **** — Snapshot read concern is already the default for transactions; the issue is the snapshot itself being pruned. (70% 失败率)
- **** — This is not configurable and would cause memory exhaustion. (100% 失败率)
