AxonDB per tenant

Hi,

I am looking at how can I use Axon Framework in a multitenant schenario where each tenant has its own schema and separate axondb instance.

In theory I can see that I can use EntityManagerProvider to get current unitofwork and set schema on entitymanager connection (for postgres, set search_path=).

However, I am unable to figure out how can I use one axondb instance per tenant.
Any suggestions?

Regards,
Swapnil

Hi Swapnil,

in AxonDB, you could use a Context per tenant. However, multi-context is only supported in the enterprise edition. If you want to have a separate instance per tenant, you’d also need to configure a client for each tenant.

An alternative, that you could consider, is storing the tenant ID in the meta data of events. When reading events, you could filter on this tenant ID. This is an approach we have used in some of the saas-based projects that I was involved with.

Hope this helps.
Cheers,

Allard

Thanks Allard.

So, with multi-context support, we can still have one EventHandler instance which can process events for all the tenants?
Idea is to just isolate tenant data but share the application processes. This will allow to add tenants at runtime and we do not need to worry about adding static configurations every time new tenant is added.

Regards,
Swapnil

Hi Swapnil,

I have been involved with a couple of multi-tenant systems in the past. In some cases, physical separation of data has to exist for legal reasons. In other cases, it is seen as an easier way to ensure data doesn’t accidentally fall into the hands of the wrong tenant.

In the first case, setting up tenants will always be more involved, since physical storage needs to be set up and (db) clients need to be aware of this new storage. As far as I have seen, these cases are relatively rare, and can also be solved using data encryption.

In the latter case, I would recommend storing your events into a single event store, and marking all events with the tenant ID. If all action is always performed by (or on behalf of) a tenant, you could use interceptors to validate this meta data is present/correct on these messages. Some of our clients use this in SaaS based medical application, where security measures are in place to prevent data to be served to the wrong tenant.

My suggestion of using contexts in AxonDB will help you separate the data, but unfortunately, does not help when a single application reads from more than one tenant. Basically, these contexts should be seen as separate databases. They are meant to isolate data from different bounded contexts (as defined by Domain Driven Design). While certain applications should be able to read from multiple contexts at once, this is not designed to be very dynamic.

Hope this helps.
Allard