Backend / Multi-tenancy

Multi-tenancy

One invoicing app, three customer companies. We look at three ways to keep their data apart, watch one leak and one overloaded server, and weigh what each level of separation costs.

Intermediate7 steps~7 min
Acme — a tenant: one customer company
Birch — a second tenant
Cedar — a third tenant, later the noisy one
App — one running copy, shared by all
// THE SHORT VERSION

In 30 seconds

Multi-tenancy means one running system serves many customers, called tenants, instead of each customer getting a private copy. Much online business software is built this way.

The data can be separated in three ways: rows in shared tables marked with a tenant ID, a separate schema per tenant, or a whole database per tenant.

Sharing is cheap but risky. A missing filter can show one tenant another’s data, and one busy tenant can slow the rest. More separation reduces both risks and costs more to run.

Multi-tenancy, step by step

STEPS01 / 07

The 90% and 900 ms figures are invented, and the bars in the last step show a rough ordering, not measurements. A database per tenant only stops noisy neighbours if the databases run on separate hardware.

STEP 01 / 07 One app, many customers
THE MATH
tenants: 3 app copies: 1
  1. Step 1: One app, many customers. An invoicing product is sold to three companies: Acme, Birch and Cedar. Instead of installing a separate copy for each, the vendor runs one system that all three use. Each customer organisation is called a tenant. Sharing makes the product cheap to run and easy to update, and it raises a question at once: whose data is whose?
  2. Step 2: Model 1: shared tables. The simplest model puts everyone’s rows in the same tables and adds a tenant_id column saying who owns each row. When Acme asks for its invoices, the query filters on that column and gets back only Acme’s two rows. One database and one set of tables serve every tenant, which is the cheapest arrangement there is.
  3. Step 3: The data-leak risk. In shared tables, the filter is the only wall between tenants. One query written without it returns all six rows, and Acme is shown Birch’s and Cedar’s invoices. Careful teams do not rely on every programmer remembering: they apply the filter in one central place, or have the database enforce it with row-level security.
  4. Step 4: Noisy neighbours. Tenants who share hardware also share its limits. Cedar starts a huge report and its queries take 90% of the database server’s processing power. Acme and Birch did nothing different, yet their pages now take 900 ms instead of 50. This is the noisy-neighbour problem. Per-tenant limits help; so does moving a heavy tenant out.
  5. Step 5: Model 2: schema per tenant. A stronger model keeps one database but gives each tenant its own schema: a separate, named set of tables. Acme’s invoices live in acme.invoices and contain no one else’s rows, so a forgotten filter can no longer mix tenants. The app must still choose the right schema for each request, and all tenants still share one server.
  6. Step 6: Model 3: database per tenant. The strongest model gives every tenant a whole database, often on its own server. Nothing is shared at the data level: a tenant can be backed up, restored, moved to another country or given more power by itself, and a neighbour’s heavy report runs elsewhere. The price: every database must be paid for, monitored and upgraded.
  7. Step 7: Cost against isolation. The three models sit on one scale. Shared tables cost least per tenant and separate tenants only by a filter. A database per tenant separates them most and costs most to run. A schema per tenant falls between. Many products mix the models: small customers share, while large or regulated ones pay for a database of their own.
VALUES SO FAR
tenants3customers
app1 running copyshared
model 1····tenant_id
leak····risk
noisy····risk
model 2····stronger
model 3····strongest
price····trade-off

Spotted a mistake on this page?