Currently, we have adopted an axon framework for an event-based application with MongoDB as the database in the SaaS application. We have adopted the approach of adding the field tenantId to all the events created.
For legal reasons, I have to make the application multitenant(SaaS) with a database per tenant. Before Proceeding with technical discussion will provide details about the versions we are using:
axon-framework - 4.4
axon-mongo - 4.4
We are also using other microservices running on spring-boot and MongoDB wherein, we are able to achieve the same by overriding few classes of MongoDB.
Now we are trying the similar approach with axon-mongo, but we are not able to find any appropriate help where we can change the DB at runtime based on Tenant.
It would be great if you can assist how we can achieve this. Also, any code example would be helpful. If this is not possible what are other alternatives?
I know how awkward that sounds written a couple of months after your post. I’m really sorry about that. It seems that your post somehow managed to remain unnoticed within the pile of other things until now.
Since it’s been quite some time, do you mind me asking if the question is still relevant? If it is, please let us know what your current situation is? Otherwise, it will be great to hear how did you solve it?
Either way, thank you for choosing Axon Framework. I hope it will serve you well.
Yes Milen, this is valid and thanks for your reply. Currently for time-being, What I have done is using a single axon framework db for all the tenants and added tenant Id to the existing aggregate identifier.
This works fine but the processing might be slow as it is a single db. And with GDPR issues, I have to keep data separate which is also a concern.
I assume you use MongoDB as Event Storage, correct? I am not too familiar with it myself so I don’t know what MongoDB’s ecosystem has to offer. But generally speaking, if you have good reasons to stick to MongoDB as Event Storage then it’s worth looking if there is anything that can provide multitenancy on that level. I’d look for some extension or 3rd party solution that can make multiple MongoDBs appear as one to AxonFramework and then do the routing accordingly.
If there is none then you would have to “hack” the Axon’s MongoDB extension. I have no clue how much work would that be but it certainly feels like a lot to me. And then you have to ask yourself if you are comfortable with managing that on your own as both MongoDB and AxonFramerwork evolve. From my personal experience, the maintenance cost in such cases is far bigger than the cost of creating the solution.
If using MongoDB as Event Storage is not a strict requirement, I’d strongly recommend doing a PoC with Axon Server instead. Axon Server Enterprise comes with multi-context support which would allow you to model your multitenancy requirement relatively easy. There is also Axon Data Protection which will likely address the GDPR issues you mentioned.
Please let me know if this is an option you are willing to consider. If so, I can connect you with colleagues of mine who can provide more details, trial keys, etc.
Finally, in case you haven’t seen them, there are other topics where multitenancy was discussed. Perhaps they can give you some hints if you choose to proceed with doing this on your own.