design-multi-tenancy
Skill9 starsChanged 5 months agoApache-2.0View on GitHub
Architects a SaaS so many customer orgs share infrastructure without leaking into each other — picking an isolation model (shared schema + Postgres RLS, schema-per-tenant, or database-per-tenant) against an explicit cost/blast-radius/ops tradeoff table, resolving and propagating tenant context from request to DB session, and enforcing isolation in depth (app-layer query scoping PLUS RLS as the safety net) so a single forgotten tenant filter can't cross-leak. Also covers per-tenant quotas/noisy-neighbor mitigation, fan-out migrations across thousands of tenants, tenant offboarding (export + hard delete), optional per-tenant keys, and safe cross-tenant admin features.
- Deletes or force-pushes
In this repository147 other files it gives its agents.
- second-brain/AGENTS.mdAGENTS.md
- second-brain/CLAUDE.mdCLAUDE.md
- agent-tool-mcp-builderSkill
- api-design-reviewSkill
- async-concurrency-correctnessSkill
- audit-accessibility-wcagSkill
- audit-license-complianceSkill
- audit-technical-seoSkill
- auth-jwt-sessionSkill
- author-codemodSkill
- brainstorm-designSkill
- build-audit-loggingSkill
- build-cdc-streaming-pipelineSkill
- build-cli-toolSkill
- build-data-tableSkill
- build-etl-pipelineSkill
- build-form-validationSkill
- build-native-mobile-uiSkill
- build-office-docsSkill
- build-offline-first-syncSkill
- build-react-componentSkill
- build-realtime-channelSkill
- build-spreadsheetSkill
- build-vector-searchSkill
- caching-strategySkill
- cicd-pipeline-authorSkill
- cloud-cost-optimizeSkill
- code-commentsSkill
- code-reviewSkill
- compose-local-dev-stackSkill
- configure-bundler-buildSkill
- configure-dns-tlsSkill
- configure-reverse-proxy-lbSkill
- configure-security-headers-cspSkill
- contract-testingSkill
- datetime-timezone-correctnessSkill
- db-migration-safetySkill
- debug-ci-pipeline-failureSkill
- debug-flaky-testsSkill
- debug-frontend-browserSkill
- debug-root-causeSkill
- defend-llm-prompt-injectionSkill
- deliver-webhooksSkill
- dependency-upgradeSkill
- deploy-releaseSkill
- design-api-paginationSkill
- design-authorization-modelSkill
- design-backup-dr-recoverySkill
- design-event-sourcing-cqrsSkill
- design-protobuf-grpc-serviceSkill
- design-relational-schemaSkill
- design-search-index-infraSkill
- design-state-machineSkill
- design-token-systemSkill
- diff-table-paritySkill
- distributed-locks-leasesSkill
- dockerfile-optimizeSkill
- encrypt-sensitive-dataSkill
- error-messageSkill
- estimate-workSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
4 rules. About 0 are common, 1 is distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 90
Caching: every cache key includes tenant_id (doc:{tenant_id}:{id}) so tenant A can never read tenant B's cached value, and invalidation can …
- Very specific: line 95
RLS backstop: run a query that *omits* the app-layer filter against a session with app.tenant_id = A → still returns only A's rows.
- Very specific: line 97
Pool leak test: run interleaved requests for A then B over a transaction-mode pool and assert B never sees A's SET LOCAL value.
- Distinctive: line 98
Optional per-tenant keys: if tenants have their own encryption keys (envelope encryption, key per tenant in a KMS), test that the wrong key …
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
When to Use
Reach for this skill when the question is "how do I keep tenant A's data away from tenant B while they share the same stack?" — the isolation architecture, not the per-user permissions inside one org:
- "We're going multi-tenant — shared tables with a
tenant_id, a schema per customer, or a DB per customer?" - "How do I make sure one missing
WHERE tenant_idcan't leak another org's data?" - "Resolve the tenant from the subdomain /
X-Tenantheader / JWT org claim and scope every query to it" - "We have 4,000 tenants — how do I run a schema migration across all of them safely?"
- "Enterprise customer wants their data in a separate database / their own encryption key"
- "One big tenant is hammering the DB and starving everyone else (noisy neighbor)"
- "Build admin impersonation / global analytics without accidentally bypassing isolation"
NOT this skill:
- Designing the tables/keys/normalization themselves (PKs, 1:N, constraints) → design-relational-schema (this skill adds the
tenant_id+ RLS layer on top of that model) - Roles/permissions for users within a single tenant (admin vs viewer, per-resource sharing) → design-authorization-model (authZ within a tenant ≠ isolating tenants from each other)
- DSAR export format, consent capture, lawful basis, erasure-across-backups policy → map-privacy-data-gdpr (referenced in step 7 for the offboarding mechanics)
- Capping request rate/volume per caller mechanics (token bucket, 429, Redis counters) → rate-limiting (referenced in step 6 for per-tenant quotas)
- Running one risky
ALTERon one large live table safely (locks, backfill) → db-migration-safety (referenced in step 8 for the fan-out) - Cache patterns/TTLs/stampede in general → caching-strategy (referenced in step 9 for tenant-keyed caches)
Steps
Pick the isolation model from a tradeoff table — default shared+RLS, escalate per tenant only when a reason demands it. The three models are not all-or-nothing; a mature SaaS often runs hybrid pods.
Dimension Shared schema + RLS Schema-per-tenant Database-per-tenant Isolation Logical (one bug from leak) Stronger (namespace) Strongest (physical) Cost / tenant Lowest (one DB, shared) Low–medium Highest (conn pool, idle DB, backups each) Ops / migration burden One migration, all tenants Loop over N schemas Loop over N databases (heaviest) Blast radius All tenants (shared) Per-schema Per-tenant only Noisy neighbor Worst — shared buffers/CPU/locks Some sharing Isolated resources Per-tenant restore / PITR Hard (row-level surgery) Medium Trivial (restore that DB) Tenant count it scales to 100k+ hundreds–low thousands tens–low hundreds Pick: shared schema + RLS by default (cheapest, scales to many small tenants); schema-per-tenant when you want per-tenant restore/customization without N databases' connection overhead; database-per-tenant for enterprise/compliance (HIPAA/SOC2 data-residency), per-tenant encryption/restore, or one tenant so large it deserves its own resources. Hybrid pods: small tenants share a pool, large/enterprise tenants get dedicated DBs — a
tenant → shard/connectionrouting map (a "tenant catalog" table in a control-plane DB) decides at request time. Write the decision in an ADR (see write-adr); migrating models later is a large project.- Add
tenant_idto every tenant-owned table — non-null, indexed, first column of composite indexes. In the shared model, every table carrying tenant data getstenant_id uuid NOT NULL REFERENCES tenant(id). Make it the leading column of relevant indexes and most composite PKs/uniques (UNIQUE (tenant_id, email)notUNIQUE (email)— email is unique per tenant, not globally). Global/system tables (plans, feature flags, thetenantregistry itself) have notenant_id. Never lettenant_idbe nullable or default — a null tenant row is an isolation hole. Resolve tenant context at the edge, from a trusted source — never from a client-supplied body field. Map the inbound request to exactly one tenant:
Source How Note Subdomain acme.app.com→acmeFriendly; needs wildcard DNS/TLS; map slug→tenant_id in catalog X-TenantheaderAPI/service-to-service Trust only if the caller is authenticated; never from a browser unauthenticated JWT org/tenantclaimfrom the verified token Most trustworthy — signed, can't be forged client-side Resolve once at the edge (middleware), validate the tenant is active, and store it in an immutable request context (not a mutable global). The cardinal rule: derive
tenant_idfrom the authenticated identity, never from a request body/query param — a client-suppliedtenant_idis a cross-tenant skeleton key (this is exactly the gap design-authorization-model warns about). If subdomain and token disagree, reject.Defense in depth — app-layer scoping is the primary guard, Postgres RLS is the safety net. The #1 production multi-tenancy bug is a single query that forgot its tenant filter → cross-tenant leak. You need both layers because each fails differently:
- App layer (primary): every query is scoped through a tenant-aware repository / ORM global filter so developers physically can't write an unscoped query. Don't rely on each engineer remembering
WHERE tenant_id = $1— inject it centrally (e.g. an ORM global scope, a base repository that always appends the filter, a query builder that refuses to run without a tenant). - DB layer (backstop): Postgres Row-Level Security catches the day someone bypasses the repository or writes raw SQL.
ALTER TABLE document ENABLE ROW LEVEL SECURITY; ALTER TABLE document FORCE ROW LEVEL SECURITY; -- applies to the table owner too CREATE POLICY tenant_isolation ON document USING (tenant_id = current_setting('app.tenant_id')::uuid) -- read/update/delete visibility WITH CHECK (tenant_id = current_setting('app.tenant_id')::uuid); -- blocks INSERT into another tenantFORCEis non-negotiable (without it the table owner — usually your app's role — bypasses RLS).WITH CHECKstops a write that sets a foreigntenant_id. The app role must not haveBYPASSRLS.- App layer (primary): every query is scoped through a tenant-aware repository / ORM global filter so developers physically can't write an unscoped query. Don't rely on each engineer remembering
- Set the RLS variable with
SET LOCALinside the transaction — the connection-pool caveat that breaks naive RLS. RLS readscurrent_setting('app.tenant_id'). You must set it per request — but how depends on the pooler:- With PgBouncer in transaction mode (the common setup), a connection is handed to a different tenant's request the instant your transaction commits. A session-level
SET app.tenant_id = ...therefore leaks the previous tenant's value into the next request — a catastrophic cross-tenant bug. - Fix: set it transaction-scoped so it auto-resets at commit/rollback:
BEGIN; SET LOCAL app.tenant_id = '...'; -- reset automatically at COMMIT/ROLLBACK; never plain SET -- ... all queries in this request ... COMMIT; - Equivalent:
SELECT set_config('app.tenant_id', $1, true)(thetrue= local). Every tenant request must run inside a transaction that begins withSET LOCAL. Assert in the repository that the var is set before any query runs, so a missing context fails closed (returns zero rows / errors) rather than leaking.
- With PgBouncer in transaction mode (the common setup), a connection is handed to a different tenant's request the instant your transaction commits. A session-level
- Per-tenant quotas + noisy-neighbor mitigation. In a shared model one tenant can starve the rest. Enforce per-tenant rate limits and quotas keyed by
tenant_id(token bucket / sliding window — see rate-limiting), plus resource guards: statement timeouts, max connections per tenant, row/storage caps, background-job concurrency caps per tenant. For chronic offenders or very-large tenants, move them to a dedicated pod/DB (step 1's hybrid). Track per-tenant usage metrics (queries/sec, storage, CPU) so you can detect and isolate a noisy neighbor before it causes an incident. - Tenant offboarding — clean per-tenant export and verifiable hard delete. Deletion and export are isolation-critical and a GDPR obligation (mechanics: map-privacy-data-gdpr):
- Export: dump all rows where
tenant_id = $1across every table to a machine-readable archive. Database-per-tenant makes this apg_dumpof one DB; shared schema requires a tenant-scoped export of every table (drive it from a registry of tenant-owned tables so none is missed). - Hard delete: in shared schema,
DELETEcascades bytenant_id(rely onON DELETE CASCADEfrom thetenantrow, or a deterministic ordered delete) — and don't forget derived data: caches, search indexes, object storage, analytics warehouse, backups' retention policy. Database/schema-per-tenant:DROP DATABASE/DROP SCHEMAis the cleanest, most auditable erasure. Verify deletion (assert zero rows remain for the tenant) and log it for compliance.
- Export: dump all rows where
- Migrations across thousands of tenants — online, batched, versioned, idempotent. Schema changes don't break with one model but they scale differently:
- Shared schema: one migration changes all tenants at once — fast, but a bad migration's blast radius is everyone. Use online/safe DDL (see db-migration-safety: avoid long table locks, backfill in batches, add indexes
CONCURRENTLY). - Schema-per-tenant / DB-per-tenant: loop the migration over every schema/database. This must be batched, resumable, and idempotent — track each tenant's schema version in the catalog, run N at a time, record success/failure per tenant, and be able to retry only the failures. A 4,000-tenant migration that aborts at tenant 2,500 must resume, not restart. Roll out behind a flag and canary on a few tenants first.
- Shared schema: one migration changes all tenants at once — fast, but a bad migration's blast radius is everyone. Use online/safe DDL (see db-migration-safety: avoid long table locks, backfill in batches, add indexes
- Cache and search keyed by tenant; cross-tenant admin features that don't bypass isolation.
- Caching: every cache key includes
tenant_id(doc:{tenant_id}:{id}) so tenant A can never read tenant B's cached value, and invalidation can be per-tenant (see caching-strategy). Same for search indexes (per-tenant index or a mandatory tenant filter on every query). - Admin / impersonation: when support impersonates a tenant, set the same
app.tenant_idand go through the same scoped path — don't add a "god query" that ignores RLS. Use a separate, audited DB role withBYPASSRLSonly for narrow platform operations, and log every impersonation (build-audit-logging). Global analytics (cross-tenant metrics) is the one legitimate cross-tenant read: run it through a dedicated read-only role/replica with explicit tenant aggregation, isolated from the application path — never by relaxing the app's RLS.
- Caching: every cache key includes
Test tenant isolation as a first-class, automated guarantee — the leak test is the one that matters. Ship these as CI tests, not manual checks:
- Cross-tenant denial: seed data for tenant A and tenant B; with context set to A, assert that every read/list/get of B's resources (by real id) returns zero rows / 404 / deny — including raw SQL paths, the cache, and search.
- RLS backstop: run a query that omits the app-layer filter against a session with
app.tenant_id = A→ still returns only A's rows. Proves the data tier holds when the app forgets. - Fuzz the
tenant_id: randomize/swap the tenant in the request context and assert no other tenant's data is ever returned and no write lands in the wrong tenant (WITH CHECKholds). - Pool leak test: run interleaved requests for A then B over a transaction-mode pool and assert B never sees A's
SET LOCALvalue. - Optional per-tenant keys: if tenants have their own encryption keys (envelope encryption, key per tenant in a KMS), test that the wrong key can't decrypt another tenant's data and that key deletion = crypto-shredded data.
Done = an isolation model chosen against the tradeoff table and recorded in an ADR;
tenant_idnon-null + indexed on every tenant table (and leading in uniques); tenant context derived from the verified identity at the edge and propagated as immutable request state; every query scoped at the app layer and RLS (FORCE+WITH CHECK, app role withoutBYPASSRLS) enforced, with the var set viaSET LOCALper transaction; per-tenant quotas in place; migrations fan out batched/resumable/versioned; export + verified hard delete defined; caches/search/admin/analytics tenant-keyed; and an automated cross-tenant leak test (plus fuzz + pool-leak) passing in CI.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

