Multiple Databases

440 views
Skip to first unread message

Richard

unread,
Feb 2, 2018, 7:18:47 AM2/2/18
to Axon Framework Users
Hi 

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.

Frans van Buul

unread,
Feb 5, 2018, 4:16:36 PM2/5/18
to axonfr...@googlegroups.com
Hi Richard,

There are more people doing that. I've uploaded some sample code here: https://github.com/AxonIQ/giftcard-demo-series  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,

Frans van Buul
AxonIQ
+31 6 5068 2984

 

--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframework+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Richard

unread,
Mar 12, 2018, 6:48:02 AM3/12/18
to Axon Framework Users
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,
Richard

Reply all
Reply to author
Forward
0 new messages