MongoDB deep-dive · Part 8 of 11. Previous: Sharding architecture. Next: Backup & point-in-time recovery.
Change streams give your application a real-time feed of mutations to a collection (or database, or entire cluster) without requiring direct oplog access. They’re built on top of the oplog and surfaced through a stable, resumable cursor API that abstracts away replica set topology, failovers, and oplog internals.
The core abstraction: watch() returns a cursor. You iterate it. Each
iteration yields a change event describing a mutation. If your process
crashes and restarts, you tell the cursor where you left off using a resume
token, and you pick up without gaps.
Getting the resume token contract right is the most important thing in this article.
Basic usage
from pymongo import MongoClient
client = MongoClient("mongodb://host1,host2,host3/?replicaSet=rs0")
col = client.mydb.orders
# Watch all changes to the orders collection
with col.watch() as stream:
for change in stream:
print(change["operationType"], change["documentKey"]["_id"])
Each change document looks like:
{
"_id": {"_data": "82..."}, // resume token
"operationType": "insert",
"ns": {"db": "mydb", "coll": "orders"},
"documentKey": {"_id": "o-100"},
"fullDocument": {"_id": "o-100", "amount": 99.99, ...}
}
The _id of each change event is the resume token for that position in
the oplog. Store it; use it to resume after a crash.
Resume tokens
The resume token is an opaque value — its internal structure is an
implementation detail that has changed across MongoDB versions. Never parse or
construct one yourself. Treat it as a black box that you persist and hand back
to watch().
import json
from pathlib import Path
TOKEN_FILE = Path("/var/lib/myapp/change_stream_token.json")
def load_token():
if TOKEN_FILE.exists():
return json.loads(TOKEN_FILE.read_text())
return None
def save_token(token):
TOKEN_FILE.write_text(json.dumps(token))
resume_token = load_token()
# resume_after= is a keyword argument — not a positional dict
with col.watch(resume_after=resume_token) as stream:
for change in stream:
# 1. PROCESS the event (business logic)
process_event(change)
# 2. SAVE the token AFTER successful processing
save_token(change["_id"])
startAfter vs resumeAfter
resume_after resumes from a token that was produced by a prior watch() on
this collection. start_after is similar but can resume from an invalidate
event token (the event emitted when a watched collection is dropped or
renamed). Use start_after if your change stream watches a collection that
might be dropped and you want to resume processing from the token before the
drop:
col.watch(start_after=token)
For normal crash recovery, resume_after is correct.
startAtOperationTime
Both resume_after and start_after require a token from a previous change
stream run. What if you don’t have a token — for example, you’re replaying
changes from a point in time corresponding to a backup?
from bson import Timestamp
# Start the change stream from the moment of a backup
backup_time = Timestamp(1732492800, 1) # Unix timestamp + ordinal
with col.watch(start_at_operation_time=backup_time) as stream:
for change in stream:
process_event(change)
save_token(change["_id"])
startAtOperationTime lets you open a change stream starting at a specific
cluster time, even without a prior resume token. The common use case: you’ve
restored a backup to time T and want to replay all changes since T to bring
the restored state up to date without re-processing changes from before the
backup.
Filtering the change stream
watch() accepts an aggregation pipeline that filters or transforms the change
events before they reach your code. Only a subset of stages are allowed
($match, $project, $addFields, $replaceRoot, $replaceWith), but
$match is the most useful:
# Only watch for completed orders
with col.watch([
{"$match": {
"operationType": {"$in": ["insert", "update"]},
"fullDocument.status": "completed",
}}
]) as stream:
for change in stream:
process_completed_order(change)
save_token(change["_id"])
The oplog window
Change streams are backed by the oplog. The resume token points to a position
in the oplog. If your change stream consumer falls behind and the oplog wraps
around past your last token position, the stream cannot resume — the
history it needs is gone. MongoDB raises a ChangeStreamHistoryLost error.
The oplog is a capped collection. Its window is determined by its configured size and your write rate. With a 50 GB oplog and 100 MB/s write throughput, the window is roughly 500 seconds — just over 8 minutes. If your consumer stops for longer than the oplog window, you need to re-snapshot and restart.
import pymongo.errors
with col.watch(resume_after=last_token) as stream:
try:
for change in stream:
process_event(change)
save_token(change["_id"])
except pymongo.errors.PyMongoError as e:
if "ChangeStreamHistoryLost" in str(e):
# The oplog window has passed — must resync from scratch
handle_history_lost()
raise
Mitigation: size the oplog generously on write-heavy clusters, and alert
early when your consumer lag approaches a threshold (e.g., > 50% of the oplog
window). rs.printReplicationInfo() shows the oplog window in hours for each
member.
Watching at higher scopes
Change streams can be opened at three scopes:
# Collection scope (most common)
col.watch()
# Database scope — all collections in the database
db.watch()
# Cluster scope — all databases and collections
client.watch()
Cluster-level change streams are powerful for audit logging and ETL pipelines,
but they produce much higher event volumes. Always filter with a $match stage
when watching at cluster scope.
The resume token is a contract: you hold up your end by processing before saving. At-least-once with idempotent processing is the correct default — at-most-once silently drops events, and in an event-driven system, silence is the worst failure mode.
Recall · check yourself
Before you go
Answer from memory first, then reveal. These cards also live in the review queue — come back in a few days.
-
R01 Save the resume token before or after processing the event?
After. Process-then-save = at-least-once (idempotent processing required, nothing missed). Save-then-process = at-most-once — a crash silently skips an event.
-
R02 What invalidates a saved resume token?
The oplog rolling past it — if the consumer is down longer than the oplog window covers, resumption fails and you need a full re-sync. Size the oplog for your longest outage.
-
R03 In PyMongo, what's wrong with
col.watch({"resume_after": token})?The first positional arg is the aggregation pipeline — the token is silently ignored and the stream starts from now. It must be the keyword:
col.watch(resume_after=token). -
R04 What ordering guarantee do change streams give?
Cluster-wide total order of majority-committed changes (they read from the oplog) — every mutation, in order, resumable via the token contract.