Chapter 35
SaaS Multi-Tenancy
SaaS Multi-tenancy Database Design
Multi-tenancy is a core SaaS architecture concern: multiple customers share infrastructure while maintaining data isolation.
Three Models
- Database per Tenant: Maximum isolation, highest cost, O(N) ops complexity
- Schema per Tenant: Good isolation, shared instance, 500 tenant limit
- Shared Tables: Lowest cost, tenant_id column on every table, noisy neighbor risk
Shared Schema Pattern
Every table gets a tenant_id column with an index. Application middleware injects tenant context from JWT/subdomain. Repositories always filter by tenant_id — never write a query without it.
Noisy Neighbor Mitigation
- Use
MAX_EXECUTION_TIMEhint for large queries - Route batch exports from free-tier tenants to read replicas
- Partition by tenant_id range to reduce hot spots
Custom Fields
JSON column is simplest: custom_fields JSON. Create functional indexes on frequently-queried keys: ((custom_fields->>'$.industry')).