Video in progress
In a SaaS system, how do you compare the tenant data isolation models: separate database, separate schema and shared schema?
Database per tenant / Schema per tenant / Shared schema — Isolation: highest / high / depends on app/RLS; Number of tenants: tens to hundreds / hundreds / thousands+; Cost: high / medium / low; Migration: per database / per schema / one; "Noisy neighbor": no / yes (one server) / yes; Restoring one tenant: easy /…
📖 Answer
| Database per tenant | Schema per tenant | Shared schema | |
|---|---|---|---|
| Isolation | highest | high | depends on app/RLS |
| Number of tenants | tens to hundreds | hundreds | thousands+ |
| Cost | high | medium | low |
| Migration | per database | per schema | one |
| "Noisy neighbor" | no | yes (one server) | yes |
| Restoring one tenant | easy | moderate | hard |
| Reporting across tenants | hard | hard | easy |
In a shared schema, every table has tenant_id and all indexes start with it. The @TenantId field in Hibernate 6 adds the condition to every query for you. If you add PostgreSQL row-level security on top, the database won't hand over another tenant's row even if the application makes a mistake:
alter table orders enable row level security;
create policy tenant_isolation on orders
using (tenant_id = current_setting('app.tenant_id')::bigint);
In practice a hybrid is common: small customers in the shared schema, large or special-requirement customers (banks, government) in their own database. For that, the code has to know where a tenant lives and be able to route.
When deciding I look at: contracts and compliance (a "data must be stored separately" requirement), the size of the largest tenant (can one tenant choke the shared database?), and operational capacity (can you back up, monitor and upgrade 500 databases?).
Whichever model you pick, verify tenant isolation with automated tests. In SaaS this is the most expensive mistake.
Sign up to mark studied questions and save your progress.