Choosing a database design for multi tenant application!

2025-06-05

Multi tenant applications are very common now days. They allows business to host same source code for multiple customers in single infrastructure, it provides benefits of data isolation and security. There are multiple ways we can design databases for multi tenant application. Each design decision influences, data isolation, security, scalability and maintenance cost. In this blog, we'll learn different approaches of multi tenant database design.

We'll use PostgreSQL example throughout the blog, but the concepts can be applied to any database.

What is multi tenancy?

The term tenant is not new and not tied to software development. You'll get something similar when you just type tenant on google.com

tenant: a person who pays money (rent) to the owner of a room, flat, building or piece of land so that he/she can live in it or use it

Similarly, in software development, multi applications are developed and deployed by a single entity. Customers can use the deployment by paying required fees.

Some notable example are:

Database Design Choices

We have mainly three database design choices, each have their own pros/cons. Each design decision influences, data isolation, security, scalability and maintenance cost.

  1. Database Per Tenant
  2. Schema Per Tenant
  3. Discriminator Column in every table

Alt text

Let's explore every option in details

Database Per Tenant

In this approach, we create a database for each tenant. This approach provides higher degree of isolation and data security. Due to separate database, efforts like maintenance, change in SQL schema, data migration, etc will be increased.

If we store all databases in single database server, then we'll end up having large number of connection pools from application to the single database server. It can cause performance bottleneck.

When should we use this approach:

ORM & Framework Support

Schema Per Tenant

In this approach, each tenant gets its own database schema within a single shared database. A schema acts as a namespace that holds all the tenant's database objects—such as tables, indexes, and sequences—separately from other tenants. This ensures a strong level of logical isolation while still using a single database instance.

From a design perspective, this approach is somewhat similar to the Database Per Tenant model, but with significantly lower infrastructure overhead. Since all schemas live within the same database, you can reuse a shared connection pool, reducing the cost of maintaining multiple database connections.

However, it's important to note that not all relational databases support schemas in the same way. PostgreSQL and SQL Server support schemas well, while MySQL has no support.

When should we use this approach:

ORM & Framework Support

Discriminator Column

In this approach, we add a tenant_id column in every table to identify rows belongs to the tenant.

col_1 col_2 tenant_id
Row 1 Data 1
Row 2 Values 2

This approach have lower degree of data isolation and security, but higher degree of scalability than above two approaches. It significantly reduces maintenance effort and schema migration overheads, at the same time it increases development efforts.

In this approach data isolation provided by the application layer, whereas other two approaches provides data isolation without any additional effort.

When should we use this approach?

ORM & Framework Support

Don’t Use tenant_id in the Primary Key

A common mistake in multi-tenant systems is including tenant_id as part of the primary key.

For example:

CREATE TABLE users(
    id <data_type>,
    tenant_id <data_type>,
    name VARCHAR(255),
    email VARCHAR(255),
    PRIMARY KEY (tenant_id,id)
)

While this might make sense to have tenant_id in primary key, but it can lead to complexity in querying and joins, ripple effect when deleting/modifying primary keys on every other table, etc.

Summary

We have seen three approaches to design multi-tenancy database. Below is the metric table that summarizes when should we use any approach.

* Database Per Tenant Schema Per Tenant Discriminator Column
Security High Moderate Low
Data Isolation High Moderate Low
Scalability Low Moderate High
Maintenance High Moderate Low
Development Effort Moderate (Higher than schema per tenant due to connection pool management) Low High
No. of tenants <=100 <=10000 Unlimited
Database Support All Databases Few Databases All Databases