Snapshotting Migration
Axon Framework 5.1 restores snapshotting for event-sourced entities, but the API is no longer centered around SnapshotTriggerDefinition.
Instead, snapshot creation is driven by SnapshotPolicy and configured on the EventSourcedEntityModule.
This page covers the migration path from the Axon Framework 4 snapshotting model to the Axon Framework 5.1 model. For the conceptual background on how snapshotting works in Axon Framework 5, see the Snapshotting reference documentation.
|
The most significant changes are:
|
SnapshotTriggerDefinition to SnapshotPolicy
In Axon Framework 4, snapshot creation was typically configured through a SnapshotTriggerDefinition.
In Axon Framework 5.1, snapshot decisions are made by a SnapshotPolicy, which can react to both individual events and the full sourcing result.
-
Axon Framework 4
-
Axon Framework 5.1
SnapshotTriggerDefinition triggerDefinition = new EventCountSnapshotTriggerDefinition(100);
repository.setSnapshotTriggerDefinition(triggerDefinition);
SnapshotPolicy snapshotPolicy =
SnapshotPolicy.afterEvents(100)
.or(SnapshotPolicy.whenSourcingTimeExceeds(java.time.Duration.ofMillis(500)));
EventSourcedEntityModule<String, Account> accountModule =
EventSourcedEntityModule.declarative(String.class, Account.class)
.snapshotPolicy(snapshotPolicy)
.build();
The main migration step is to move the snapshot trigger out of repository-level configuration and into the entity module. That keeps snapshotting aligned with the rest of the entity configuration, including identifier resolution and criteria resolution.
Declarative configuration
When you configure an entity declaratively, snapshotting is enabled by adding a SnapshotPolicy to the EventSourcedEntityModule.
This is the recommended approach for applications that want explicit control over when snapshots are created.
SnapshotPolicy snapshotPolicy = SnapshotPolicy.afterEvents(250);
EventSourcedEntityModule<String, Account> accountModule =
EventSourcedEntityModule.declarative(String.class, Account.class)
.snapshotPolicy(snapshotPolicy)
.build();
EventSourcingConfigurer.create()
.componentRegistry(cr -> cr.registerComponent(
SnapshotStore.class,
c -> new InMemorySnapshotStore(c.getComponent(Converter.class))
))
.componentRegistry(cr -> cr.registerModule(accountModule))
.start();
This configuration keeps snapshotting local to the entity definition:
-
the
SnapshotPolicydecides when a snapshot should be created -
the
SnapshotStoredecides where snapshots are persisted -
the entity module ties both concerns together
If you are migrating an Axon Framework 4 application, this is the closest equivalent to configuring snapshot behavior on the aggregate repository.
Spring Boot configuration
For Spring Boot applications, snapshotting can be enabled by adding @Snapshotting alongside the @EventSourced annotation on the entity class:
@EventSourced
@Snapshotting(afterEvents = 100)
public class Account {
// entity behavior omitted for brevity
}
The annotation supports two threshold attributes:
-
afterEvents: trigger a snapshot after a fixed number of events have been applied during sourcing. -
afterSourcingTime: trigger a snapshot when the total sourcing time exceeds the given ISO-8601 duration (for example,"PT5S"for five seconds).
When both attributes are set, either condition independently triggers a snapshot.
Use a bare @Snapshotting (no attributes) when the threshold is inherited from a higher-level annotation on an enclosing class, package, or module.
See annotation-based configuration for details.
For more complex policies that require payload inspection, declare the entity as an explicit EventSourcedEntityModule bean and configure the SnapshotPolicy programmatically.
Spring’s component registry picks up Module beans automatically, so this integrates with the regular Spring Boot setup:
@Configuration
class AccountConfiguration {
@Bean
EventSourcedEntityModule<String, Account> accountModule() {
SnapshotPolicy snapshotPolicy = SnapshotPolicy.afterEvents(250)
.or(SnapshotPolicy.whenEventMatches(
msg -> msg.type().qualifiedName().equals(
new QualifiedName(AccountClosed.class)
)
));
return EventSourcedEntityModule.declarative(String.class, Account.class)
.snapshotPolicy(snapshotPolicy)
.build();
}
@Bean
SnapshotStore snapshotStore(AxonServerConnectionManager connectionManager, EventConverter converter) {
return new AxonServerSnapshotStore(connectionManager.getConnection(), converter);
}
}
Snapshot storage
Axon Framework 5.1 uses a dedicated SnapshotStore for persisting snapshots.
Snapshots are stored separately from the regular event stream, and the snapshot store is responsible for loading the latest compatible snapshot for an entity.
If no usable snapshot is available, the entity is reconstructed from the event stream.
InMemorySnapshotStore
Use InMemorySnapshotStore for tests and other ephemeral setups where snapshot persistence is not required.
AxonServerSnapshotStore
Use AxonServerSnapshotStore when you want persistent snapshot storage backed by Axon Server.
|
When migrating from Axon Framework 4, make sure your application no longer assumes snapshot persistence is part of the event store implementation itself. Snapshot storage is now an explicit dependency of the entity module. |
Reusing existing Axon Framework 4 snapshots
When a context runs on Dynamic Consistency Boundary (DCB) storage, Axon Server keeps the snapshots Axon Framework 4 takes, both those taken before the context was migrated and those taken by Axon Framework 4 applications still running against it. Axon Server stores such an aggregate-based snapshot under the identifier of its aggregate, positioned in the context-wide event stream, and identified by its payload type (the aggregate class) and revision.
An Axon Framework 5 application can start from these snapshots instead of replaying an entity’s full history. An aggregate-based snapshot holds Axon Framework 4 aggregate state, in the format it was serialized with, so it is lifted into the entity type explicitly, through a snapshot transformation:
XStream xStream = new XStream();
xStream.allowTypes(new Class<?>[]{LegacyAccount.class});
xStream.alias("com.example.bank.BankAccount", LegacyAccount.class);
SnapshotTransformerChain chain = SnapshotTransformerChain.builder()
.register(SnapshotTransformation
.pathFromAggregateBased(VersionedType.of("com.example.bank.BankAccount", "0.0.0"))
.storedBy(new XStreamConverter(xStream))
.to(VersionedType.of("com.example.bank.Account", "1.0.0"))
.transform(LegacyAccount.class, legacy -> new Account(legacy.id, legacy.balance))
.build())
.build();
configurer.componentRegistry(registry -> registry.registerComponent(SnapshotTransformerChain.class, c -> chain));
-
pathFromAggregateBasednames the payload type and revision of the Axon Framework 4 snapshots to start from. A snapshot without a revision has version0.0.0. Register a path for each revision you can read. -
storedBydeclares the converter the snapshot was serialized with. Axon Framework 4 serializes snapshots with XStream by default, which theXStreamConverterof theaxoniq-legacymodule reads. -
toandtransformmap the stored state into the entity type, identified by its name and version.
Registering the chain is all that is needed: when an entity is sourced, Axon Framework 5 also asks Axon Server for the aggregate-based snapshot of its identifier, and lifts it before the entity sees it.
Rules for aggregate-based snapshots:
-
An aggregate-based snapshot is only used when its payload type and revision match a declared path. Any other snapshot stored under the same aggregate identifier, such as one of a revision you did not declare, is passed over, and the entity is sourced in full.
-
An aggregate-based snapshot is only looked for when the entity has no snapshot of its own. Once sourced, the entity writes and uses snapshots of its own type, even when Axon Framework 4 keeps writing newer ones.
-
Give the entity a name or version that differs from the payload type and revision of the Axon Framework 4 snapshots, so a snapshot of either cannot be mistaken for the other.
-
The lookup requires an Axon Server version that supports snapshot-aware sourcing. Against an older Axon Server, aggregate-based snapshots are not found, and entities rebuild from their event streams.
Running Axon Framework 4 and 5 side by side
Axon Framework 4 and 5 applications can source the same entities from the same DCB context at the same time. Axon Framework 4 keeps starting from its own snapshots, and never sees those Axon Framework 5 writes, as they are stored separately.
Axon Server tags the events Axon Framework 4 appends with the aggregate type as key, and with AGGREGATE_ID, both with the aggregate identifier as value.
An Axon Framework 5 entity sources those events through the aggregate type tag.
For Axon Framework 4 to see the events Axon Framework 5 appends to the same entity, tag them the same way, including AGGREGATE_ID.
Without a transformation
Without a declared path, existing Axon Framework 4 snapshots are not used: affected entities rebuild from a full event replay, and fresh snapshots are written from then on.
This is not a correctness problem: a snapshot is only an optional cache, and every entity can be rebuilt from its event stream. It is a performance one, and the cost depends on your domain. Until fresh snapshots accumulate, each affected entity is sourced from its full history, so the first load after migration is slower. For short streams this is negligible, but an entity with a long stream, hundreds of thousands or millions of events, can suffer a significant response-time hit. Assess the impact against your longest-lived entities, and declare a path for those whose replay would be too slow. For the storage engine choices that decide whether a context runs in aggregate-based or DCB mode, see the Event Store migration.