Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

Compare three multi-tenant database models for a B2B SaaS on PostgreSQL: shared tables with a tenant_id column, schema per tenant, and database per tenant. Which would you choose for 2000 small tenants and 5 very large ones?

Shared tables with tenant_id are the cheapest and simplest to run but need strict filtering and suffer noisy neighbours, schema per tenant adds some isolation and easy per-tenant restore but thousands of schemas slow migrations and catalogs, and database per tenant isolates best but costs the most. For 2,000 small tenants and 5 very large ones I would use a hybrid: small tenants in shared tables keyed by tenant_id, the large ones on dedicated databases, with a routing layer mapping each tenant to its data source.

Why this question is asked

The tenancy model is one of the earliest decisions in a SaaS product and one of the hardest to change. It decides how much each customer costs to host, how long a schema migration takes, how one customer's heavy load affects others, and what we can promise in a security review. Interviewers ask it with a mix of many small and a few huge tenants because no single model fits both. In this answer, we will look at each model with its costs, design the shared tables properly, design the hybrid with routing, and explain how to move a tenant between pools.

1Model 1: Shared Tables With a tenant_id Column

Every tenant's rows live in the same tables, and each row carries a tenant_id. This is the cheapest model: one schema, one migration run, one connection pool, and small tenants cost almost nothing to add. Onboarding a new customer is inserting a row.

The costs are the risk and the neighbours. Every query must filter by tenant, so we enforce it with row-level security and a data access layer that adds the filter. And all tenants share the same resources, so one tenant's large import or expensive report slows everyone, the noisy neighbour problem. Per-tenant backup and restore is also hard, because restoring one tenant means extracting its rows from a full backup.

2Model 2: Schema Per Tenant

Each tenant gets its own PostgreSQL schema, a namespace with its own copy of every table, inside one database. Queries set the search_path to the tenant's schema, so code does not filter by tenant at all. Restoring or exporting one tenant is easy, and a bug in a WHERE clause cannot cross tenants.

The trouble starts at scale. 2,000 schemas with 200 tables each means 400,000 tables plus their indexes in the system catalog. Every migration must run 2,000 times, which can take hours and fail halfway. Catalog lookups and tools like pg_dump slow down, and connection pools either hold a pool per schema or keep switching search_path. It works well for tens or a few hundred tenants, and badly for thousands.

3Model 3: Database Per Tenant

Each tenant gets its own database, often on its own instance. This gives the strongest isolation: separate resources, separate backups, separate encryption keys, and the ability to tune, upgrade or place the tenant in a chosen region. For a regulated or very large customer, that is often a contract requirement.

It is also the most expensive. Each database has a minimum cost, needs monitoring, patching and backups, and every migration runs across the whole fleet. For 2,000 small tenants that overhead would dwarf their revenue.

Comparing the three models
ConcernShared tablesSchema per tenantDatabase per tenant
Cost per small tenantlowestlowhighest
Isolationlogical, RLSnamespacephysical
Noisy neighboursyesyes, same instanceno
Migrationsonceonce per schemaonce per database
Per-tenant restorehardeasyeasy
Comfortable tenant count100,000+hundredstens to hundreds

4Designing the Shared Tables Well

If most tenants live in shared tables, the design must make the tenant the first thing every access path uses. tenant_id is part of every primary key and every unique constraint, and it leads every index. The reason is that almost every query filters by tenant first; with tenant_id leading, the index keeps each tenant's rows together, and a query for one tenant reads only that tenant's part of the index.

SQL
CREATE TABLE invoices (
tenant_id uuid NOT NULL,
id bigint NOT NULL,
number text NOT NULL,
created_at timestamptz NOT NULL,
PRIMARY KEY (tenant_id, id),
UNIQUE (tenant_id, number) -- invoice numbers are unique per tenant
);
CREATE INDEX invoices_recent ON invoices (tenant_id, created_at DESC);
-- foreign keys include the tenant, so a row can never point to another tenant's row
ALTER TABLE invoice_lines ADD FOREIGN KEY (tenant_id, invoice_id) REFERENCES invoices (tenant_id, id);

Notice the composite foreign key. With only invoice_id, a bug could attach a line to another tenant's invoice; including tenant_id makes that impossible. If the tables grow very large, we can later hash-partition them by tenant_id, and every index still begins with the column that the queries use.

5The Hybrid for 2,000 Small and 5 Large Tenants

For our mix, the hybrid is the natural answer. The 2,000 small tenants share one pooled database, or a few if needed, which keeps their cost low and migrations simple. The 5 very large tenants each get a dedicated database with the same schema, so their load cannot hurt the pool and they can be tuned and scaled on their own, and offered per-tenant backup, region or encryption.

A routing layer makes this transparent to the code. A small tenant directory maps each tenant to its data source. In Spring we can use AbstractRoutingDataSource, which picks the target DataSource per call from a lookup key, here the current tenant's pool id. Migrations run once on the shared database and once on each dedicated one, so 6 runs instead of 2,005.

Java
public class TenantRoutingDataSource extends AbstractRoutingDataSource {
private final TenantDirectory directory; // tenant id -> "shared-1" or "dedicated-acme"
TenantRoutingDataSource(TenantDirectory d) { this.directory = d; }
@Override protected Object determineCurrentLookupKey() {
return directory.poolFor(TenantContext.require()); // "dedicated-acme" for a large tenant
}
}
Step throughHybrid tenancy
  1. Application servicesknow only the current tenant
  2. Tenant directory and routing DataSourcetenant -> pool
  3. Shared database2,000 small tenants, tenant_id + RLS
  4. Dedicated databases5 large tenants, same schema
  5. Migration runnerapplies each change to all 6 targets

5 layers, from the top down. Step through them one at a time.

The lookup key must be decided before the transaction borrows its connection, which happens naturally because the tenant is known from the request before any service call. We keep the same schema everywhere, including tenant_id in the dedicated databases, so moving a tenant never needs a different code path.

6Follow-up questions

How would you move a tenant from the shared pool to a dedicated database?

Because the schema is the same everywhere, moving is a data copy plus a routing change. We create the dedicated database with the current schema, copy the tenant's rows, for example with logical replication using row filters on tenant_id (available in PostgreSQL 15 and later, check your version) or a batched copy job, and let it catch up with ongoing changes. Then we pause writes for that tenant for a short window, verify row counts and checksums per table, switch its entry in the tenant directory, and resume. The old rows stay in the shared database for a few days as a rollback path, and are deleted after the move is confirmed.

Do small tenants need RLS if the code always filters?

Yes, as defence in depth. Code filters are written by many people over many years, and one missed filter is a data breach. RLS makes the database refuse to return other tenants' rows even when the code forgets.

When would schema per tenant be the right choice?

For a product with tens or a few hundred tenants that each need easy export, restore, or slight customisation, and where the team can tolerate migrations running per schema.

7Conclusion

In this answer, we saw that shared tables are cheapest and simplest but need strict tenant filtering and suffer noisy neighbours, schema per tenant adds isolation and easy restore but scales poorly to thousands of schemas, and database per tenant isolates best at the highest cost. For 2,000 small tenants and 5 large ones, a hybrid fits: shared tables where tenant_id leads every key and index, dedicated databases for the large tenants, and a routing layer that maps each tenant to its data source.

What to say in the interviewAbout 80 seconds aloud

Open with

Shared tables with a tenant_id column are the cheapest and easiest to operate: one schema, one migration, adding a tenant is a row. The risks are a missing tenant filter, which I would guard with row-level security, and noisy neighbours, since everyone shares the same resources.

Then, if they want more

Schema per tenant gives namespace isolation and easy per-tenant restore, but with thousands of schemas the catalog gets huge, migrations have to run thousands of times, and connection pooling gets awkward. Database per tenant gives the strongest isolation and per-tenant tuning, regions and backups, but it is the most expensive and the hardest to operate at scale.

For 2,000 small tenants and 5 very large ones I would use a hybrid. The small tenants share tables in one pooled database, with tenant_id leading every primary key, unique constraint, index and foreign key so queries stay efficient and rows cannot point across tenants. The 5 large ones get dedicated databases with the same schema.

A tenant directory and a routing layer, such as Spring's AbstractRoutingDataSource, map each tenant to its data source, so the code does not care where a tenant lives. That also makes it possible to move a tenant later with a copy, a verification, and a routing switch.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 15
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.
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 three multi-tenant database models for a B2B SaaS on PostgreSQL: shared tables...": https://scrivio-interview-library.netlify.app/databases/data-modeling/db9ecf0982/