Resume tokens for reliability in MongoDB - Time & Space Complexity
When using resume tokens in MongoDB change streams, it's important to understand how the time to resume watching changes as the data grows.
We want to know how the cost of resuming a stream scales with the size of the data or events.
Analyze the time complexity of resuming a change stream using a resume token.
const changeStream = collection.watch([], { resumeAfter: resumeToken });
changeStream.on('change', (change) => {
console.log(change);
});
This code resumes watching changes from a specific point using a resume token.
Look at what happens when resuming the stream.
- Primary operation: The database locates the resume token in the oplog (operation log).
- How many times: This is a single lookup operation to find the resume point before streaming new changes.
The time to find the resume token depends on how the oplog is indexed and stored.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Few operations to find token |
| 100 | Still a few operations due to indexing |
| 1000 | Still a few operations, efficient lookup |
Pattern observation: The lookup time stays roughly the same because the oplog is indexed, so finding the resume token does not slow down as data grows.
Time Complexity: O(1)
This means resuming with a token takes about the same time no matter how many changes have happened before.
[X] Wrong: "Resuming a change stream takes longer as more changes happen because it has to scan all previous events."
[OK] Correct: MongoDB uses an index on the oplog, so it can jump directly to the resume token without scanning all earlier events.
Understanding how resume tokens work and their time cost shows you know how databases keep streams reliable and efficient, a useful skill for real-world applications.
What if the oplog was not indexed? How would the time complexity of resuming with a token change?
Practice
Solution
Step 1: Understand what resume tokens track
Resume tokens mark the last seen event in a change stream to continue watching from that point.Step 2: Identify the purpose of resume tokens
They help avoid missing or repeating events when watching database changes.Final Answer:
To continue watching database changes without missing any events -> Option DQuick Check:
Resume tokens = Continue watching changes reliably [OK]
- Thinking resume tokens speed up queries
- Confusing tokens with backups
- Assuming tokens encrypt data
Solution
Step 1: Recall the option name for resuming change streams
The MongoDB change stream API uses the option namedresumeAfterto resume from a token.Step 2: Confirm other options are incorrect
Options likestartAfter,continueFrom, andrestartFromare not valid MongoDB options.Final Answer:
resumeAfter -> Option AQuick Check:
Resume option = resumeAfter [OK]
- Using startAfter instead of resumeAfter
- Confusing option names
- Assuming continueFrom is valid
const changeStream = collection.watch([], { resumeAfter: savedToken });
changeStream.on('change', (change) => {
console.log(change._id);
});Solution
Step 1: Understand the effect of resumeAfter option
TheresumeAfteroption tells MongoDB to start the change stream from the event after the saved token.Step 2: Analyze the output of the event listener
The listener prints the_idof each change event starting after the saved token.Final Answer:
It will print the _id of changes starting after the savedToken event -> Option AQuick Check:
resumeAfter = start after saved token [OK]
- Thinking it prints all changes from start
- Assuming resumeAfter disables events
- Confusing syntax error with option usage
Solution
Step 1: Identify the importance of saving resume tokens
To avoid missing events, you must save the resume token after processing each event.Step 2: Understand the impact of not saving tokens timely
If you resume from an old token, events between the last processed and saved token will be missed.Final Answer:
You did not save the resume token after the last event -> Option CQuick Check:
Save token after each event to avoid missing [OK]
- Assuming resumeAfter usage alone prevents missing events
- Confusing startAfter with resumeAfter
- Blaming database support incorrectly
Solution
Step 1: Understand the requirement for reliability
To never miss or repeat events, the system must remember where it left off in the change stream.Step 2: Identify the best practice for resuming
Saving the resume token after each event and usingresumeAfteron restart ensures continuous, reliable watching.Step 3: Evaluate other options
Restarting from the beginning wastes resources and may repeat events; fixed timestamps can miss events; polling is inefficient and unreliable.Final Answer:
Save the resume token after each change event and use resumeAfter on restart -> Option BQuick Check:
Save token + resumeAfter = reliable watching [OK]
- Restarting from beginning causes repeats
- Using fixed timestamps misses events
- Ignoring tokens leads to unreliable watching
Ready to prove it?
Pass the test and this pattern moves unknown
