Event Sourcing Snapshots Explained
On this page
Learning Objective
Explain why rehydration cost grows linearly with stream length, implement snapshotting correctly as a derived cache rather than a second source of truth, and choose a snapshot frequency and invalidation strategy that survives changes to the reducer.
Concept Explanation
The cost of the event-sourcing model is paid on every read of the write model. Loading an aggregate means reading its entire stream and folding it, so a command against an aggregate with 40 events is cheap and a command against one with 400,000 events is not. The growth is linear in the number of events, and it is paid on every single command, which means a system that feels fast in its first year can become unusable in its third without any code changing at all.
A snapshot is a cached fold. Periodically, the system serializes the current aggregate state along with the stream version it corresponds to, and stores it separately. Rehydration then reads the most recent snapshot, deserializes it into the starting state, and folds only the events after that version. The cost of a load drops from the length of the stream to the number of events since the last snapshot, which is a constant you control.
The single most important discipline around snapshots is that a snapshot is never a source of truth. It is a derived value, exactly like a projection, and the events remain authoritative. This matters because it determines what happens when things go wrong. If a snapshot is corrupted, stale, or produced by a reducer with a bug, the correct response is to delete it and fold from the beginning, which always produces the right answer because the events never changed. If a team ever starts treating the snapshot as the record and stops being able to recompute it, they have quietly reverted to state-oriented storage with an expensive log attached.
That derived status has a direct consequence for versioning. The snapshot is a serialization of the reducer's output, so the moment the reducer changes, by adding a field to the aggregate, changing how a total is computed, or fixing a bug in how an event is applied, every existing snapshot encodes the old logic. Loading that snapshot and folding only recent events would silently produce state computed partly under the old rules and partly under the new. The standard defense is to store a schema version alongside every snapshot and to treat a version mismatch as a cache miss: ignore the snapshot, fold the full stream, and write a fresh snapshot at the new version. This makes a reducer change safe by default, at the cost of one expensive load per aggregate after a deploy.
Frequency is a tuning decision with a simple shape. Snapshot too rarely and loads stay slow; snapshot too often and you spend write amplification and storage on aggregates nobody reads. A common approach is a threshold, snapshot every N events, with N somewhere between 50 and 500 depending on how expensive the fold is and how hot the aggregate is. A better approach for uneven workloads is to snapshot asynchronously, outside the command path, driven by a background process that watches stream lengths, so no user request ever pays the cost of writing one. The deeper answer, though, is that a stream needing aggressive snapshotting is often a design signal: an aggregate accumulating hundreds of thousands of events usually has a lifecycle that should have been closed out, such as a bank account that should roll into monthly statement aggregates, or a boundary drawn too wide.
| Approach | Load cost | Write cost | Best for |
|---|---|---|---|
| No snapshots | Grows linearly with stream length forever | None | Short-lived aggregates that naturally end, like a single order |
| Snapshot every N events | Bounded by N events of folding | One extra write per N appends | The default for most long-lived aggregates |
| Asynchronous background snapshotting | Bounded, with occasional overshoot | Zero cost inside the command path | Latency-sensitive write paths and uneven traffic |
| Snapshot on every append | One read, no folding at all | Doubles write volume and storage | Rarely worth it; usually a sign to re-examine the aggregate |
| Close the stream and start a new one | Naturally bounded by the period | A periodic rollover job | Accounts, ledgers, and anything with a statement period |
Worked Example: Snapshotting With Version-Aware Invalidation
The load path below treats the snapshot strictly as a cache. A missing snapshot, a corrupt one, or one written by an older reducer all produce the same behavior: fold from the beginning and get the right answer.
CREATE TABLE snapshots (
stream_id TEXT PRIMARY KEY,
version INT NOT NULL, -- the stream version this state reflects
reducer_version INT NOT NULL, -- bumped whenever apply() changes
state JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
const REDUCER_VERSION = 4; // bump this with every reducer change
const SNAPSHOT_EVERY = 200;
async function load(streamId) {
const snap = await db.snapshots.findByPk(streamId);
// A snapshot from an older reducer is not wrong data, it is a stale cache.
const usable = snap && snap.reducer_version === REDUCER_VERSION;
const from = usable ? snap.version : 0;
const start = usable ? snap.state : EMPTY;
const events = await store.readStream(streamId, { afterVersion: from });
const state = events.reduce((s, e) => ({ ...apply(s, e), version: e.version }), start);
// Snapshot writing is fire-and-forget: it must never fail the command.
if (state.version - from >= SNAPSHOT_EVERY) {
queueSnapshot(streamId, state).catch((err) => log.warn('snapshot failed', err));
}
return state;
}
// Deleting every snapshot is always a safe operation. It costs one slow load
// per aggregate and cannot produce an incorrect result, because the events
// are the truth and the snapshot never was.
async function invalidateAll() {
await db.query('TRUNCATE snapshots');
}Real-World Examples
- EventStoreDB and Axon Framework both ship first-class snapshot support with configurable triggers, and both document the same rule this lesson emphasizes: a snapshot is a performance optimization that can be deleted at any time without data loss. Axon in particular exposes a snapshot trigger definition based on event count and treats deserialization failure of a snapshot as a cache miss rather than an error, falling back to replaying the full stream.
- Apache Kafka's log compaction, introduced in version 0.8.1, applies the same idea at the infrastructure level for keyed streams: rather than retaining every message forever, compaction retains at least the most recent value for each key, so a consumer rebuilding state from the beginning of a compacted topic gets the equivalent of a snapshot plus recent changes rather than the entire history. It is the same tradeoff, bounded rebuild time in exchange for losing the ability to see intermediate values.
Review Questions & Answers
The stream version says which events are already folded into the cached state; the reducer version says which logic produced it. If the reducer changes, by adding a field, fixing a bug in how an event is applied, or changing a derived total, then a snapshot written by the old logic represents a state the current code would never have produced. Folding only recent events onto it yields a hybrid computed partly under the old rules and partly under the new, which is far worse than being simply out of date because the error is invisible. Treating a reducer version mismatch as a cache miss makes every reducer change safe without any migration step.
Because it removes an unnecessary failure and latency dependency from the write path. A snapshot has no correctness value, so a command that succeeded should not fail or slow down because a serialization or a cache write failed. Moving the work to a background process means the user-facing append never pays the serialization cost, a snapshot store outage degrades load performance rather than breaking writes, and the frequency can be driven by observed stream lengths rather than by a counter checked on every command.
It suggests the aggregate boundary or its lifecycle is wrong. An aggregate that never ends and absorbs an unbounded number of events is usually modeling a long-running entity that the business actually thinks of in periods, an account with monthly statements, a subscription with billing cycles, an inventory item with reconciliation points. The structural fix is to close the stream at a period boundary and open a new one seeded with a carried-forward opening balance, which bounds the stream naturally. Aggressive snapshotting treats the symptom; closing the stream removes the cause.
Because the snapshot carries no information that does not already exist in the events. Deleting it and folding from version zero reproduces exactly the state it was supposed to hold, so the recovery procedure is a truncate and a slow period, with no data loss and no reconstruction from backups. A corrupted row in a state-oriented store, by contrast, has destroyed the only copy of that information, and recovery means restoring from a backup taken at some earlier moment and losing everything written since, or reconstructing the value by hand from whatever secondary records happen to exist.