Quick revision
The short answer
Shared tables with row-level security are cheapest and scale to thousands of tenants, but one policy bug affects everyone and tenants share resources. Schema per tenant isolates objects but makes migrations and the catalog heavy at thousands of schemas, and database per tenant gives the strongest isolation, separate backups and data residency at the highest cost. For 5,000 small tenants and 20 large regulated ones I would choose a hybrid: pooled tables with RLS for the small ones and dedicated databases for the regulated ones, behind a tenant routing layer, with provisioning, migrations and isolation tests automated for both.
What to remember
In this answer, we compared three tenant isolation models on PostgreSQL. Shared tables with RLS are cheapest and scale to thousands of tenants but share fate and resources, schema per tenant isolates objects but makes the catalog and migrations heavy at 5,000 tenants, and database per tenant gives strong isolation, separate backups and residency at the highest cost. For 5,000 small and 20 large regulated tenants we chose a hybrid with a tenant directory and a routing DataSource, and automated provisioning, migrations and isolation tests.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Shared tables with row-level security are cheapest and scale to thousands of tenants, but one policy bug affects everyone and tenants share resources. Schema per tenant isolates objects but makes migrations and the catalog heavy at thousands of schemas, and database per tenant gives the strongest isolation, separate backups and data residency at the highest cost. For 5,000 small tenants and 20 large regulated ones I would choose a hybrid: pooled tables with RLS for the small ones and dedicated databases for the regulated ones, behind a tenant routing layer, with provisioning, migrations and isolation tests automated for both.
Why this question is asked
How tenants are separated is one of the earliest and most expensive decisions in a SaaS product, because it shapes cost, security reviews, migrations and even which customers we can sign. It is also hard to change later, since moving data between models touches every table. Interviewers ask this at senior level to see whether we can weigh isolation against operational cost with real numbers, rather than picking the model we used last time. In this answer, we will recap the three models, compare what each costs at our scale, design the hybrid with its routing layer, and look at the automation that makes any model workable.
1A Quick Recap of the Three Models
In the shared or pooled model, all tenants live in the same tables and every row has a tenant_id column. Row-level security, a PostgreSQL feature that adds a filter condition to every query automatically, makes sure a session only sees its own tenant's rows. In schema per tenant, each tenant gets its own schema, a namespace with its own copy of every table, inside one database, and the application picks the schema per request with search_path. In database per tenant, each tenant gets a separate database, often on a separate cluster, with its own backups and possibly its own region.
All three are valid. They differ in how strongly tenants are separated, how much each tenant costs, and how much work it is to operate many of them.
| Aspect | Shared tables + RLS | Schema per tenant | Database per tenant |
|---|---|---|---|
| Isolation | logical, one policy for all | objects separated | strong, separate DB |
| Cost per small tenant | lowest | low | high |
| Migrations | run once | run 5,000 times | run per database |
| Catalog size | small | huge (about a million relations) | small per DB |
| Noisy neighbours | shared CPU, I/O, locks | shared | isolated if own cluster |
| Backup, restore, residency | all tenants together | per schema is awkward | per tenant, any region |
2Shared Tables With Row-Level Security
The pooled model is the natural default for many small tenants. Adding a tenant is inserting a row, migrations run once, and 5,000 tenants of a few thousand rows each fit comfortably in one well-indexed database. The cost per tenant is close to zero, which matters when most tenants pay little.
The risks are shared fate. A mistake in the policy or in the code that sets the tenant for a transaction could expose one tenant's data to another, so policies must be tested continuously. A single large tenant running heavy reports competes for CPU, I/O and locks with everyone else, which is the noisy neighbour problem. And restoring one tenant to yesterday's state means extracting its rows from a whole-database backup, which is slow and error-prone.
3Schema per Tenant
Schema per tenant feels like a middle ground: each tenant's data is in separate tables, so a missing WHERE clause cannot leak across tenants, and dropping a tenant is DROP SCHEMA. At tens or hundreds of tenants it works well. At 5,000 tenants with 200 tables each, the database holds about a million tables plus their indexes in its system catalog. Planning slows, tools like pg_dump take a long time, autovacuum has a huge list of relations to cycle through, and every migration runs 5,000 times, so a deploy that fails halfway leaves tenants on different schema versions.
So for our 5,000 small tenants this model has most of the operational cost of separation without the strong isolation, because they still share one database and its resources.
4Database per Tenant
A separate database, and especially a separate cluster, gives what regulated customers ask for: their data is physically apart, they can have their own backup schedule and retention, restores affect only them, the database can live in the region their regulator requires, and keys can be per tenant. Noisy neighbours disappear if they have their own cluster.
The cost is real. Each database needs connections, and with a connection pool per database in every application instance, 5,000 databases would need tens of thousands of connections. Each needs monitoring, upgrades and backup verification. For 20 large regulated tenants who pay enough to cover it, this is exactly right. For 5,000 small ones it would be ruinous.
5The Hybrid Design and Its Routing Layer
So I would choose a hybrid: the 5,000 small tenants share pooled tables with RLS, possibly spread over a few pooled clusters as they grow, and the 20 regulated tenants each get a dedicated database, on a dedicated cluster where their contract requires it. The application does not need to know which is which. A tenant directory, a small table or service, maps each tenant to its target, and a routing layer picks the right DataSource per request.
- Authenticated requesttenant id from the user's token
- Tenant directory lookupcached map: tenant -> pooled or dedicated target
- Routing DataSourcepicks the connection pool for the target
- Pooled clustershared tables, RLS with
app.tenant_id - Dedicated clustersone per regulated tenant, own backups and region
5 layers, from the top down. Step through them one at a time.
The application code and schema are the same for every target.
Notice that the same schema and the same code run on both kinds of target, which keeps the product single. It also gives us an upgrade path: when a small tenant grows or signs a regulated contract, we copy its rows from the pool into a new dedicated database, switch its directory entry, and delete the pooled rows. The connection cost stays manageable because only 20 or so dedicated pools exist per application instance, or we put a pooler such as PgBouncer in front of them.
6Automation Makes Any Model Safe
Whatever we choose, the people cost comes from doing things by hand. Provisioning a tenant must be one automated workflow: create the directory entry, and for dedicated tenants create the cluster, roles, keys and backups from templates. Migrations run through a pipeline that applies them to every target, records each target's schema version, and stops the rollout on the first failure so no target is left half-migrated. And isolation tests run continuously: synthetic tenants in every pool try to read each other's data through the real API and directly through SQL as the application role, and any success pages the team.
7Follow-up questions
How do you move a tenant from the pool to a dedicated database without downtime?
Copy its rows with logical replication or a change-data-capture stream filtered by tenant_id into the new database, let it catch up, briefly block writes for that tenant, verify counts and checksums, switch the directory entry, and then remove the pooled rows. Because routing is per tenant, nobody else notices.
Would you ever choose schema per tenant?
Yes, for a product with tens or a few hundred tenants who need some separation, such as custom extensions or per-tenant restores, where the migration fan-out is manageable. It is the scaling to thousands that makes it painful.
How do you handle a noisy neighbour in the pool before moving them?
Measure per-tenant load with query statistics tagged by tenant, apply per-tenant rate limits or queue heavy jobs in the application, move reporting for big tenants to replicas, and spread tenants across several pooled clusters so one heavy tenant cannot affect all 5,000.
8Conclusion
In this answer, we compared three tenant isolation models on PostgreSQL. Shared tables with RLS are cheapest and scale to thousands of tenants but share fate and resources, schema per tenant isolates objects but makes the catalog and migrations heavy at 5,000 tenants, and database per tenant gives strong isolation, separate backups and residency at the highest cost. For 5,000 small and 20 large regulated tenants we chose a hybrid with a tenant directory and a routing DataSource, and automated provisioning, migrations and isolation tests.
Open with
I would compare the three on isolation, cost per tenant and operational load. Shared tables with row-level security are the cheapest: a new tenant is a row, migrations run once, and thousands of small tenants fit in one database.
Then, if they want more
The downside is shared fate, a bug in the policy or in setting the tenant could expose everyone, noisy neighbours share CPU and locks, and per-tenant restores are hard.
Schema per tenant separates objects, but at 5,000 tenants with a couple of hundred tables each you have about a million relations in the catalog, slow tooling and 5,000 migration runs per deploy, while tenants still share one database's resources.
Database per tenant gives the strongest isolation, separate backups and restores, data residency and per-tenant keys, at a high cost in connections and operations.
For this mix I would go hybrid: the 5,000 small tenants in pooled tables with RLS, spread over a few clusters as needed, and the 20 regulated ones each in a dedicated database, with a tenant directory and a routing DataSource choosing the target per request. Same schema and code everywhere, and a tenant can graduate from the pool.
Whatever the model, I would automate tenant provisioning, migrations across all targets with version tracking, and continuous isolation tests.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare tenant isolation approaches for a SaaS product on PostgreSQL: shared tables with...": https://scrivio-interview-library.netlify.app/databases/database-security/3fccae1626/