liveBook · Manning

Snapshot

As explained in Akka in Action, Second Edition

A snapshot is a persisted representation of a persistent actor's state at a point in its event history that accelerates recovery.

A snapshot is a persisted representation of a persistent actor’s state at a point in its event history. Recovery loads the latest snapshot and replays only later journal events, avoiding a potentially lengthy full-history reconstruction. section 9.3.3

Configure snapshotting around operational recovery needs:

  • Trigger a snapshot when a significant event arrives, such as QuarterReached, or after a fixed number of processed events. section 9.3.3
  • Retain only the latest snapshots needed for recovery; keeping two snapshots releases resources while preserving a fallback. section 9.3.3
  • Treat snapshot failure as a logged recovery signal rather than an automatic actor stop. section 9.3.3
  • Skip a known corrupted or incompatible snapshot with SnapshotSelectionCriteria.none, allowing recovery to use another path. section 9.3.3
EventSourcedBehavior[Command, Event, State](...).snapshotWhen {
    case (state, QuarterReached(_), sequenceNumber) => true       #1
    case (state, event, sequenceNumber)             => false      #2
 }
 .withRetention(
     RetentionCriteria.snapshotEvery(numberOfEvents = 100,   keepNSnapshots = 2))                                       #3

Use snapshotWhen to trigger snapshots for selected events, then apply snapshotEvery to establish periodic retention. section 9.3.3

Persistent container recovery

A sharded SPContainer stores cargo events in a journal and takes a snapshot after every 100 events, retaining two snapshots. Its database stores journal events and snapshots in separate tables. If the actor restarts, the latest usable snapshot supplies its prior state, and only subsequent events require replay. A known bad snapshot can be excluded from recovery. section 9.4 section 9.3.3

How Akka in Action, Second Edition covers it

How deep it goes
Explains event-based and periodic triggers, retention, failure signals, and exclusion of unusable snapshots.
Examples
Uses QuarterReached triggers, every-100-event retention, SPContainer cargo messages, and journal and snapshot database tables.
What it assumes
Assumes persistent actors, event journals, and recovery, while establishing why replaying extensive histories becomes slow.
What you can do after
Operate/configure snapshotWhen, snapshotEvery, retention, and snapshot-selection policies for persistent actors.
How it connects
Connects snapshots with persistent actors, event sourcing, event journals, sharding, recovery, retention, and database persistence.

Also explained in

Compare how 11 books explain snapshot →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Akka in Action, Second Edition. The section links point to the parts of the book it draws on.