multi-tenancy
Skill0 starsChanged 32 days agoMITView on GitHub
Designing and implementing multi-tenant data isolation: chooses between row-level, schema-per-tenant, and database-per-tenant strategies, propagates tenant context through the request lifecycle, enforces tenant scoping at the repository layer so business logic never touches another tenant's data, applies Row Level Security in PostgreSQL, and audits for tenant leakage bugs. Use when you want to add multi-tenancy, isolate tenant data, implement row-level security, propagate tenant context, prevent cross-tenant data leaks, add a tenant_id to queries, build a SaaS application, or audit for tenant isolation bugs.
5 other projects use a version of this.
Adopted by
Most recently taken up by
- MartinOlivero/saas-buildertheir own version, first seen Oct 13, 2026
- psubramanian/Claude-Skillstheir own version, first seen Oct 13, 2026
- rrezartprebreza/spring-boot-skillstheir own version, first seen Aug 7, 2026
- Iteam-company/claude-code-boilerplatestheir own version, first seen May 19, 2026
- YepAPI/skillstheir own version, first seen Apr 12, 2026
Other versions5 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 5
- rrezartprebreza/spring-boot-skills★ 344same name
- YepAPI/skills★ 8same name
- MartinOlivero/saas-builder★ 4same name
- Iteam-company/claude-code-boilerplates★ 2same name
- psubramanian/Claude-Skills★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository68 other files it gives its agents.
- AGENTS.mdAGENTS.md
- .claude/CLAUDE.mdCLAUDE.md
- accessibilitySkill
- api-designSkill
- api-versioningSkill
- authSkill
- background-jobsSkill
- cachingSkill
- capacity-planningSkill
- changelogSkill
- chaos-engineeringSkill
- ci-cdSkill
- circuit-breakerSkill
- claude-best-practicesSkill
- code-commentsSkill
- code-hygieneSkill
- code-reviewSkill
- code-structureSkill
- commit-styleSkill
- concurrency-and-lockingSkill
- database-transactionsSkill
- data-consistencySkill
- debug-infra-startupSkill
- debug-sessionSkill
- demo-video-creatorSkill
- demo-video-script-creatorSkill
- dependency-injectionSkill
- dependency-updateSkill
- disk-cleanupSkill
- distributed-tracingSkill
- dockerSkill
- env-configSkill
- env-setupSkill
- error-handlingSkill
- event-driven-architectureSkill
- feature-flagsSkill
- file-storageSkill
- graceful-shutdownSkill
- idempotencySkill
- input-validationSkill
- jars-engineering-blogsmithSkill
- jars-workshop-builderSkill
- licensingSkill
- loggingSkill
- memory-and-allocationsSkill
- migrate-dbSkill
- monitoringSkill
- notificationsSkill
- openapiSkill
- paginationSkill
- perf-profileSkill
- pr-descriptionSkill
- query-optimizationSkill
- rate-limitingSkill
- refactorSkill
- releaseSkill
- runbook-writerSkill
- searchSkill
- secrets-auditSkill
- simplifySkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
3 rules. 0 are common, 1 is distinctive, and 2 are very specific to this project.
0
Common
Most of the 5 edited versions we read keep these.
1
Distinctive
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 31
Every request carries a tenant.
- Distinctive: line 254
Every tenant-scoped table needs:
- Very specific: line 316
Never accept tenant_id from the request body or query params.
“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
multi-tenancy
Pick the isolation strategy first. Enforce it at the data layer — never rely on application code alone to keep tenants separate.
1. Choose the isolation strategy
| Strategy | Isolation | Cost | Use when |
Row-level (tenant_id column) | Logical | Low | Most SaaS apps, shared schema acceptable |
| Schema-per-tenant | Medium | Medium | Regulated data, easy per-tenant migrations |
| Database-per-tenant | Physical | High | Enterprise customers, strict data residency |
Row-level is the right default for most SaaS products. Schema-per-tenant adds operational complexity without meaningful security improvement for most threat models. Database-per-tenant is only justified when contractual or regulatory requirements demand physical isolation.
This skill covers row-level isolation in depth — it is the most common and the most likely to have subtle bugs.
2. Tenant context — propagate through the request lifecycle
Every request carries a tenant. Resolve it once at the boundary (middleware), store it
in a context variable, and read it everywhere else. Never pass tenant_id as a function
argument through every layer — that pattern is fragile and easily forgotten.
# lib/tenant_context.py
"""
lib/tenant_context.py
----------------------
Thread/task-local tenant context. Set once per request in middleware.
All repositories read from here — never from function arguments.
Depends on: nothing (stdlib only)
Used by: middleware/tenant_middleware.py, repositories/*
"""
from contextvars import ContextVar
from lib.logger import get_logger
logger = get_logger(__name__)
_tenant_id: ContextVar[str | None] = ContextVar("tenant_id", default=None)
def set_tenant(tenant_id: str) -> None:
_tenant_id.set(tenant_id)
def get_tenant() -> str:
tid = _tenant_id.get()
if not tid:
raise RuntimeError(
"Tenant context not set. Ensure TenantMiddleware runs before this code."
)
return tid
def get_tenant_or_none() -> str | None:
return _tenant_id.get()# middleware/tenant_middleware.py
"""
middleware/tenant_middleware.py
--------------------------------
Resolves tenant from JWT sub-claim or X-Tenant-ID header.
Must run before any route handler or repository call.
Depends on: lib/tenant_context.py, lib/auth.py
Used by: main.py (registered as FastAPI middleware)
"""
from fastapi import Request, HTTPException
from starlette.middleware.base import BaseHTTPMiddleware
from lib.tenant_context import set_tenant
from lib.auth import decode_jwt
from lib.logger import get_logger
logger = get_logger(__name__)
SKIP_PATHS = {"/health", "/metrics", "/docs", "/openapi.json"}
class TenantMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
if request.url.path in SKIP_PATHS:
return await call_next(request)
tenant_id = await self._resolve_tenant(request)
if not tenant_id:
raise HTTPException(status_code=401, detail="Tenant could not be resolved")
set_tenant(tenant_id)
logger.info("Tenant resolved. tenant_id=%s path=%s", tenant_id, request.url.path)
return await call_next(request)
async def _resolve_tenant(self, request: Request) -> str | None:
# Primary: resolve from JWT (preferred — authenticated)
auth_header = request.headers.get("Authorization", "")
if auth_header.startswith("Bearer "):
token = auth_header.removeprefix("Bearer ")
try:
claims = decode_jwt(token)
return claims.get("tenant_id")
except Exception as exc:
logger.warning("JWT decode failed in tenant middleware. error=%s", exc)
return None
# Fallback: explicit header (for service-to-service calls with pre-auth)
return request.headers.get("X-Tenant-ID")3. Repository layer — enforce tenant scoping here, nowhere else
The repository is the only place that touches SQL. It is the correct and final place to
enforce tenant isolation. Business logic must never pass raw tenant_id into queries —
it reads from context and the repository applies it.
# repositories/base_repository.py
"""
repositories/base_repository.py
---------------------------------
Base class for all tenant-scoped repositories.
Subclasses inherit _tenant_id and _scope() automatically.
Depends on: lib/tenant_context.py, lib/db.py
Used by: repositories/*
"""
from lib.tenant_context import get_tenant
from lib.db import db
from lib.logger import get_logger
logger = get_logger(__name__)
class TenantRepository:
"""All queries in subclasses must call self._scope() to add tenant_id filter."""
@property
def _tenant_id(self) -> str:
return get_tenant()
def _scope(self, query: str, params: dict) -> tuple[str, dict]:
"""Append tenant_id filter to any WHERE clause."""
connector = "WHERE" if "WHERE" not in query.upper() else "AND"
scoped_query = f"{query} {connector} tenant_id = :tenant_id"
return scoped_query, {**params, "tenant_id": self._tenant_id}# repositories/order_repository.py
"""
repositories/order_repository.py
----------------------------------
All DB operations for orders — always tenant-scoped.
Depends on: repositories/base_repository.py
Used by: services/order_service.py
"""
from repositories.base_repository import TenantRepository
class OrderRepository(TenantRepository):
async def find_by_id(self, order_id: int) -> dict | None:
query, params = self._scope(
"SELECT * FROM orders WHERE id = :id",
{"id": order_id},
)
# Final query: SELECT * FROM orders WHERE id = :id AND tenant_id = :tenant_id
return await db.fetch_one(query, params)
async def list_recent(self, limit: int = 20) -> list[dict]:
query, params = self._scope(
"SELECT id, total, status, created_at FROM orders ORDER BY created_at DESC LIMIT :limit",
{"limit": limit},
)
rows = await db.fetch_all(query, params)
return [dict(r) for r in rows]
async def create(self, total: int, status: str) -> dict:
# tenant_id always injected from context — never from caller
row = await db.fetch_one(
"INSERT INTO orders (tenant_id, total, status) VALUES (:tenant_id, :total, :status) RETURNING *",
{"tenant_id": self._tenant_id, "total": total, "status": status},
)
return dict(row)4. PostgreSQL Row Level Security — enforce at the database level too
RLS is a second line of defence. Even if application code has a bug, the database refuses to return another tenant's rows.
-- Enable RLS on every tenant-scoped table
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
ALTER TABLE orders FORCE ROW LEVEL SECURITY; -- applies to table owner too
-- Policy: app DB user can only see rows matching the session variable
CREATE POLICY tenant_isolation ON orders
USING (tenant_id = current_setting('app.tenant_id', TRUE));
-- The application sets the variable at the start of each transaction:
-- SET LOCAL app.tenant_id = 'tenant_abc';# lib/db.py — set the RLS variable on every connection checkout
from sqlalchemy import event, text
from sqlalchemy.ext.asyncio import create_async_engine
from lib.tenant_context import get_tenant_or_none
engine = create_async_engine(settings.database_url)
@event.listens_for(engine.sync_engine, "connect")
def set_rls_variable(dbapi_connection, connection_record):
# Called when a connection is first created — set safe default
cursor = dbapi_connection.cursor()
cursor.execute("SET app.tenant_id = ''")
cursor.close()
async def get_scoped_connection():
"""Get a connection with the current tenant's RLS variable set."""
tenant_id = get_tenant_or_none() or ""
async with engine.connect() as conn:
await conn.execute(text(f"SET LOCAL app.tenant_id = '{tenant_id}'"))
yield conn5. Schema and migration requirements
Every tenant-scoped table needs:
-- Every tenant-scoped table must have:
ALTER TABLE orders ADD COLUMN tenant_id TEXT NOT NULL;
-- Composite indexes: tenant_id first, then query columns
-- Without this, row-level filtering does a full table scan per query
CREATE INDEX idx_orders_tenant_status ON orders (tenant_id, status);
CREATE INDEX idx_orders_tenant_created_at ON orders (tenant_id, created_at DESC);
-- Foreign keys reference within the same tenant (use partial unique constraints)
CREATE UNIQUE INDEX idx_orders_tenant_external_id
ON orders (tenant_id, external_id);6. Audit for tenant leakage
These patterns cause cross-tenant data leaks. Search for them actively.
# Find queries that may be missing tenant_id filter
grep -rn "FROM orders" src/ --include="*.py" | grep -v "tenant_id"
grep -rn "db.fetch" src/ --include="*.py" | grep -v "tenant_id"
# Find places that read tenant_id from request body instead of context
grep -rn "tenant_id" src/routes --include="*.py"
# tenant_id should NEVER appear in route handlers — only in repositoriesCommon leakage patterns:
| Pattern | Risk | Fix |
WHERE id = :id without tenant_id | Returns any tenant's row by ID | Always use _scope() |
tenant_id in request body | Caller supplies their own tenant | Read from context only |
| Background job missing tenant context | Processes wrong tenant's data | Set context from job payload |
COUNT(*) without tenant filter | Leaks row counts to other tenants | Scope all aggregate queries |
| Cache key without tenant prefix | Tenant A reads tenant B's cached response | Prefix: {tenant_id}:{key} |
# ✅ Cache key with tenant prefix
from lib.tenant_context import get_tenant
cache_key = f"{get_tenant()}:user:{user_id}"# ✅ Background job — set tenant context from job payload
async def handle_invoice_job(job_data: dict) -> None:
from lib.tenant_context import set_tenant
set_tenant(job_data["tenant_id"]) # must be first line
# now all repository calls are scoped to the correct tenant
await invoice_service.generate(job_data["order_id"])Rules
- Enforce tenant scoping at the repository layer. Business logic must never add
WHERE tenant_id = xmanually — it must come from the base repository automatically. - Never accept
tenant_idfrom the request body or query params. Resolve it from the authenticated JWT or session only. - Prefix every cache key with
tenant_id. A cache miss is far better than a cross-tenant data leak. - Set tenant context as the first line of every background job handler.
- Enable PostgreSQL RLS as a second line of defence — it catches application-layer bugs that would otherwise silently leak data.
- Index
(tenant_id, <query_column>)on every scoped table. Without it, every query scans the entire table and filters — performance collapses as tenant count grows.
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.

