Architecture
5 min read
November 22, 2026
Multi-tenant architecture: how to build multi-tenant applications
Shared tables, schema per tenant or database per tenant? A comparison of multi-tenant architecture models, row-level security in PostgreSQL, and the places tenant isolation usually leaks.
By Parth Patel
A multi-tenant application serves many customers (tenants) from one system while keeping each customer's data, configuration and experience separate. Almost every B2B SaaS product is multi-tenant, and the way tenancy is built decides how safe, how cheap and how easy to change the product will be for years.
This guide compares the main multi-tenant architecture models, shows how to enforce isolation in practice, and covers the details teams usually discover too late: background jobs, caching, files, noisy neighbours and migrations.
The three isolation models
Shared database, shared tables
Every tenant's rows live in the same tables, separated by a tenant_id column.
- Pros: cheapest to run, simplest to operate, one migration for everyone, easy cross-tenant analytics for you as the operator.
- Cons: isolation depends on every query filtering correctly; one large tenant can affect others.
Schema per tenant
Each tenant gets its own schema (or set of tables) in a shared database.
- Pros: stronger separation, per-tenant backup and restore is easier.
- Cons: migrations run once per tenant; thousands of schemas strain connection pools and tooling.
Database per tenant
Each tenant gets a dedicated database, sometimes a dedicated server.
- Pros: the strongest isolation, per-tenant performance tuning, data residency options, simple deletion.
- Cons: the highest cost and operational effort; every change is a fleet operation.
Most products should mix models
The common, practical answer is shared tables by default, dedicated databases for the few customers who need them (and pay for them). Build the application so it resolves a tenant to a connection, not to a hard-coded database:
type TenantRoute = { tenantId: string; database: "shared" | `dedicated:${string}` };
async function dbFor(route: TenantRoute) {
return route.database === "shared" ? sharedPool : await dedicatedPool(route.database);
}Then moving a tenant from shared to dedicated is a data migration and a routing change, not a rewrite.
Enforce isolation in one place
The most dangerous multi-tenant bug is one query that forgets WHERE tenant_id = .... Relying on every developer to remember is not a strategy. Enforce it centrally:
Row-level security in PostgreSQL. The database applies the tenant filter to every query:
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
ALTER TABLE invoices FORCE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON invoices
USING (tenant_id = current_setting('app.tenant_id', true)::uuid)
WITH CHECK (tenant_id = current_setting('app.tenant_id', true)::uuid);At the start of each request (or transaction), the app runs SET LOCAL app.tenant_id = '...' with the tenant resolved from the authenticated session. The application role must not be the table owner or a superuser, or the policy is bypassed.
A tenant-scoped data layer in the application, where repositories take the tenant from request context and there is no API to query without one, is a good second layer.
Resolve the tenant once, early
Decide how a request identifies its tenant: a subdomain (acme.app.com), a path segment, a custom domain, or the user's selected organization. Resolve it once in middleware, validate that the authenticated user is a member of that tenant, and put it in request context. Everything downstream reads it from there.
Never accept a tenantId from the request body or query string as the source of truth.
The places tenancy leaks
Isolation in the main database is not enough. Tenancy must follow the data everywhere it goes:
- Background jobs. Every job payload carries the tenant ID, and the worker sets the same context before touching data.
- Caches. Every cache key includes the tenant:
tenant:42:dashboard:summary, neverdashboard:summary. - File storage. Prefix object keys by tenant and serve files through short-lived signed URLs checked against the tenant.
- Search indexes and vector stores. Filter by tenant on every query, or keep separate indexes for large tenants.
- Logs and analytics. Include the tenant ID so you can answer "what happened for this customer".
Noisy neighbours
In a shared system, one tenant's heavy import or report can slow everyone down. Protect the others with:
- Per-tenant rate limits on the API.
- Per-tenant concurrency limits on background job queues, so one tenant's 100,000 jobs don't starve the rest.
- Query timeouts and pagination limits.
- Per-tenant metrics, so you can see who is consuming capacity before customers complain.
When a tenant consistently needs more, that is the signal to move them to dedicated resources.
Configuration and customization
Tenants will want different plans, feature flags, branding and settings. Keep this as data, not code: a tenant_settings record and a feature-flag service, checked through one interface. Resist per-tenant code branches; they multiply testing and slow every release.
Migrations and tenant lifecycle
Plan for the full lifecycle:
- Onboarding creates the tenant, its first admin, default settings and any dedicated resources, idempotently.
- Migrations on shared tables must be backward compatible (expand, migrate data, then contract), because old and new code run side by side during deploys.
- Offboarding must delete or export a tenant's data across every store above: database, files, caches, search, backups according to your retention policy. Data protection laws such as India's DPDP Act and the GDPR make this a requirement, not a nice-to-have.
Choosing your model
| If you need... | Choose |
|---|---|
| Many small customers, low cost, fast iteration | Shared tables with row-level security |
| A few enterprise customers with strict isolation or residency | Dedicated databases for those tenants |
| Per-tenant restore and moderate tenant counts | Schema per tenant |
Start simple, enforce isolation centrally, and make the tenant follow the data everywhere. Those three habits prevent almost every serious multi-tenant incident. For the wider architecture around tenancy, see designing a scalable SaaS architecture.