1. Overview

In this article, we’ll be looking at how Axon supports aggregate snapshotting.

We consider this article to be an expansion of our main guide on Axon. As such, we’ll utilize both Axon Framework and Axon Server again. We’ll use the former in this article’s implementation, and the latter is the event store and message router.

2. Aggregate Snapshotting

Let’s start by understanding what snapshotting an aggregate means. When we start with Event Sourcing in an application, a natural question is how do I keep sourcing an aggregate performant in my application? Although there are several optimization options, the most straightforward is to introduce snapshotting.

Aggregate snapshotting is the process of storing a snapshot of the aggregate state to improve loading. When snapshotting is incorporated, loading an aggregate before command handling becomes a two-step process:

  1. Retrieve the most recent snapshot, if any, and use it to source the aggregate. The snapshot carries a sequence number, defining up until which point it represents the aggregate’s state.
  2. Retrieve the remainder of events starting from the snapshot’s sequence, and source the rest of the aggregate.

If snapshotting should be enabled, a process that triggers the creation of snapshots is required. The snapshot creation process should ensure the snapshot resembles the entire aggregate state at its creation point. Lastly, the aggregate loading mechanism (read: the repository) should first load a snapshot, and then, any remaining events.

3. Aggregate Snapshotting in Axon

Axon Framework supports snapshotting of aggregates. For a complete overview of this process, check out this section of Axon’s reference guide.

Within the framework, the snapshotting process consists out of two main components:

The Snapshotter is the component that constructs the snapshot for an aggregate instance. By default, the framework will use the entire aggregate’s state as the snapshot.

The SnapshotTriggerDefinition defines the trigger towards the Snapshotter to construct a snapshot. A trigger can be:

  • after a set amount of events, or
  • once loading takes a certain amount, or
  • at set moments in time.

The storage and retrieval of snapshots reside with the event store and the aggregate’s Repository. To that end, the event store contains a distinct section to store the snapshots. In Axon Server, a separate snapshots file reflects this section.

Snapshot loading is done by the repository, consulting the event store for this. As such, loading an aggregate, and incorporating a snapshot, are wholly taken care of by the framework.

4. Configuring Snapshotting

We will be looking at the Order domain introduced in the previous article. Snapshot construction, storage, and loading are already taken care of by the Snapshotter, event store, and repository.

Hence, to introduce snapshotting to the OrderAggregate, we only have to configure the SnapshotTriggerDefinition.

4.1. Defining a Snapshot Trigger

 Since the application uses Spring, we can add a SnapshotTriggerDefinition to the Application Context. To that end, we add a Configuration class:

@Configuration
public class OrderApplicationConfiguration {
    @Bean
    public SnapshotTriggerDefinition orderAggregateSnapshotTriggerDefinition(
      Snapshotter snapshotter,
      @Value("${axon.aggregate.order.snapshot-threshold:250}") int threshold) {
        return new EventCountSnapshotTriggerDefinition(snapshotter, threshold);
    }
}

In this case, we chose the EventCountSnapshotTriggerDefinition. This definition triggers the creation of a snapshot once the event count for an aggregate matches the ‘threshold.’ Note that the threshold is configurable through a property.

The definition also needs the Snapshotter, which Axon adds to the Application Context automatically. Hence, it can be wired as a parameter when constructing the trigger definition.

Another implementation we could’ve used, is the AggregateLoadTimeSnapshotTriggerDefinition. This definition triggers the creation of a snapshot if loading the aggregate exceeds the loadTimeMillisThreshold. Lastly, since it’s a snapshot trigger, it also requires the Snapshotter to construct the snapshot.

4.2. Using the Snapshot Trigger

Now that the SnapshotTriggerDefinition is part of the application, we need to set it for the OrderAggregate. Axon’s Aggregate annotation allows us to specify the bean name of the snapshot trigger. 

Setting the bean name on the annotation will automatically configure the trigger definition for the aggregate:

@Aggregate(snapshotTriggerDefinition = "orderAggregateSnapshotTriggerDefinition")
public class OrderAggregate {
    // state, command handlers and event sourcing handlers omitted
}

By setting the snapshotTriggerDefinition to equal the bean name of the constructed definition, we instruct the framework to configure it for this aggregate.

5. Snapshotting in Action

The configuration sets the trigger definition threshold to ‘250.’ This setting means that the framework constructs a snapshot after 250 events are published. Although this is a reasonable default for most applications, this prolongs our test.

So to perform a test, we will adjust the axon.aggregate.order.snapshot-threshold property to ‘5.’ Now, we can more easily test whether snapshotting works.

To that end, we start Axon Server and the Order application. After issuing sufficient commands to an OrderAggregate to generate five events, we can check if the application stored a snapshot by searching in the Axon Server Dashboard.

To search for snapshots, we need to click the ‘Search button in the left tab, select ‘Snapshots’ in the top left corner, and click the orange ‘Search’ button to the right. The table below should show a single entry like this:

axon server dashboard snapshot search

6. Conclusion

In this article, we looked at what aggregate snapshotting is and how Axon Framework supports this concept.

The only thing required to enable snapshotting is the configuration of a SnapshotTriggerDefinition on the aggregate. The job of creation, storage, and retrieval of snapshots, is all taken care of for us.

You can find the implementation of the Order application and the code snippets over on GitHub. For any additional questions on this topic, also check out Discuss AxonIQ.