Ninety thousand events before every call
A prepaid mobile operator keeps each line's account as an event stream: ToppedUp {amount: 10.00}, CallCharged {seconds: 312, cost: 0.47}, DataUsed {mb: 18}. That design is Event Sourcing, and this article assumes it. Before the charging service authorises a call, it loads the line's events from the store and applies them in order to get the balance, the active bundles and the data left. The code that does this and then decides is the line's aggregate; Aggregate explains what belongs inside one.
For most phones that takes a few milliseconds. Line 30071 is a SIM in a delivery van's tracking unit. It opens a data session every twenty minutes, every session is an event, and after three and a half years its stream holds about 90,000 of them. Replaying them takes this service about 1.8 seconds, against a promised answer within 200 ms.
Keeping the rebuilt state in memory helps only until a deploy, a restart or a rebalance; then the next call on line 30071 pays the full 1.8 seconds again.
Write down where you got to
After applying event 90,000, the service saves the line's state together with the number of the last event it includes: {line: 30071, version: 90000, balance: 4.20, bundles: [...]}. That saved record is a Snapshot. The next rebuild no longer starts at event 1:
- The service loads the snapshot with the highest version for line 30071, here version 90,000.
- It sets the aggregate's state from the snapshot.
- It reads from the event store only the events after version 90,000.
- It applies those events in order, and the aggregate is current.
The aggregate starts from the state saved at version 90,000 and asks the store only for what came after it.
With 400 events since then, the rebuild applies 400 instead of 90,400: its cost depends on how far the line is past its last snapshot, not on its age.
The number inside the snapshot
The version stored with the state is what makes the shortcut correct. The tail has to start at exactly the event after the last one the state includes. Start one too early and a ToppedUp credits the line twice; start one too late and a CallCharged is never paid.
So the version is not looked up separately, as the stream's length when the snapshot job ran: events appended in between make the two numbers describe different moments. It comes from the same replay that produced the state, in the same record. For the same reason a snapshot is taken only from events the store has accepted: state saved right after deciding on a charge, before an append that was then rejected because another request appended first, would hold an event the stream does not have. And the snapshot to load is the highest version, not the most recently written.
A cache, not a second ledger
Everything in the snapshot can be computed from the events: it is a cache, and the events stay the record. Delete every snapshot and the system still gives the right answers, slower, as long as the current code can still replay every event in the store. That is why the snapshot write need not share a transaction with the event append: if it fails, the next rebuild replays a longer tail.
Here a snapshot serves only the aggregate's rebuild; screens that show a balance read a projection, which Event Sourcing describes. Memento shares the idea of saving state to restore later, but a memento lets one object put back an earlier state of its own, as an undo does, without exposing its internals, while a snapshot is a starting point for replaying the events after it.
How often to save one
A common rule is a count: save a new snapshot once the aggregate has applied N events since its last one. With N set to 1,000, the aim is to keep line 30071's tail to about a thousand events, around 20 ms at the rate above plus the time to load the snapshot; the tail runs longer while a new snapshot is still being written, or when a write fails. A phone with a few hundred events a year goes years without reaching N, and that is fine: its short stream is cheap to replay.
Smaller N means shorter tails and more snapshot writes; larger N means fewer writes and longer worst-case rebuilds. Some teams measure instead of counting, saving a snapshot when a rebuild's tail takes longer than a budget such as 50 ms. Either way the choice needs numbers from the real streams. Snapshotting on every event adds a write of the whole state to every append, to shorten tails that were already short.
When the state changes shape
The operator introduces a fair-use rule: after 20 GB in a calendar month, the line's speed drops. The aggregate gains a field, dataUsedThisMonth, computed from DataUsed events. The new code loads line 30071's snapshot from version 90,000, written by the old code before the field existed, and starts the counter at zero. Every megabyte the van used earlier that month is missing, and the line keeps its full speed for longer than the rule allows.
A replay from event 1 would count the month correctly; the snapshot kept only what the old code needed. So snapshots carry a schema version of their own, and code that finds an older one has two choices:
- Discard and rebuild. Ignore the old snapshot and replay the stream from event 1. This is correct whenever the code can replay the events, and it costs one full replay per long stream, best spread out rather than paid by every long line just after a deploy.
- Upcast. Convert the old snapshot to the new shape as it is loaded. This works only when the new state can be computed from the old snapshot alone, such as a renamed field or a balance moved from euros to cents. Not here: the old snapshot has no record of the month's usage.
Events change shape too, a harder problem because they cannot be thrown away; Schema evolution: event formats & compatibility explains which event changes stay compatible.
The rebuild path in the Challenge
The Event Sourcing: Snapshots & Projections Challenge asks for both halves: snapshots saved every N events on the aggregate's rebuild path, and a read model that a projector can rebuild from scratch. On the snapshot half, make the version show. Draw the Snapshot between the store and the aggregate, give the edge from the Snapshot the version it covers and the edge from the Event Store the words events after that version, and put your N on the edge where the aggregate saves the Snapshot. The projector reads the Event Store, never the Snapshot, because a projection is rebuilt from the events. The palette's Memento node has no place in this design.