LOAD-BEARINGfree
Architecture labTier 13 · unit 9 of 31

Multi-tenant SaaS

A B2B application serving thousands of customer organisations from shared infrastructure. Every request belongs to exactly one tenant, no tenant may ever observe another's data, and the largest customer must not be able to starve the rest.

Envelope5,000 tenants · largest is 400× the median · one leak is every customer's leak

What the design must do

  • Tenant identity established at the edge, before any business logic runs
  • Isolation enforced below hand-written queries, not by remembering a filter
  • Per-tenant quotas so one customer cannot consume the shared capacity
  • Cache keys scoped by tenant — an unscoped key is a data leak

The worked envelope

Assumed. 5,000 tenants sharing infrastructure, the largest 400× the median, a shared connection pool of 100.

Worked envelope
Load units4,999 median + one at 400 = 5,399 units
Largest tenant400 ÷ 5,399 = 7.4% of all load, from 0.02% of tenants
Storagemedian 5 GB × 5,000 ≈ 25 TB; largest alone 2 TB
Per-tenant opsbackup, reindex, migration: 400× spread in duration
Pool exhaustionone tenant holding 40 of 100 connections = 40% of capacity
Blast radiusone missing WHERE clause = 5,000 customers

What the numbers decide. Nothing here is about volume. Twenty-five terabytes and a hundred connections are unremarkable numbers, and that is exactly the point: this lab's constraints are all about distribution and isolation, not scale.

The 400× spread means every per-tenant operation you write has to work for both ends of it — a migration that takes four seconds for the median takes half an hour for the whale, and a restore procedure that assumes the median will time out when it is needed most. And the last row is the one that makes this domain different from every other lab: the failure mode is not slowness, it is one tenant seeing another tenant's data, and no amount of capacity planning prevents it.

The decisions, and what each one costs

Shared schema with a tenant column, isolation enforced below the query

Because
5,000 separate databases is 5,000 migrations, connection pools and backup jobs; a shared schema keeps operations tractable at this count
The cost
Isolation now depends on every query being correct, and one omitted predicate is every customer’s data
Rejected
Database-per-tenant, which is genuinely correct at 50 tenants and operationally impossible at 5,000

Row-level security or a mandatory scope, not a convention

Because
A filter that developers must remember is a filter that will eventually be forgotten, and it passes review because a missing clause looks like no clause
The cost
A performance cost on every query and a policy layer that must itself be tested
Rejected
Code review and discipline, which is what every company that has leaked tenant data was also relying on

Per-tenant quotas on connections, query time and request rate

Because
One tenant taking 40 of 100 connections degrades the other 4,999, and the whale is 400× the median by design rather than by abuse
The cost
A legitimate large customer hits limits and complains; quotas need to be tiered rather than uniform
Rejected
Trusting fair use, which works until the first customer writes an unbounded report query

The largest tenants get dedicated capacity

Because
At 400× the median, one tenant’s load profile is nothing like the pool’s, and their spikes are everyone’s incident
The cost
Two deployment shapes to operate, and a migration path when a tenant crosses the threshold
Rejected
Scaling the shared pool for the whale, which pays whale prices for 4,999 median tenants

Components in the reference design

Client
Browser or mobile app
API Gateway
Auth, routing, quota enforcement
Rate Limiter
Token bucket per API key
App Service
Stateless business logic
SQL DB
Relational, ACID, joins
Cache
Redis / Memcached, sub-ms lookups

The reference design

Where tenancy is decided. The tenant comes from the authenticated principal at the gateway, never from a request parameter — a tenant id the client can supply is a tenant id the client can change. Resolve it once, attach it to the request context, and treat everything downstream as operating inside that context.

Isolation that cannot be forgotten. You will write tens of thousands of queries and need every one correct, so the filter must not be something a developer remembers. Push it below the query layer: row-level security policies keyed on a session variable, or a data-access layer where the tenant predicate is applied by construction and raw SQL is unavailable. Lesson 12.04 made the argument; this is where it becomes a schema decision.

The test that actually catches breaches. A test suite with one tenant passes whether or not the filter is applied — the rows returned are correct either way. Seed a second tenant and assert its data is absent. The absence assertion is the only one that fails when the filter is missing.

Noisy neighbours. Quotas belong at the edge, before the work is done: per-tenant rate limits on the gateway, and separate connection pools or worker pools so a tenant running a 40-second report cannot occupy every thread. Lesson 9.04's bulkhead, partitioned by customer rather than by dependency.

The largest customer breaks the shared model first. At 400× the median, one tenant's working set evicts everyone else's from a shared cache and their queries dominate the shared database. The usual answer is not a different architecture but a different placement: move the handful of outliers onto dedicated infrastructure running the same code, and keep the shared pool for the long tail.

Cache keys. Every key carries the tenant. This sounds obvious and is the most common real leak in multi-tenant systems, because a cache key is written once in a helper and copied everywhere after.