Prompt
I'm building a SaaS product with customer-specific data isolation — what architecture do you recommend?
Latest observation
For customer-specific data isolation in a SaaS product, I’d recommend choosing an architecture based on how strong the isolation needs to be, how many tenants you expect, and how much operational complexity you can tolerate.
My default recommendation
For most SaaS products, the best balance is:
Shared application + shared database + tenant-isolation at the data layer
Use:
- one app deployment
- one logical database
- every row scoped by
tenant_id - strict authorization in the application
- optional database row-level security if your database supports it
This is usually the best starting point because it gives:
- low cost
- simpler operations
- easier scaling early on
- flexibility to add stronger isolation later for high-value customers
Common multi-tenant isolation models
1. Shared everything
All tenants share:
- app
- database
- tables
Rows are separated by tenant_id.
Pros
- cheapest
- easiest to operate
- easiest to scale at first
Cons
- weakest blast-radius isolation
- highest risk if app authorization is flawed
- harder to meet strict compliance requirements
Best for
- early-stage SaaS
- SMB customers
- products where cost efficiency matters more than hard isolation
2. Shared app, separate database per tenant
Each customer gets its own database.
Pros
- strong isolation
- easier tenant-level backup/restore
- easier customer-specific data deletion/export
- lower risk of cross-tenant leakage
Cons
- more operational overhead
- more migrations to manage
- harder to scale to very large tenant counts
- connection management can get complex
Best for
- enterprise customers
- regulated data
- customers who require strong logical separation
3. Separate app + separate database per tenant
Each tenant gets its own full stack.
Pros
- strongest isolation
- maximum customizability
- ideal for highly regulated or “bring your own environment” customers
Cons
- expensive
- high ops complexity
- difficult to automate at scale
Best for
- high-compliance enterprise deployments
- premium isolated environments
- very small number of tenants with large revenue per tenant
4. Hybrid / tiered model
Use shared infrastructure for most tenants, but move larger or regulated tenants to dedicated databases or dedicated environments.
Pros
- good balance of cost and isolation
- supports enterprise sales
- allows progressive hardening
Cons
- more architectural complexity
- you need tenant placement/migration tooling
Best for
- most SaaS companies that expect both SMB and enterprise customers
What I usually recommend in practice
Start with a hybrid architecture
- default: shared app + shared DB with
tenant_id - enterprise tier: dedicated database or dedicated schema
- very large / regulated customers: dedicated environment
This gives you a path to evolve without rewriting everything later.
Key design principles
1. Make tenant context mandatory everywhere
Every request should resolve a tenant context from:
- subdomain
- auth token claims
- org membership
- request metadata
Then enforce tenant scope consistently in:
- queries
- caching
- background jobs
- logging
- analytics
- file storage access
2. Enforce isolation in more than one layer
Don’t rely only on application code.
Use:
- application authorization checks
- database constraints/indexes
- row-level security if available
- per-tenant storage paths
- per-tenant encryption keys if needed
Defense in depth matters.
3. Never trust client-supplied tenant IDs alone
A user should not be able to simply send tenant_id=123 and access that tenant’s data unless your server has verified membership and authorization.
Derive tenant access from authenticated identity, not raw request input.
4. Separate control plane and data plane
A clean pattern is:
- control plane: tenant provisioning, billing, auth, plan management
- data plane: app logic and tenant data operations
This makes provisioning and scaling much easier.
5. Design for export, delete, and restore
Tenant isolation becomes much easier if you can:
- export a tenant’s full data
- delete a tenant completely
- restore a single tenant from backup
That’s especially important for compliance and support.
If you want strong isolation with manageable complexity
A strong practical pattern is:
- PostgreSQL
- shared app tier
tenant_idon all tenant-owned tables- Row Level Security
- tenant-aware Redis cache keys
- object storage prefixes per tenant
- background jobs include tenant context
- dedicated databases only for premium/regulated tenants
This is a very solid architecture for many SaaS products.
When to choose dedicated databases
Consider per-tenant databases if:
- customers demand hard separation
- you have enterprise sales
- compliance is important
- you need tenant-specific backup/restore
- your tenants are large enough to justify it
If you expect thousands of tiny tenants, per-tenant DBs can become painful unless you have strong automation.
My short recommendation
If you’re early or mid-stage:
Use a shared app and shared database with strict tenant scoping, and design the system so high-value tenants can later move to dedicated databases.
If you want, I can also give you:
- a reference architecture diagram,
- a PostgreSQL schema pattern with
tenant_idand RLS, or - guidance tailored to your expected tenant count, compliance needs, and cloud stack.
Brands