Chapter 11
How does a late reader catch up?
Design a snapshot/live handoff with versions, gaps and deletes.
Prerequisite: Replay a cached value. Course map →
The situation
A subscriber connects after prices were published. A latest-value cache fixes the first missing value, but a concurrent update during snapshot transfer can still be applied in the wrong order.
Choose the question before retention
A live notification feed answers what is being published now. A current-state view answers what is true now. An audit history answers how the state changed. They need different retained data and delivery contracts. Do not name all three a stream and assume their recovery needs match.
The last-value investigation stores one price and sends it when the publisher receives the subscription notification. That run uses one topic and no concurrent updates. A service that also needs multiple topics, deletions or concurrent updates must decide how those changes fit the handoff.
Locate a consistent handoff
A snapshot captures the current values at a named version or position in the source history. Give later updates versions or positions that can be compared with that point. A reader can keep arriving updates while it loads the snapshot, then apply only the updates that follow the snapshot within the same ordering.
A subscription notification reaching a publisher does not itself freeze the snapshot. Independent snapshot and live services must agree on how their boundaries relate. Decide how to handle repeated updates, older versions and deletes. If a publisher starts its count again on restart, include an epoch, an ID for that run of the publisher.
What a late reader can recover
Illustrative case.
Compare a cache with a snapshot plus updates. What must agree before the reader combines the snapshot and updates?
| Boundary | Useful evidence | Remaining question |
|---|---|---|
| Live feed | Current publications | Missed history not retained |
| Latest-value cache | Current projection | Version/delete semantics |
| Snapshot + updates | Rebuild candidate | A shared version for snapshot and updates |
| Sequence gap | Missing observation detected | An authority must provide repair |
Detect gaps without inventing history
If a publisher numbers its updates in order, a gap shows which numbers the reader has yet to see. To find the missing values, replay them from stored history, load a newer snapshot or ask the system that owns those values.
A tombstone records that a key was deleted. Keep its version long enough to reject a delayed update from before the deletion. The number of distinct keys and the time you keep these markers affect memory use.
Practice the difficult transitions
Test a reader joining while updates continue, a gap during snapshot transfer, an old update after a delete, a duplicate replay, retention loss and publisher restart. Compare final views and observed change histories separately.
Document what happens when repair is impossible: mark the answer incomplete, rebuild from a declared authority or reject the request. Silently displaying a plausible stale value is not an acceptable substitute for a completeness contract.
Check your understanding
A snapshot contains A at version 8. While it loads, the reader saves updates at versions 7 and 9 and a delete at 10. What should it display after processing the updates?
Show hints
Hint 1
Which updates are newer than the snapshot?
Hint 2
What should happen if version 8 returns after the delete?
Show explanation
Ignore version 7, apply version 9, then remove A at version 10. Keep the deletion version so you can reject later messages about A that carry an older version. This works when the snapshot and updates use the same version order.
ReferencesPrimary sources for this chapter’s design case
Use these guides to check the framework or protocol contract discussed in this chapter. Moving “stable” or “current” guides can describe a different release; check the version used by your deployment. These readings do not execute the distributed frameworks they compare.
ZeroMQ Guide: advanced pub/sub