Home›Design›Multi-tenant SaaS
Course · Spread the dataLesson 21 of 330 read
Design doc · Multi-tenant SaaS

Multi-tenant SaaS

One database for all customers, one schema each, or one database each?

Applications like this: B2B software where each customer is a company with its own users and data. This page is written the way I write a database design doc: the workload first, then where each kind of data goes and why, then how it grows, what breaks, and what I rejected.

The short answerStart with a shared schema and a tenant_id on every row, enforced by row-level security in PostgreSQL or by a mandatory filter in the data layer. Move very large tenants to their own database when they need it.
1 · The workload

What the application asks of the database

DimensionThis application
WritesVaries by tenant. A few tenants usually produce most of the load.
ReadsAlways inside one tenant.
Read to writeTypical application mix.
ConsistencyStrict inside a tenant. Tenants never see each other's data.
LatencyInteractive.
GrowthBy number of tenants, plus a few that grow very large.
Connected

Real failures behind this design

These incidents are why the choices below matter. Each one is written up with its numbers and sources.

It happened
Check your own
2 · What goes where

Each kind of data, its store, and why

ApplicationAll tenants, shared tablesPostgreSQL or MySQLSOURCE OF TRUTHVery large or regulated tenantsTheir own database, same schemaSOURCE OF TRUTHPer-tenant settings and routingA small catalog tableSOURCE OF TRUTH

Solid arrows: the application writes there. Dashed arrows: a copy fed from the source of truth (change data capture, an outbox, or a cache fill), which can be rebuilt from it.

DataWhere it goesWhy
All tenants, shared tablesPostgreSQL or MySQLOne schema to migrate, one pool of connections, cheap to add a tenant.
Very large or regulated tenantsTheir own database, same schemaIsolation of load and data, at the cost of more databases to operate.
Per-tenant settings and routingA small catalog tableSays which database each tenant lives in.

How each family of store behaves under traffic: Database families.

3 · Data model

The core tables and the queries they serve

CREATE TABLE projects (
  tenant_id bigint NOT NULL,
  id        bigint NOT NULL,
  name      text NOT NULL,
  PRIMARY KEY (tenant_id, id)
);

ALTER TABLE projects ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON projects
  USING (tenant_id = current_setting('app.tenant_id', true)::bigint);

-- the application sets the tenant at the start of each transaction
BEGIN;
SET LOCAL app.tenant_id = '42';
-- queries for this tenant run here
COMMIT;
Try it · real PostgreSQL in your browser

Run it: row-level security

Two tenants share one table. A policy adds the tenant filter to every query, so a query that forgets its WHERE clause still sees only its own rows.

  1. Connect as the app, for tenant 1
    SET ROLE app;
    SET app.tenant_id = '1';
    SELECT * FROM notes;
  2. Switch to tenant 2
    SET app.tenant_id = '2';
    SELECT * FROM notes;
  3. Write into another tenant
    INSERT INTO notes VALUES (1, 'Written by Globex into Acme');
  4. Connect as the table owner
    RESET ROLE;
    SELECT * FROM notes;
4 · How it grows

Start simple, change one thing at a time

  1. Shared schemaCheapest and simplest. Fine for thousands of small tenants.
  2. A few tenants dominateMove them to their own databases, routed by the catalog table.
  3. Many large tenantsShard by tenant_id across several databases. Citus (for PostgreSQL) and Vitess (for MySQL) do this.

Scale it: watch this design grow

Pick a stage, then throw something at it. The numbers are a model to show the shape of each change, not a measurement; the measured numbers are in the lab notebooks.

5 · What breaks first

The failures to plan for

What happensWhat you seeWhat I do
A missing tenant filterData leak between customersRow-level security, or a data layer that cannot build a query without tenant_id.
One tenant's heavy jobEvery other tenant slows downRate-limit per tenant and move heavy tenants out.
Schema per tenant at thousands of tenantsMigrations take hours, catalog bloatPrefer shared schema with tenant_id.
6 · Alternatives

What I considered and did not pick

Cost. Shared schema is the cheapest per tenant. Price large tenants that need their own database accordingly.

How this page was writtenFrom the engines' documentation and common practice. Measured on real servers: row-level security hiding other tenants from a query without a filter, in the notebooks. Before you build: check the anti-patterns, size it with the capacity calculator, and write your own version from the design doc template. Want a second opinion? Submit it for review.
7 · Where to run it

On-prem, managed, EC2 or Kubernetes

StageWhere it runs
Where I would startManaged relational, shared schema
When it growsManaged for the shared tier. Kubernetes with an operator, or many managed instances, for tenants that get their own database

For a team without a dedicated database team. Who does which job in each option, and the full-cost comparison: Where to run the database.

8 · Estimate it

The numbers, worked live

The back-of-the-envelope numbers I would want before choosing anything. Move the assumptions to match your application. The link keeps your numbers, so a colleague sees the same ones.

Part 3 of 4 · Design · 24 pagesAll pages

Which database does my application need? What kind of application data goes in which database, and why, as design docs, plus the anti-patterns and the decisions behind them.