Separate jdbc persistent store for each customer (Multi-tenancy)

Hello friends,

I must create a separate jdbc persistent store for each customer (Multi-tenancy). And I need to resolve this during runtime. I am OK with embedding the customer code in persistentId (e.g. ABCCustomer_orders_actor) and using the substring to resolve the respective db connection.

One closest option seems to be Customized loading of the db connection SlickDatabaseProvider. But it does not seem to be helpful, but only config is available from the abstract database method. I think this is called only once during init, so not suitable?

class CustomDatabaseProvider extends SlickDatabaseProvider {
  override def database(config: Config): SlickDatabase = {
    //My requirement: resolve the respective database of the customer.
  }
}

Is there any other way I can solve this problem?

Appreciate any kind help.
MK

I think it is tricky to make it completely dynamic, but if up front listing of each customer db in config is workable I think you could make the persistent actor decide which journal plugin id to use at startup, based on parsing that out of the persistence id.

Look at akka.persistence.RuntimePluginConfig for classic persistence, or Behaviors.setup + EventSourcedBehavior#withJournalPluginId in EventSourcedBehavior/typed persistence.

1 Like