Security & Operations

Multi-tenancy & Isolation

Serve many customers from one platform without noisy-neighbor and data leaks.

Advanced, about 55 minutes to read, 4 open questions to practise.

1Understand

The idea

Multi-tenancy means one platform serves many customers while each believes they are alone: no cross-tenant data leaks, predictable performance, and unit economics that do not provision a dedicated database per free signup. Models span pooled rows with tenant_id, schema-per-tenant bridge, and silo databases for enterprise/regulatory wins.

The interview checks discipline: tenant_id on every SQL query, cache key, queue message, and search filter, not “we trust developers to remember.” Noisy neighbor controls (per-tenant rate limits, fair queues) matter as much as schema diagrams.

Silo everything on day one is ops bankruptcy; pool without tenant keys is a breach waiting for a cache typo. Enterprise exceptions should be data-driven (contract, residency, noisy workload), not habit.

Intuition

Pooled (shared tables). Cheapest; tenant_id column + composite indexes (tenant_id, created_at). Middleware sets TenantContext from JWT claim or subdomain. Postgres RLS optional safety net: policy tenant_id = current_setting('app.tenant'). Leak = missing WHERE clause or wrong cache key.

Bridge (schema per tenant). Same cluster, tenant_acme.products, stronger isolation, migration runs N times; connection pool per schema or search_path switch.

Silo (DB per tenant). Fortune-500 contract, data residency Germany-only, custom schema, registry maps tenant → connection string; backup/restore per customer; cost scales with enterprise count, not MAU.

Tenant resolution. acme.app.com → tenant_id=acme; API key metadata includes tenant; never accept raw tenant_id from client without binding to credential.

Fairness. Gateway rate limit per tenant tier; worker pool concurrency caps; noisy batch export → dedicated queue with max parallel jobs per tenant.

Cross-cutting: search index shared with mandatory tenant_id filter; S3 prefix s3://bucket/{tenantId}/...; logs include tenant (hashed in shared SIEM). Support break-glass impersonation with audit trail.

Onboarding: create tenant row + defaults, not new RDS instance per trial.

Metering & billing. Per-tenant usage counters (API calls, storage bytes) enable invoices and prove fairness when enforcing quotas.

Deletion / offboarding. Tenant export and hard delete across SQL, search, S3 prefix, orchestrated workflow; tombstone tenant_id everywhere.

Upgrade path. Start pooled; promote tenant to silo when contract signed, registry flip without code fork if connection routing abstracted early.

What you will be able to do

  • Choose pooled vs siloed tenancy with a reason
  • Prevent cross-tenant data access in keys and queries
  • Limit noisy neighbors with quotas

When this comes up

  • Tenant id
  • Noisy neighbor
  • Silo vs pool
  • Row-level security

2See it

Architecture sketch

Pooled tenancy with isolation controls

Default shared compute/DB with tenant_id. Silo when contract or noisy neighbor demands.

100%
ClientGatewayServiceStore

Walkthrough

Every query carries tenant

Missing filter is a data leak

1 / 4

100%

Resolve tenant from host or token. Never trust a client-supplied tenant id alone.

Read the walkthrough as text

Worked example, B2B analytics SaaS: 40k SMB tenants + 2 regulated enterprises on silo.

Workload: dashboards, API exports, shared Postgres, Redis cache, ES search.

  1. Default pool: all SMB on one Postgres; every table has tenant_id; index (tenant_id, id) on hot tables.
  2. Request path: auth resolves tenant from subdomain; middleware SET LOCAL app.tenant_id; repository base class appends tenant predicate.
  3. Cache: keys t:{tenantId}:dashboard:summary, never dashboard:summary.
  4. Search: ES query always filter: { term: { tenant_id } } from server context, not client param.
  5. Fairness: free tier 100 rps, pro 2k rps at API gateway; export jobs max 2 concurrent per tenant.
  6. Enterprise silo: tenant registry flag dedicated_db=true → route to bigco-db.internal; same codebase, different datasource bean.
  7. Testing: integration test attempts cross-tenant read with two JWTs, must 403/empty.
  8. Noisy neighbor: one tenant 10TB export, shard to low-priority queue, does not block interactive queries (separate pool).

What interviewers listen for: tenant in every layer; pool vs silo tradeoff; quotas; leak prevention example; enterprise exception justified.

Napkin math

Tenant napkin

default = shared pool + tenant_id on every row

queries always scoped (middleware / RLS)

quotas: QPS and storage per tenant

noisy neighbor → rate limit or silo DB

enterprise: dedicated schema / cluster

Isolation is a product of filters, quotas, and sometimes silos.

3Answer it

How to answer in an interview

When SaaS, B2B, “many customers on one stack”:

  1. Isolation model, pool default; silo when and why.
  2. Tenant context, how resolved and enforced.
  3. Data plane, SQL, cache, search, object keys.
  4. Fairness, rate limits, queue fairness, tiers.
  5. Blast radius, one bad tenant cannot exhaust fleet.
  6. Enterprise path, registry routing without forked codebases.
  7. Testing / RLS, negative cross-tenant cases.

Phrases: “pooled by default,” “tenant-scoped cache keys,” “per-tenant concurrency caps,” “silo for residency not for hobby tier.”

  1. Support access, break-glass impersonation with audit trail, not shared admin password.
  1. Close, “Isolation is a property of every query, not a deployment diagram ornament.”

Answer checklist

1. Isolation model (pool, bridge, silo). 2. Tenant on every query and cache key. 3. Fairness: rate limits / quotas per tenant. 4. Blast radius of a bad tenant. 5. When to silo enterprise customers.

Tradeoffs & numbers

  • Pooled: lowest $/tenant; requires ruthless code review and RLS optional backstop.
  • Schema-per-tenant: middle isolation; migration N-way scripts hurt.
  • DB-per-tenant: strongest; connection limits, backup cost, schema drift across customers.
  • Per-tenant limits: fairness; billing complexity mapping tier → quota.
  • Shared ES cluster: cost-efficient; one missing filter catastrophic, automated query lint.
  • When NOT silo: free tier, <100 tenants, no regulatory clause, pool until contract forces.
  • Custom schema tenants: even in pool model, feature flags per tenant, avoid 40 forked code paths.
  • Interview tip: give a leak scenario (cache key missing tenant) and the fix in the same breath.

Kinds of questions (4)

Same pattern, different interview surfaces. Recognise which variant you are in before you answer.

  • Pooled shared DB

    Example. 10k law firms on one Postgres; each must see only their matters. `tenant_id` on every query/cache key; noisy-neighbor limits; when silo vs pool is a contract-driven exception.

    How. tenant_id on matters table; composite indexes; middleware injects tenant; integration tests for IDOR across firms. Middleware injects tenant; integration test that cross-tenant read fails; fair queue per tenant.

  • Schema- or DB-per-tenant

    Example. Hospital chain mandates isolated schema for HIPAA audit scope. `tenant_id` on every query/cache key; noisy-neighbor limits; when silo vs pool is a contract-driven exception.

    How. Tenant registry selects schema; migrations scripted per schema; backup per tenant for restore SLA. Middleware injects tenant; integration test that cross-tenant read fails; fair queue per tenant.

  • Per-tenant rate limits

    Example. One tenant’s buggy integration loops `GET /events` at 5k rps. `tenant_id` on every query/cache key; noisy-neighbor limits; when silo vs pool is a contract-driven exception.

    How. Gateway token bucket per API key; 429 with Retry-After; internal alert on sustained throttle for capacity planning. Middleware injects tenant; integration test that cross-tenant read fails; fair queue per tenant.

  • Enterprise silo

    Example. Bank contract requires dedicated RDS in `eu-central-1` only for their data. `tenant_id` on every query/cache key; noisy-neighbor limits; when silo vs pool is a contract-driven exception.

    How. Routing datasource from registry; shared app binary; separate backup/DR runbook, not default onboarding path. Middleware injects tenant; integration test that cross-tenant read fails; fair queue per tenant.

Common mistakes

Failure modes the AI coach watches for when you practise open questions.

Forgotten tenant filter

Avoid. SELECT * FROM orders WHERE id=?. Any tenant can read another’s row.

Prefer. Always constrain by tenant_id (or DB RLS). Test cross-tenant access.

Weak sketch

Bad

100%

Stronger sketch

Good

100%
  • Missing tenant key

    Why it happens. Shared caches and connection pools reuse entries across tenants; one wrong key exposes another customer’s payload.

    How it fails. Redis caches `report:weekly` globally; Tenant B sees Tenant A’s revenue chart.

    Fix. Prefix `tenantId` on every cache, queue, and object key; code review checklist; optional RLS.

  • No per-tenant quotas

    Why it happens. A single heavy tenant can exhaust DB connections, API capacity, or worker threads for all others.

    How it fails. Global rate limit 10k rps shared; one enterprise runs batch export and starves SMB UI.

    Fix. Per-tenant gateway limits, fair queue scheduling, tiered concurrency caps on background jobs.

  • Silo everything too early

    Why it happens. Provisioning isolated databases per signup multiplies cost, migration time, and on-call surface without compliance benefit.

    How it fails. Every free trial triggers Terraform for dedicated RDS + ES cluster.

    Fix. Pooled multi-tenant default; silo triggered by contract, residency, or measured noisy-neighbor failure.

4Practise

Concept check

A short quiz on this lesson before open questions. Optional, and it does not gate practice.

Open questions

Ready to practise?

Write an answer, get AI coaching, then revise. You are not building a real system.

Practise with an open question

All systems lessons