Snapshot store plugin

The snapshot plugin enables storing and loading snapshots for event sourced persistent actors.

Schema

The snapshot table need to be created in the configured database, see schema definition in Creating the schema.

Configuration

To enable the snapshot plugin to be used by default, add the following line to your Akka application.conf:

akka.persistence.snapshot-store.plugin = "akka.persistence.r2dbc.snapshot"

It can also be enabled with the snapshotPluginId for a specific EventSourcedBehavior and multiple plugin configurations are supported.

Note

Snapshots are optional, and if you know that the application doesn’t store many events for each entity it is more efficient to not enable the snapshot plugin, because then it will not try to read snapshots when recovering the entities.

See also Connection configuration.

Reference configuration

The following can be overridden in your application.conf for the snapshot specific settings:

sourceakka.persistence.r2dbc {
  snapshot {
    class = "akka.persistence.r2dbc.snapshot.R2dbcSnapshotStore"
    table = "snapshot"

    # the column type to use for snapshot payloads (bytea or jsonb)
    payload-column-type = "BYTEA"

    # Otherwise it would be a pinned dispatcher, see https://github.com/akka/akka/issues/31058
    plugin-dispatcher = "akka.actor.default-dispatcher"

    # Enables an optimization in Akka for avoiding snapshot deletes in retention.
    only-one-snapshot = true
  }
}

Usage

The snapshot plugin is used whenever a snapshot write is triggered through the Akka Persistence APIs.

Snapshot serialization

The state is serialized with Akka Serialization and the binary snapshot representation is stored in the snapshot column together with information about what serializer that was used in the ser_id and ser_manifest columns.

For PostgreSQL the payload is stored as BYTEA type. Alternatively, you can use JSONB column type as described in PostgreSQL JSON.

Retention

The R2DBC snapshot plugin only ever keeps one snapshot per persistence id in the database. If a keepNSnapshots > 1 is specified for an EventSourcedBehavior that setting will be ignored.

The reason for this is that there is no real benefit to keep multiple snapshots around on a relational database with a high consistency.

See also EventSourcedCleanup tool.

Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.