Multiple Databases


We have a requirement to use different databases for the eventstore and the query read model.

I a can’t find a good example on how to do this. Does anyone have experience of using Axon this way?

Our end goal would be to have an eventstore in one database and tracking processors which read that eventstore database and stores the entity/aggregate in a different database, and possible another database for saga management.

I am struggling with the autowiring and beans in Spring when using multiple JpaRepositories.

Any help would be much appreciated.

Hi Richard,

There are more people doing that. I’ve uploaded some sample code here: If you look into the giftcard-multiconfig-monolith sample, you’ll find 3 configuration examples (which you can switch by changing profile): everything on a H2 database (that’s the most basic case that many people start with), 2 separate Postgres databases, and a combination of Postgres and AxonDB (more on that below). I haven’t used sagas, but setting up a separate db for sagas for simply be repeating the case of splitting of the events database.

The code to wire up two separate Postgres databases may seem a bit complex. It’s worth noting that for 90% or so, this is unrelated to Axon. When using JPA-based persistence, Spring Boot does a lot of work for you in terms of beans it instantiates by default. Once you go from one to two databases, you have to do all that work manually, twice: the DataSource, the TransactionManager, the EntityManagerFactory, the EntityManager etc. In this example, I’m also using Flyway to manage the database schema.

When setting up 2 of all of these in Spring, it’s necessary that Spring knows which instance to inject in which cases. This is achieved by marking one instance of each bean type as @Primary, and marking other ones with some @Qualifier. In this case, I’m using @Qualifier(“events”) for everything that has to do with storing events and snapshots. My tracking tokens and projection records go into the @Primary stack.

Once you have everything set up on the JPA level, it’s a simple matter of instantiating Axon’s TokenStore to use the @Primary stack, and Axon’s EventStore to the @Qualifier(“events”) stack.

Now, since you’re already considering to store your events in a separate database system, you might want to checkout AxonDB. This system is highly optimized for storing events for event-sourcing. It provides superior performance to an RDMBS in this role, pushes updates to tracking processors actively, and has a great query tool for ad-hoc queries referencing event payloads. For this reason, I’ve also included a profile that combines Postgres with AxonDB.

Hope this is useful,

Hi Frans van Buul

Thank you for taking the time to produce such a detailed example, i will be starting a new piece of work in a few weeks and using this as the bases. I've been keeping a track on axondb and will evaluate it as part of our use case, along with the GDPR module.

Much appreciated,