Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW