Prompt
What should I use for tenant isolation in a B2B SaaS app?
Latest observation
Nov 14, 2026
OpenAI APIWeb search: off
For a B2B SaaS app, the usual choice is logical tenant isolation at the application and data layer, with the exact model depending on your security/compliance needs and scale.
Common options
1) Shared database, shared schema
- Every row has a
tenant_id - App filters by tenant on every query
- Easiest and cheapest to start
- Good for many SaaS products
Pros
- Simple ops
- Easy to scale early
- Low cost
Cons
- Mistakes in query scoping can leak data
- Harder to support strong per-tenant compliance boundaries
2) Shared database, separate schema per tenant
- Each tenant gets its own schema
- Better isolation than row-level tenancy
- More operational overhead
Pros
- Cleaner separation
- Easier per-tenant exports/backups than row-level
Cons
- More migrations/ops complexity
- Still shared infrastructure
3) Separate database per tenant
- Stronger isolation
- Useful for larger customers or regulated environments
Pros
- Best isolation
- Easier to meet enterprise/security requirements
- Can customize backups, encryption, scaling per tenant
Cons
- Highest operational complexity
- Harder to manage migrations at scale
- More expensive
What I’d recommend
For most B2B SaaS startups:
Start with shared DB + tenant_id + strong application enforcement.
Then add:
- Row-level security if your database supports it well
- Separate databases or schemas for premium/regulated tenants later
This is often called a hybrid model:
- Default tenants: shared tenancy
- Enterprise/compliance tenants: isolated deployment or isolated database
Best practices if you choose shared tenancy
- Put
tenant_idon every tenant-owned table - Enforce tenant scoping in:
- application code
- database constraints
- background jobs
- APIs
- caches
- search indexes
- object storage paths
- Use row-level security (RLS) if possible
- Never trust client-supplied tenant IDs alone
- Make tenant context part of auth/session claims
- Test for cross-tenant access bugs
- Audit logs should include tenant ID
Rule of thumb
- Early stage / SMB SaaS: shared DB + tenant_id
- Mid-market / growing SaaS: shared DB + RLS + selective isolation
- Enterprise / regulated: separate DB or separate stack for some tenants
If you want, I can also give you:
- a decision matrix for tenant isolation, or
- a reference architecture for PostgreSQL/MySQL + app code.