Snapshots
A snapshot is a cached materialization of an aggregate’s state at a known stream sequence. It is a read optimization only: it shortens the fold needed to reconstruct current state. The event stream stays the single source of truth, and deleting every snapshot must leave recovery fully possible.
Status: designed, not implemented. ION aggregates reconstruct from the full event stream today. This page documents the current behavior and the snapshot design that would be introduced if aggregate streams outgrow the fold budget.
Why ION folds the full stream today
Reconstruction cost is a function of events per aggregate. The reference guidance is that snapshots add no value below roughly 100 events per aggregate, and ION aggregates sit well under that line.
| Aggregate | Typical events per stream | Fold cost | Snapshot needed |
|---|---|---|---|
IonSecurityIncident | ~20 | One point-read of a single logical partition | No |
IonInvestigation | low tens | One point-read of a single logical partition | No |
IonSecurityTicket | low tens | One point-read of a single logical partition | No |
Because the partition key is the stream id, loading a whole aggregate is a single-partition query of about 20 small documents. That is cheaper than the read, validation, and invalidation machinery a snapshot would add. Snapshots become worthwhile only if an aggregate boundary allows long streams (hundreds to thousands of events) — at which point the first question is whether the aggregate boundary is correct, not whether to snapshot.
State reconstruction without snapshots
The command path rebuilds state by folding every event in the stream:
load state(streamId):
events = read all events where partitionKey = streamId order by sequence asc
state = initial()
for event in events:
reader = schemaReader(event.eventType, event.version) # upcast if needed
state = apply(state, reader(event))
return state, lastSequence = events.last.sequence
apply is a pure fold — no clock, no I/O, no randomness — so the same stream always
produces the same state. See
deterministic reconstruction.
Snapshot design (when introduced)
Where snapshots live in Cosmos DB
Snapshots are disposable derived data, so they are kept separate from the immutable event documents:
- A dedicated
snapshotscontainer (or a clearly typedsnapshotdocument that is never returned by stream queries), partitioned by the same stream id so a snapshot read is a single-partition point operation co-located with its stream. - Only the latest snapshot per stream is required; older snapshots are overwritten or TTL-expired. They carry no history obligation.
- Snapshot throughput is isolated from the event-append path so snapshot writes never contend with command commits.
Snapshot document
| Attribute | Example | Purpose |
|---|---|---|
id | IonSecurityIncident-{incidentId}:snapshot | Latest-snapshot pointer for the stream. |
partitionKey / streamId | IonSecurityIncident-{incidentId} | Co-locates the snapshot with its stream. |
aggregateType | IonSecurityIncident | Fold/reader selection. |
sequence | 180 | The last event sequence folded into state. The boundary for catch-up. |
schemaVersion | v3 | Aggregate-state schema; drives invalidation. |
state | { ... } | Serialized aggregate state at sequence. |
createdAt | timestamp | Operational metadata; never used in domain logic. |
Creating a snapshot — off the command path
Snapshots are built asynchronously from Change Feed so the write path stays a plain transactional append. They are never produced inside the command transaction.
- A snapshot worker observes appends via a dedicated Change Feed lease, isolated from operational projectors.
- When the event count since the last snapshot crosses the threshold, it folds the stream to current state.
- It upserts the latest-snapshot document for the stream. The write is idempotent
on
(streamId, sequence)— re-running the worker cannot corrupt the snapshot. - A snapshot for a sequence already covered is a no-op.
Using a snapshot for state generation
The command path prefers the snapshot and folds only the tail of the stream:
load state(streamId):
snap = point-read latest snapshot for streamId
if snap is null or not schemaCompatible(snap.schemaVersion):
return full fold(streamId) # safe fallback — always valid
state = deserialize(snap.state)
tail = read events where partitionKey = streamId and sequence > snap.sequence
for event in tail (ascending):
state = apply(state, schemaReader(event))
return state, lastSequence = max(snap.sequence, tail.last.sequence)
Optimistic concurrency is unchanged: the append still asserts the expected next sequence, so a stale snapshot can never cause a lost update — at worst it triggers a reload, which falls back to the tail fold or full fold.
Invalidation rules
- A snapshot is valid only if its
schemaVersionmatches the current aggregate-state schema. A breaking state-schema change or a new upcaster invalidates existing snapshots; the loader discards them and folds the stream. - A snapshot is never trusted without verifying
streamId,sequence, andschemaVersion. - Snapshots are rebuildable at any time from the stream and may be dropped in bulk.
Snapshot rules
- MUST treat the event stream as authoritative; a snapshot is a cache.
- MUST verify stream identity, sequence, and schema version before applying a snapshot.
- MUST keep full-fold reconstruction working with every snapshot deleted.
- MUST build snapshots asynchronously from Change Feed, never inside the command transaction.
- MUST-NOT store a snapshot in a way that event-stream queries can return it.
- MUST-NOT introduce snapshots before measured fold cost justifies them; revisit the aggregate boundary first.