eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

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 – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

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 EventCountSnapshotTriggerDefinitionThis 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.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

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.

eBook Jackson – NPI EA – 3 (cat = Jackson)