intermediateEvent Sourcing Handlers & Rebuilding State

When a command arrives for an aggregate that already has prior events, how does Axon determine that aggregate's current state before handling the new command?

There is no persisted 'current row' for the aggregate anywhere on the write side. Instead, Axon retrieves every event ever stored for that specific aggregate identifier, instantiates a brand-new, empty instance of the aggregate class using its no-argument constructor, and replays each stored event through the matching @EventSourcingHandler methods, in the exact order those events originally occurred. Only after that full replay completes does the new command handler actually run, now operating against a freshly rebuilt instance whose fields reflect the complete prior history. Axon does cache aggregate instances in memory to avoid replaying from scratch on every single command in quick succession, but the underlying model — state as the derived result of replaying history — holds true regardless of that caching optimization.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to How does a materialized view differ from an ordinary CQRS projection?← Back to all Event-Driven Microservices questions