Multi-tenancy patterns
Serve many customers from one system by choosing, per layer, silo (dedicated), pool (shared with a tenant_id) or a bridge of both, trading cost per tenant against isolation, noisy neighbors and compliance.
Analogy: An apartment building: pool tenants share plumbing and elevators, a silo tenant gets a detached house with its own meter, and a bridge is the penthouse with a private elevator in a shared building.
How it works
- Silo gives the strongest isolation, easy residency and per-tenant keys, but cost and ops grow linearly with tenants.
- Pool has the lowest cost per tenant, but isolation must live below app code, e.g. row-level security.
- The pool's signature risk is the noisy neighbor; contain it with per-tenant quotas, fair queuing and moving whales out.
- A tenant catalog maps tenant → tier, database, index, region; moving a tenant becomes a data copy plus one update.
- Meter cost per tenant (tokens, GPU-seconds, storage), or a heavy flat-plan customer quietly costs more than it pays.
Check yourself
Answer to mark it learned; your answers aren't scored.
Your pooled SaaS enables Postgres row-level security on every table, filtering on tenant_id. A pen test still reads other tenants' rows through the app, which connects as the role that owns the tables. Most likely cause?
Go deeper
Full explanation
Multi-tenancy means one system serves many customers (tenants) while each sees only its own data and gets predictable performance. AWS's SaaS guidance names three models, chosen per layer (compute, database, index, queue):
- Silo: dedicated resources per tenant, up to a separate cloud account. Strongest isolation, easy residency and per-tenant keys; cost and ops grow linearly with tenants.
- Pool: shared resources, every row and request carries a
tenant_id. Lowest cost per tenant, one fleet to upgrade; isolation must be enforced below app code, e.g. Postgres row-level security (Multi-tenant isolation in AI apps). - Bridge: a mix, e.g. pooled app tier with a schema per tenant, or pool for free/pro tiers and silo for enterprise.
The pool's signature problem is the noisy neighbor: one tenant's bulk import saturates shared CPU, DB connections or GPUs. Contain it with per-tenant Rate limiting and quotas, fair queuing, and moving whales to their own shard or silo.
Two habits make it work:
- A tenant catalog: one lookup mapping tenant → tier, database, index, region. Moving a tenant to a silo becomes a data copy plus one catalog update.
- Cost per tenant: meter tokens, GPU-seconds and storage by
tenant_id, or a heavy flat-plan customer quietly costs more than it pays.
AI angle: per-tenant vector namespaces, plus token quotas and model tiers enforced on the LLM gateway.
Code Tenant catalog plus a per-tenant token budget
import time
from dataclasses import dataclass
@dataclass
class Tenant:
id: str
tier: str # "pro" (pool) | "enterprise" (silo)
db_dsn: str # shared pool DSN, or a dedicated one
namespace: str # vector index namespace
tokens_per_min: int # LLM quota, set by tier
# The one place that knows where each tenant lives: moving a tenant = one row.
CATALOG = {
"acme": Tenant("acme", "pro", POOL_DSN, "ns-acme", 200_000),
"bigbank": Tenant("bigbank", "enterprise", BIGBANK_DSN, "ns-bigbank", 2_000_000),
}
_buckets: dict[str, tuple[float, float]] = {} # tenant -> (tokens left, last refill)
def take(t: Tenant, cost: int) -> bool:
"""Token bucket per tenant: one tenant's batch job can't starve the rest."""
left, last = _buckets.get(t.id, (t.tokens_per_min, time.monotonic()))
now = time.monotonic()
left = min(t.tokens_per_min, left + (now - last) * t.tokens_per_min / 60)
ok = left >= cost
_buckets[t.id] = (left - cost if ok else left, now)
return ok
def handle(tenant_id: str, prompt: str) -> str: # tenant_id from the verified JWT
t = CATALOG[tenant_id]
if not take(t, estimate_tokens(prompt)):
raise TooManyRequests(retry_after=10) # 429 for this tenant only
docs = vector_search(prompt, namespace=t.namespace)
return call_llm(prompt, docs, db=t.db_dsn, tier=t.tier)Real example
A document-AI SaaS runs 2,000 tenants on a pooled Postgres with RLS and one vector index with a namespace per tenant. A bank signs a large contract that requires EU residency and its own encryption key. Instead of re-architecting, they adopt a bridge: the bank gets a silo stack in an EU region from the same Terraform and images, and the catalog points it there. Meanwhile, one free-tier tenant's bulk ingest was eating ~60% of embedding throughput; a per-tenant tokens-per-minute quota restored p95 for everyone else.
Trade-offs +3 · −3
+ You gain
- Pool: lowest cost per tenant and one fleet to deploy, migrate and monitor.
- Silo: strongest isolation, plus easy per-tenant residency, keys, backups and deletion.
- Bridge maps pricing tiers to isolation tiers, so enterprises pay for their silo.
− You pay
- Pool: noisy neighbors by default, and one enforcement gap is a cross-tenant leak.
- Silo: cost and toil grow with every tenant; each migration runs N times.
- Bridge: two operating models to test, deploy and support.
Use it when
Any SaaS or internal platform serving several customers or business units. Default to pool with database-enforced isolation; add silos for tenants whose contracts, regulation or load justify the cost.
Avoid it when
Don't pool a customer whose contract requires dedicated keys or residency just to save cost, and don't silo hundreds of small tenants: fixed per-stack cost eats the margin.
Numbers to know 3
- Postgres RLS is skipped for superusers and
BYPASSRLSroles, and for the table owner unlessFORCE ROW LEVEL SECURITYis set. - HTTP 429 Too Many Requests (RFC 6585) with
Retry-Afteris the standard reply to a tenant over its quota. - Postgres logical replication supports row filters since v15: copy one tenant's rows out of the pool with
WHERE tenant_id = ....
Common pitfalls 4
- No per-tenant quotas or fair queuing, so one tenant's batch job becomes everyone's outage.
- Tenant placement hardcoded across services instead of a catalog, so moving one tenant to a silo is a rewrite.
- A silo per tenant for hundreds of small customers: N migrations, N dashboards, N on-call surfaces.
- Not metering usage per tenant, so unprofitable customers stay invisible until the cloud bill arrives.
Interview questions 3
Answer out loud first, then reveal.
Silo or pool for a new B2B AI SaaS?Show answer
Pool by default, with isolation enforced by the platform rather than developer discipline: tenant_id from the verified token, Postgres row-level security, a vector namespace per tenant, and tenant-scoped cache keys. That gives me the lowest cost per tenant and one fleet to upgrade. But I'd keep a tenant catalog and deploy everything from the same templates, so the stack can also run as a silo, because the first enterprise deal will ask for residency or dedicated keys, and I want to sell that as a tier, not re-architect for it.
How do you handle a noisy neighbor?Show answer
First I make it visible: usage and latency per tenant, so I know who consumes what. Then I contain it in layers: per-tenant rate limits and token quotas at the gateway, concurrency caps and fair queuing in workers so one tenant's 10k-job batch can't starve the queue, and separate pools for batch and interactive traffic. If a tenant is structurally bigger than the rest, I move it to its own shard or silo and price that accordingly. Quotas are a product decision too, so I tie them to tiers.
A pooled enterprise customer now requires a dedicated database. How do you move them?Show answer
If placement lives in a tenant catalog, it's a data migration, not a re-architecture. I stand up a silo from the same schema and images, copy that tenant's rows with logical replication filtered on tenant_id, and keep it in sync while I verify counts and checksums. Then I pause that tenant's writes for seconds, let replication catch up, flip its catalog entry and resume. Only that tenant sees the blip. Last, I delete its rows from the pool and confirm nothing still reads the old location.