Rebuilding projections in Axon Framework 3

CQRS (Command Query Responsibility Segregation) allows you to have separate models for reading and writing. Combining that pattern with Event Sourcing leads to a powerful capability: updating query (read) models, based on events. In real-time or rebuilding them from an existing collection of events. This post focuses on such projections, in applications that are built on Axon Framework.

In earlier versions of Axon (2.x), rebuilding projections could be done by organizing them into clusters, and then wrapping them with a ReplayingCluster instance. The ReplayingCluster can detect when a replay has finished, and signal that. I’ve used that class with good results in a couple of projects. However, in a recent project I switched to Axon 3, where it no longer exists.

Event Processors

Indeed, Axon 3 introduces two new concepts: the Subscribing and Tracking event processors. The Subscribing processor follows the event stream in real-time, whereas the Tracking processor keeps track of events it has processed (using a token). This means that the Tracking processor can be stopped and resumed, and it will pick up processing where it left off. I wrote something about using the Tracking processors earlier.

Example application

For the project I mentioned, I wrote some custom code to get replays (with Axon 3) to work and, more specifically, determine when a replay is finished. A discussion in the Axon group triggered me to rewrite that approach into a simple application and push that to GitHub. The app has a very simple domain: Persons can be registered (through a controller), and there is a single projection which can be rebuilt. The progress and status of individual tracking processors (so, rebuilds) can be queried through another controller.

Warning: the application is a proof of concept of one potential implementation for replays, and probably needs significant cleanup. It could also stop working with future Axon updates 🙂

The application can be found here: https://github.com/mrook/axon-projection-rebuild-demo

Let me know what you think!

Michiel Rook

Michiel Rook is an experienced, passionate & pragmatic freelance coach, developer & speaker from the Netherlands. He loves helping teams and companies to develop better software and significantly improve their delivery process. When he’s not thinking about continuous deployment, DevOps or event sourcing he enjoys music, cars, sports and movies.

Leave a Reply

Your email address will not be published. Required fields are marked *