Every SaaS product makes the same promise to every customer: your data is yours, and nobody else's actions will break your experience. Multi-tenancy is how you keep that promise while running one platform for many customers. The model you choose shapes your security, your costs, your migrations and your operations for years, and it is expensive to change later.

The three common models

1. Shared database, shared schema

All tenants live in the same tables. Every tenant-owned row carries a tenant_id.

  • Pros: cheapest to run, simplest to migrate (one schema), easy cross-tenant analytics for the platform team, scales to very large numbers of small tenants.
  • Cons: isolation depends entirely on application and database rules being correct; one heavy tenant can affect others; per-tenant backup and restore is harder.

2. Shared database, schema per tenant

Each tenant gets its own schema (namespace) inside one database.

  • Pros: stronger logical separation, per-tenant restore is easier, a bug in a query's tenant filter cannot leak data across schemas.
  • Cons: migrations must run once per tenant; thousands of schemas strain tooling and connection pools; cross-tenant reporting needs extra work.

3. Database per tenant

Each tenant gets a separate database, sometimes on separate infrastructure.

  • Pros: strongest isolation, per-tenant performance tuning, data residency per customer, straightforward per-tenant backup, restore and deletion.
  • Cons: highest cost and operational effort; provisioning, migrations and monitoring must be fully automated to scale beyond a handful of tenants.

How to choose

Rather than starting from a preference, answer these questions:

  1. How many tenants do you expect, and how big are they? Thousands of small tenants push towards a shared schema. Dozens of large enterprise tenants make separate databases realistic.
  2. What do your customers' contracts and regulators require? Some industries or customers require physical separation or regional hosting.
  3. How uneven is the load? If a few tenants generate most of the traffic, you need a way to isolate them, regardless of the default model.
  4. How much operational automation can your team sustain? Database-per-tenant without automation becomes a full-time job.

Many products end up with a hybrid: a shared schema for most tenants, and dedicated databases for the few that need or pay for them. If you go that way, put tenant-to-database routing behind a single resolver from day one.

Enforcing isolation in a shared schema

In the shared model, the risk is simple: one query that forgets WHERE tenant_id = $1 leaks data. Relying on developers to remember it in every query does not scale. Enforce it centrally instead.

Resolve the tenant once per request

Determine the tenant from the authenticated identity (never from a client-supplied header alone) and attach it to the request context. Everything downstream reads it from there.

Scope queries in one layer

Make the data-access layer require a tenant for every tenant-owned query, so an unscoped query is a compile-time or review-time error rather than a production incident.

Add a database backstop with row-level security

PostgreSQL's row-level security lets the database itself refuse rows from other tenants, even if application code has a bug:

sql
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
ALTER TABLE orders FORCE ROW LEVEL SECURITY;

CREATE POLICY tenant_isolation ON orders
  USING (tenant_id = current_setting('app.tenant_id')::uuid)
  WITH CHECK (tenant_id = current_setting('app.tenant_id')::uuid);

At the start of each transaction, the application sets the tenant:

sql
BEGIN;
SELECT set_config('app.tenant_id', '4b1f…', true); -- true = local to this transaction
SELECT * FROM orders WHERE status = 'open';         -- only this tenant's rows
COMMIT;

Two details matter. The application should connect as a role that does not own the tables and does not have BYPASSRLS, otherwise policies are skipped. And with connection pooling, the setting must be transaction-local, as above, so it never leaks to the next request using the same connection.

Test isolation explicitly

Add integration tests that create two tenants, write data as one and assert the other can never read, update or delete it through any endpoint. These tests are cheap and catch the most damaging class of SaaS bug.

Noisy neighbours

Isolation is not only about data. One tenant running a huge export should not slow everyone else down.

  • Rate limits and quotas per tenant at the API layer.
  • Per-tenant queues or fair scheduling for background jobs, so one tenant's backlog does not starve others.
  • Per-tenant metrics, so you can see who is consuming capacity.
  • A path to move a heavy tenant to dedicated resources, which is far easier if routing is already centralised.

Tenant lifecycle

Design these flows early, because they touch every part of the system:

  • Provisioning: creating a tenant should be one automated operation (records, defaults, roles, and for isolated models, the schema or database).
  • Configuration and feature flags per tenant.
  • Migrations that run safely across all tenants, with a way to track which tenant is on which version in non-shared models.
  • Offboarding: export and verifiable deletion of a tenant's data.

A starting recommendation

For most new B2B SaaS products, a reasonable default is:

  • shared database and shared schema,
  • tenant resolved from the authenticated identity,
  • tenant scoping enforced in the data-access layer,
  • PostgreSQL row-level security as a backstop,
  • per-tenant rate limits and metrics,
  • and tenant routing behind a resolver, so that a dedicated database for a large customer later is a configuration change, not a rewrite.

Whatever you choose, write the decision down with the alternatives you rejected and why. Two years from now, someone will ask, and the reasoning matters as much as the choice.