Event sourcing persists domain objects as a sequence of (state changing) events. To load a domain object from an event store, an application must load and replay those events. Long-lived domain objects can potentially have a huge number of events, which would make loading them very inefficient.
The solution is to periodically persist a snapshot of the domain object’s state. The application only has to load the most recent snapshot and the events that have occurred since that snapshot was created.
The Eventuate API now supports a snapshot mechanism. To create snapshots for a domain object, you simply define a SnapshotStrategy in the Spring application context. A SnapshotStrategy defines two methods:
- possiblySnapshot() – invokes when an AggregateRepository updates an aggregate. It can decide to create a snapshot based on, for example, the number of events
- recreateAggregate() – recreates an aggregate from a saved snapshot
Currently, only Eventuate Local supports snapshots. Eventuate SaaS will support them soon. For more information, see Defining snapshot strategies in Java