Defining snapshot strategies in Java (Eventuate Local only)

A snapshot strategy is given the opportunity to create a snapshot when an aggregate is updated by calling AggregateRepository.update(). It is a class that must implement the SnapshotStrategy interface.

When using the Spring framework, snapshot strategies are registered by defining them as Spring Beans.

When using the Micronaut framework, snapshot strategies are registered by defining them as Micronaut Beans.

AccountSnapshotStrategy is an example of a snapshot strategy for an Account class:

public class AccountSnapshotStrategy implements SnapshotStrategy {

  public Class<?> getAggregateClass() {
    return Account.class;

  public Optional<Snapshot> possiblySnapshot(Aggregate aggregate, Optional<Int128> snapshotVersion, List<EventWithId> oldEvents, List<Event> newEvents) {
    Account account = (Account) aggregate;
    return Optional.of(new AccountSnapshot(account.getBalance()));

  public Aggregate recreateAggregate(Class<?> clasz, Snapshot snapshot) {
    AccountSnapshot accountSnapshot = (AccountSnapshot) snapshot;
    Account aggregate = new Account();
    List<Event> events = aggregate.process(new CreateAccountCommand(accountSnapshot.getBalance()));
    Aggregates.applyEventsToMutableAggregate(aggregate, events);
    return aggregate;

AccountSnapshotStrategy creates a snapshot every time it is invoked. Another strategy would be to create a snapshot every N events or every N seconds based on the current time and the event timestamps.

An AccountSnapshot stores the current balance of the account.

public class AccountSnapshot implements Snapshot {
  private BigDecimal balance;

  public AccountSnapshot() {

  public AccountSnapshot(BigDecimal balance) {
    this.balance = balance;

  public BigDecimal getBalance() {
    return balance;

Stay in touch
Copyright © 2021 Eventuate, Inc • All rights reserved.