Multi-tenancy
How I keep one tenant out of another tenant data: tenant keys, row-level security and composite foreign keys.
Two companies, one building: every room has its own key.
Both companies share the lift, the lobby and the electricity bill. Neither can open the other's door.
How it works
Multi-tenancy means one running system serves many customers, called tenants. Each tenant sees only its own data and settings. The design question is how strongly tenants are isolated, and what that costs.
| Model | Shape | Isolation | Cost and ops |
|---|---|---|---|
| Silo | Database or stack per tenant | Strongest | Highest. Migrations times N |
| Bridge | Schema per tenant in one database | Medium | Many schemas to migrate |
| Pool | Shared tables with a tenant_id column | Logical only | Lowest. Easiest to run |
Most early SaaS products start with a pool, and add a silo later for large or regulated customers.
Pool with row-level security
In a pool, the boundary cannot depend on every query remembering a WHERE clause. I move the boundary into the database with row-level security (RLS).
alter table conversations enable row level security;
alter table conversations force row level security;
create policy tenant_isolation on conversations
using (tenant_id = current_setting('app.tenant_id')::uuid)
with check (tenant_id = current_setting('app.tenant_id')::uuid);
The rules I hold to:
- Every tenant row carries
tenant_id. The app sets the current tenant once per transaction. - Policies fail closed. With no tenant set, the query errors. It never returns all rows.
- The tenant comes from the session, never from a URL or a request body.
FORCEmatters, so the table owner is bound by the policy too.- Foreign keys carry the tenant as well, so a row cannot point at another tenant's row.
- An isolation test creates tenants A and B and proves A cannot read or write B, on every tenant table.
If application code forgets a filter, the policy still applies. That is the point.
Neighbours and hierarchy
One heavy tenant can slow everyone in a pool. The usual defences are per-tenant rate limits, job concurrency limits, query timeouts and usage caps. A very large tenant can move to its own silo.
Resellers add a level, agency over client workspace. I keep that hierarchy shallow, two levels, and make agency access read-only and audited where I can.
Where I used it
All three of these are multi-tenant, and each case study says what it costs.
- IMS has forced row-level security on every tenant table, and foreign keys carry the tenant. An action mistake then meets a wall in the database. The cost is that context, grants and migrations need integration tests that use real roles.
- PeopleCore HR uses RLS as the tenant boundary, on Supabase and Postgres. It is live. The data is HR data, so tenants must never see each other.
- POS SaaS shares one schema with a tenant column and RLS. I rejected a project per tenant, a schema per tenant, and application filtering alone. My rule there is that a table does not ship without a cross-tenant test. POS is early-stage, so that is a design.
Trade-offs
| Benefit | Cost |
|---|---|
| One deploy and one migration. Cheap | One bug can leak across tenants |
| The database enforces isolation | RLS needs planning care and tests |
| Easy cross-tenant analytics for operators | Noisy neighbours, harder per-tenant restore |
Two costs I keep in view.
- A policy is code. Every table needs one. A policy bug is a cross-tenant leak, so the test is not optional.
- A pool is not for everyone. If a customer needs physical separation or data residency, give them a silo. For a single-customer internal tool, none of this is needed.