Lesson 10 of 14 · Assurance and response
Tenant isolation in a multi-tenant SaaS
Isolate customers of a shared SaaS from placement down to every database row: choose silo, pool or cell models per tier, carry a verified tenant context through every service, enforce it in the data layer, encrypt with per-tenant keys where needed, and prove isolation with automated tests.
Isolation is a stack, not a switch
"Multi-tenant" means many customers share infrastructure. Isolation must hold at every layer, because a leak at any one of them is a breach:
Placement which tenants share which cell / cluster / database
Edge tenant identified from the verified token; per-tenant rate limits
Services tenant context carried and checked in every service
Data queries scoped by tenant; database-enforced (RLS / schema / database per tenant)
Keys per-tenant encryption keys where required
Operations tenant-aware logs, backups, deletion, support access
A block of flats: everyone shares the lift and the roof (infrastructure), but each flat has its own front door key, its post goes only into its own letterbox, and the caretaker's master key is kept in a logged safe. Some residents pay for a house of their own (a dedicated cell).
Placement: silo, pool and cells
| Model | What is shared | Good for | Cost |
|---|---|---|---|
| Pool | Everything; tenant ID in every row | Many small tenants | Cheapest; isolation is all logical |
| Bridge | Shared compute, a schema or database per tenant | Mid-size tenants | Moderate |
| Silo | Nothing: dedicated stack | Regulated, very large, data residency | Highest |
| Cells | Each cell is a complete stack for a group of tenants | Limiting blast radius at scale; mixes all of the above | Operational tooling |
Most SaaS platforms combine them: small tenants pooled inside shared cells, large or regulated tenants in dedicated cells, all deployed from the same code and pipeline with different parameters.
Tenant context: verified, carried, enforced
- The tenant comes from the verified token (a
tenantclaim, or a lookup from the user's identity), never from a client-supplied header or body field. - The gateway validates the token and passes tenant context downstream (forwarded token or internal signed context, as in the token lesson).
- Every service checks that the resource being accessed belongs to that tenant. Libraries in the golden path do this by default so individual teams cannot forget.
- Logs and traces carry the tenant ID, so you can answer "who accessed tenant B's data?".
Data layer enforcement
The most common real leak is a query without a tenant filter. Add a second check the application cannot forget:
- Row-level security (PostgreSQL): the connection sets
app.tenant_idfrom the verified context; policies hide every other tenant's rows, even for a query that forgotWHERE tenant_id = …. - Schema or database per tenant for larger tenants: credentials per tenant, so one tenant's connection cannot see another's tables.
- Object storage: prefixes per tenant plus IAM conditions, or a bucket per large tenant.
- Caches and queues: tenant-prefixed keys and topics with ACLs; no shared global keys like
user:42.
Connection pools and RLS
With pooled connections, set the tenant per transaction (SET LOCAL app.tenant_id = … inside the transaction) so a reused connection never carries the previous request's tenant.
Keys: per-tenant encryption
With envelope encryption, each tenant's data keys are wrapped by a tenant-specific key in a KMS:
- Crypto-shredding: destroy the tenant key and their data, including in backups, becomes unreadable.
- BYOK / customer-managed keys: the customer's key, in their KMS account, wraps their data keys; they can revoke access. HYOK goes further: the key never leaves the customer's environment.
- Costs: more KMS calls (cache data keys briefly), key-rotation processes per tenant, and support runbooks for "customer revoked their key".
Noisy neighbours are an isolation problem too
One tenant exhausting shared capacity is an availability breach for the others. Per-tenant rate limits at the gateway, quotas on queues and topics, bounded connection pools, and moving fast-growing tenants to their own cell.
Prove it: cross-tenant tests
Isolation you have not tested is a belief. Automate:
- Contract tests in CI: tenant A's token against tenant B's resources on every API endpoint, expecting 403/404 and no data.
- The same as a continuous production canary with synthetic tenants.
- RLS checks: run representative queries without a tenant set; they must return nothing.
- Periodic third-party penetration tests focused on tenant boundaries.
Try it
In PostgreSQL, create an accounts table with tenant_id, enable and force RLS with the policy from the cheat sheet, and connect as a non-owner application role. Set app.tenant_id to tenant A and run SELECT * FROM accounts;: only A's rows appear. Run it without setting the variable: the query fails or returns nothing. That is the safety net a forgotten WHERE clause needs.
Recap
- Isolation must hold at placement, edge, services, data, keys and operations.
- Combine pool, silo and cells per tenant tier, all from one codebase.
- Tenant context comes from verified tokens and is enforced in every service and, again, in the database (RLS or per-tenant schemas).
- Per-tenant keys enable crypto-shredding and customer-managed keys; test isolation continuously.
This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.