SaaS · Architecture
Database per tenant versus a shared database: how I chose for a hospital SaaS.
Multi-tenant SaaS data isolation compared: shared tables, schema per tenant and database per tenant. Trade-offs in security, cost, migrations and backups.
Key takeaways
- Multi-tenancy means one codebase serving many customers, and the key decision is how to keep their data apart.
- Shared tables are cheapest to run but one missing
WHERE tenant_idcan leak data. - Database per tenant gives the strongest isolation and simple per-customer backups, at the cost of more operational work.
- The right choice depends on the sensitivity of the data and how many tenants you expect.
What multi-tenant means
A tenant is one customer of your SaaS: a hospital, a business, a school. A multi-tenant application serves many tenants from the same code. The central design question is where each tenant's data lives and how you stop one tenant ever seeing another's.
The three common models
1. Shared database, shared tables
Every table has a tenant_id column, and every query filters by it.
- Pros: simplest to build and run; one migration updates everyone; cheapest at large tenant counts.
- Cons: isolation relies on never forgetting the filter. A single missing
WHEREclause, or a bug in a report, can show one tenant another's data. Backing up or restoring one tenant is awkward. One heavy tenant can affect everyone.
2. Shared database, separate schema per tenant
Each tenant gets its own schema or set of tables in the same database server.
- Pros: better separation than shared tables; still one server to manage.
- Cons: migrations must run per schema; some database engines handle thousands of schemas poorly.
3. Separate database per tenant
Each tenant gets its own database, created from a common schema when they sign up.
- Pros: strongest isolation; per-tenant backup, restore and deletion are straightforward; a tenant's problems stay with them; easy to move one big tenant to its own server.
- Cons: more databases to manage; migrations have to run across all of them; connection handling needs care; reporting across tenants requires extra work.
Why I chose database per tenant for the hospital SaaS
My hospital management system began as a working system for a single hospital. When a second hospital wanted it, there were two options: set up another full copy by hand, which does not scale, or put two hospitals' patients in the same tables, which is not something a medical product should do.
So it became a platform where each hospital gets its own database, created from one schema when it signs up. Platform data, such as plans, invoices and the registry of hospitals, lives in a separate platform database. Each hospital database has 48 tables, and the platform has 19.
Reasons that mattered for this product:
- Sensitivity. Patient records are among the most sensitive data there is. I preferred a structural guarantee over a coding discipline.
- Simple operations per customer. Back up, restore, suspend or delete one hospital without touching the others.
- Expected scale. The number of tenants is in the tens or hundreds, not millions, so database count is manageable.
- Clear mental model. Hospital staff never see the platform layer, and the platform never reads a hospital's patients.
The costs I accepted
- Migrations have to be applied to every hospital database. I treat the schema as a single source and run changes across all tenants with care.
- Provisioning a new hospital means creating a database and its first admin automatically.
- Connections. The application switches its database connection per request based on the tenant.
- Cross-tenant reporting is limited by design, which is mostly a feature for a medical product.
A decision guide
| If this is true... | Lean towards |
|---|---|
| Very many small tenants, low-sensitivity data | Shared tables |
| Moderate tenants, some separation wanted | Schema per tenant |
| Sensitive data, tens to hundreds of tenants, per-tenant backup needs | Database per tenant |
Whichever you choose, write tests that try to read another tenant's data. They are worth more than any amount of care.
Frequently asked questions
Is shared tables unsafe?
No, many large SaaS products use it successfully. It requires strict, enforced scoping on every query, ideally in a central layer, plus tests that try to break it.
Can I move from shared to separate later?
Yes, but it is a significant migration. It is easier to decide early if your data is sensitive.
What about performance?
Separate databases isolate noisy neighbours, but you now manage many connections. Measure for your workload instead of assuming.
See how the hospital SaaS keeps every hospital in its own database.
See the Hospital SaaS