One big tenant slows 200 others. How do you isolate them?
Question
Your multi-tenant SaaS API shares 1 PostgreSQL connection pool (60 connections) and one worker queue across 1 enterprise tenant (T-big) and 200 SMB tenants.
Normally, all 200 SMBs combined use 18 connections (45 ms p99), and T-big uses 6 connections. At 09:00, T-big starts a 10x bulk export demanding 60 connections, and SMB requests start timing out.
- Why did
T-bigstarve the 200 SMBs, and what changes if you cap any single tenant at 20% of the shared pool? - Compare Silo, Pool, and Bridge isolation models here, and add queue, cache, and database Row-Level Security safeguards.
Show hints (3)
- "COMPUTE POOL CAP"
Take
20%of60connections (12). Subtract12from60to see how many connections stay available for the 200 SMB tenants. - "COMPARE ISOLATION TIERS"
Giving all 200 tiny SMBs separate databases costs too much. What if SMBs share a capped pool while
T-biggets a dedicated silo? - "ISOLATE CACHE AND ROWS"
Namespace every cache key with
tenant_id, split bulk exports onto their own worker queue, and enable PostgreSQL Row-Level Security.
Show checked explanation
Without per-tenant limits, T-big's 60-connection burst fills the entire shared pool and leaves 0 slots for SMBs. Capping any tenant at 20% (12 connections) leaves 48 connections for SMBs (30 spare above their 18-connection load). Use a Bridge model: keep SMBs on a capped pool, move T-big to a dedicated DB/worker silo, prefix cache keys, and enforce PostgreSQL Row-Level Security.
POOL = 60 CAP = int(POOL * 0.20) # 12 connections max per tenant REQS = [("T-big", 60)] + [(f"smb-{i}", 1) for i in range(18)] def allocate(cap): used, big, smbs = 0, 0, 0 for tenant, need in REQS: allowed = need if cap is None else min(need, cap) take = min(allowed, POOL - used) used += take big += take if tenant == "T-big" else 0 smbs += take if tenant != "T-big" else 0 return big, smbs, 18 - smbs, POOL - used for name, cap in (("unbounded", None), ("cap_20pct", CAP)): big, smb, wait, free = allocate(cap) print(f"{name:9} T-big={big:2d} SMBs={smb:2d} queued={wait:2d}" f" free={free:2d}")
unbounded T-big=60 SMBs= 0 queued=18 free= 0 cap_20pct T-big=12 SMBs=18 queued= 0 free=30
Author-checked on CPython 3.12.10: ran the 60-connection pool bulkhead simulation, checked capacity and headroom math, and tested per-tenant token-bucket throttling and cache-key namespacing. Checked by an editor other than the author.
Walkthrough
This is the classic noisy-neighbor failure. At 09:00, T-big's 10x export (6 * 10 = 60 connections) plus SMB traffic (18 connections) demands 78 connections from a 60-connection pool. Because the pool and worker queue are FIFO and unbounded, T-big grabs all 60 slots and starves all 200 SMBs (queued=18).
First, cap blast radius inside the shared tier. Set a per-tenant bulkhead cap of 20% (12 connections) and a per-tenant token bucket at the API gateway. Excess T-big requests fail fast with HTTP 429 and Retry-After (or move to an async export queue). SMBs keep 60 - 12 = 48 connections, leaving 30 spare connections above their 18-connection demand.
Next, pick the right SaaS tenancy model:
| Model | Compute & DB layout | Noisy-neighbor risk | Cost for 201 tenants |
|---|---|---|---|
| Pool | Shared pool + quotas/bulkheads | Medium (contained by caps) | Lowest |
| Silo | Dedicated DB & queue per tenant | None | Highest (201 clusters) |
| Bridge | Dedicated silo for T-big, capped pool for 200 SMBs | Low for SMBs, none for T-big | Balanced |
Bridge fits this customer mix best. Running 201 separate clusters wastes money on small SMBs, while sharing one database with an enterprise exporter risks repeated incidents. Give T-big a dedicated database and export worker pool funded by its enterprise tier, and keep the 200 SMBs in a pooled tier with fair-share caps.
Close the remaining shared-layer leaks:
- Separate workload queues: Route heavy bulk exports to a low-priority background queue with per-tenant fair-share scheduling, never the interactive API queue.
- Tenant-prefixed cache keys: Format every Redis key as
tenant:{tenant_id}:resource:{id}and enforce per-tenant cache quotas soT-bigcannot evict SMB hot keys or leak cached reports. - PostgreSQL Row-Level Security (RLS): Run
ALTER TABLE ... ENABLEandFORCE ROW LEVEL SECURITYon each tenant table, and connect as a role withoutSUPERUSERorBYPASSRLS. SetSET LOCAL app.tenant_id = ...inside each transaction so PostgreSQL blocks cross-tenant access even if a query dropsWHERE tenant_id = ?.
Common mistakes
- Raising the shared pool from
60to200connections without per-tenant caps.T-bigstill fills the larger pool during heavier exports and crashes PostgreSQL with connection overhead. - Building 201 dedicated silos for 200 small SMBs. Idle cluster costs, schema migrations, and operational work scale linearly with every tiny customer.
- Applying only an API rate limit while sharing one Redis keyspace (
report:42) or one export queue. One tenant can still evict cache entries or clog background workers. - Connecting as a
SUPERUSERorBYPASSRLSrole, which always bypasses PostgreSQL RLS even withFORCE ROW LEVEL SECURITY. Running as the table owner also bypasses RLS unlessFORCE ROW LEVEL SECURITYis enabled on the table.
Follow-up questions
- "How do you prevent connection-pool tenant context from leaking between requests in PgBouncer?" With transaction pooling, never use session-level
SET app.tenant_id. Always runSET LOCAL app.tenant_id = ...inside an explicit transaction block so PostgreSQL clears the setting automatically onCOMMITorROLLBACKbefore the connection returns to the pool. - "What if one of the 200 SMBs grows 50x next year and becomes a noisy neighbor too?" Track per-tenant CPU, DB time, and queue depth metrics so you spot growth early. Because the Bridge model uses the same schema and tenant router, you can migrate that growing tenant into its own cell or silo without changing application code.
- "Why does fair-share queuing beat a single FIFO queue for background exports?" In one FIFO queue,
10,000export jobs fromT-bigsit ahead of5small SMB jobs. Per-tenant virtual queues (or deficit round-robin) give every active tenant turns, so SMB jobs finish in seconds even whileT-bigdrains a huge backlog.
What a strong answer covers
Quantifies pool starvation (60/60) and shows how a 20% (12-connection) cap preserves 48 slots (30 spare) for SMBs. Compares Silo, Pool, and Bridge cleanly, and pairs per-tenant quotas and separate export queues with tenant-prefixed cache keys and PostgreSQL RLS.
Open in interactive practice deck →