MongoServerError:更改流错误:来自WiredTiger的恢复令牌已过期:令牌时间戳1234567890早于最旧时间戳1234567895
MongoServerError: ChangeStream error: resume token from WiredTiger is stale: token timestamp 1234567890 is older than oldest timestamp 1234567895
ID: mongodb/change-stream-resume-token-wiredtiger-stale
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| mongodb 6.0 | active | — | — | — |
| mongodb 7.0 | active | — | — | — |
| mongodb 8.0 | active | — | — | — |
根因分析
由于检查点或复制压力导致存储引擎的最旧时间戳超过了恢复令牌时间戳,该令牌已从WiredTiger的历史记录中被修剪。
English
The resume token timestamp has been pruned from WiredTiger's history because the storage engine's oldest timestamp advanced past it due to checkpoint or replication pressure.
官方文档
https://www.mongodb.com/docs/manual/changeStreams/#resume-tokens解决方案
-
Increase the oplog size to retain more history: `db.adminCommand({ replSetResizeOplog: 1, size: 40960 })` (40GB) and use `db.adminCommand({ setParameter: 1, minSnapshotHistoryWindowInSeconds: 3600 })` to keep WiredTiger snapshots for 1 hour. -
Implement fallback logic in the application: on this error, start a new change stream from the current time using `db.collection.watch([], { startAfter: resumeToken })` or `{ startAtOperationTime: Timestamp(...) }` and reprocess recent events. -
Reduce the resume token interval by polling change streams more frequently (e.g., every 5 seconds instead of 30 seconds) to keep tokens within the retention window.
无效尝试
常见但无效的做法:
-
95% 失败
The token is already pruned from the oplog and WiredTiger; retrying with the same token will result in the same error.
-
80% 失败
Checkpoint frequency does not directly control timestamp pruning; the oldest timestamp is managed by replication and read concern settings.
-
90% 失败
This is not configurable and would break point-in-time read operations; the error is a symptom of a broader retention issue.