After a weekend outage, a MongoDB change-stream loader into the warehouse cannot resume — diagnose and recover
A Python service reads a MongoDB change stream on shop.orders (a replica set) with pymongo and upserts the documents into a warehouse table by _id. After each batch is written it saves the stream's resume token to a file and opens the next run with watch(resume_after=token). The service was down from Friday 18:00 to Monday 06:00 (60 hours). On restart it fails at once, and every retry fails the same way. Constraints: explain the failure from the output, say what data is affected, and give a recovery that leaves no gap and no duplicates in the warehouse table.
pymongo.errors.OperationFailure: Executor error during getMore :: caused by ::
Resume of change stream was not possible, as the resume point may no longer be in
the oplog., full error: {'ok': 0.0, 'code': 287, 'codeName': 'ChangeStreamHistoryLost'}
rs0:PRIMARY> rs.printReplicationInfo()
configured oplog size: 51200MB
log length start to end: 73412secs (20.39hrs)
oplog first event time: Sun Nov 08 2026 09:41:07
oplog last event time: Mon Nov 09 2026 06:04:39Diagnose the cause.
The token points to Friday 18:00, but the oplog holds only ~20 hours and now starts Sunday 09:41, so the stream cannot resume and gap changes are lost. Open a new stream, keep its token, reload by _id upserts, then replay it; size the oplog past the longest outage.
- Opening the stream with no resume token and assuming it replays from where the loader stopped, when it starts from now
- Taking the snapshot first and opening the stream afterwards, which loses the changes made while the snapshot ran
- Sizing the oplog in gigabytes without checking how many hours it holds at peak write rate
- Why does the loader save the token only after the batch is written, and what delivery guarantee does that give?
- How does Debezium's MongoDB connector behave in the same situation under
snapshot.modeinitialandwhen_needed?
What happened. A resume token points to an event in the oplog. The oplog is a size-capped collection: at 50 GB and the current write rate it holds 20.4 hours, and its first event is Sunday 09:41. The token points to Friday 18:00, that point is gone, and the server answers ChangeStreamHistoryLost (code 287). Retrying will not help: the history does not come back.
What data is affected. Every insert, update and delete on shop.orders from Friday 18:00 to Sunday 09:41 can no longer be read from the stream. Deletes especially: a snapshot shows that a document is gone only if the warehouse compares the sets of _ids.
Recovery with no gap and no duplicates.
# 1. open a new stream BEFORE the snapshot and record its position
with orders.watch(full_document="updateLookup") as stream:
start_token = stream.resume_token
# 2. snapshot: every document -> upsert by _id; rows missing from the snapshot -> delete
# 3. replay the stream from start_token: changes made during the snapshot repeat,
# but an upsert by _id is idempotent, so there are no duplicates
with orders.watch(resume_after=start_token, full_document="updateLookup") as stream:
...So it does not happen again.
- Size the oplog in hours, not gigabytes: the window must cover the longest possible outage with a margin (
replSetResizeOplog, or a minimum retention periodoplogMinRetentionHours). - Alert on the age of the saved token against the oplog window.
- Debezium handles the same case with
snapshot.mode=when_needed: when the position is gone it snapshots again, while underinitial(the default) the connector simply fails to start.