Using Snapshots

Explore the implementation of event sourcing and snapshots, and also discuss the downsides of snapshots.

Using snapshots

There is not going to be a special interface for snapshots; a PostgreSQL SnapshotStore that satisfies the AggregateStore interface that is used. To make easy work of both applying and taking snapshots, we turn to AggregateStoreMiddleware again.

The snapshots table DDL

Another simple CREATE TABLE statement that could work with other relational databases is as follows:

CREATE TABLE baskets.snapshots (
stream_id text NOT NULL,
stream_name text NOT NULL,
stream_version int NOT NULL,
snapshot_name text NOT NULL,
snapshot_data bytea NOT NULL,
updated_at timestamptz NOT NULL DEFAULT NOW (),
PRIMARY KEY (stream_id, stream_name)
);
The snapshots table

The primary key of the snapshots table is not like the one in events. New versions of a snapshot will overwrite older ones using an UPDATE statement. It does not have to work this way, and the primary key could be changed to keep a history of snapshots if desired.

Plugging into the aggregate store middleware

Here, SnapshotStore could be coded to stand alone, but the implementation that is being used in this ...