Best Approach for Multi-Tenant MongoDB (Separate Databases per Tenant) in Phoenix?

Hi everyone,

I’m migrating a multi-tenant app from Node/Golang to Phoenix/Elixir. Here’s how our data is set up:

  • Each tenant has a separate MongoDB database (tenant_${tenant_id}).
  • There’s a master DB with metadata about tenants.
  • We don’t use schema validation—collections are schema-less and structure is only enforced by the application.

In my old stack, I could connect to any tenant’s DB as needed. In Elixir, I see the mongodb_driver requires the DB name when starting the connection/pool, usually at app boot.

My main questions:

  • How do Elixir/Phoenix apps usually handle selecting and connecting to the correct tenant database at runtime?
  • Is it common to start a connection pool per tenant, or are there better patterns for this?
  • Any advice, examples, or “gotchas” from folks who have done this before?

Appreciate any guidance or sample code. Thanks in advance!

You can always choose the database. It depends on if you use different MongoDB instances or different databases provided by one instance.

Check the documentation: Mongo — mongodb-driver v1.5.4

You should always a connection pool per database instance. It is much faster to reuse an existing connection than open a new one for a single request,