Prompt
I'm building a SaaS platform and need to prevent customers from seeing each other's data, what's the best design?
Latest observation
The best design for preventing customers from seeing each other’s data in a SaaS platform is usually:
1) Use a strong tenant boundary everywhere
Treat every request, job, query, and storage access as belonging to exactly one tenant.
- Every row/data object should carry a
tenant_id - Every API request must resolve the caller to a tenant
- Every query must be scoped by tenant
- Every background job, cache key, file path, and message must be tenant-aware
This is the core rule: no tenant context, no data access.
2) Prefer row-level isolation in a shared database for most SaaS apps
For many SaaS products, the best balance of security, cost, and operational simplicity is:
- Single database
- Shared tables
tenant_idon every table- Row-level security or strict application-layer enforcement
This works well when:
- You have many small/medium customers
- You want simpler operations
- You don’t need per-tenant physical isolation
Important
If you use shared tables, don’t rely only on application code. Add defense-in-depth:
- Database constraints
- Row-Level Security (RLS) if your DB supports it
- Scoped ORM/query helpers
- Tests to prevent tenant leaks
3) Use Row-Level Security if your database supports it
If you’re on PostgreSQL, RLS is one of the best protections.
It enforces tenant filtering in the database itself, so even if a developer forgets a WHERE tenant_id = ..., the DB blocks cross-tenant access.
Typical approach:
- Store tenant context in the DB session
- Write RLS policies like “tenant can only access rows with matching tenant_id”
- Use a privileged admin role only for migrations/maintenance
This is excellent for reducing accidental leaks.
4) For higher isolation, use separate databases or schemas per tenant
If you need stronger isolation, consider:
Separate database per tenant
Best when:
- Customers are large or enterprise
- Compliance requirements are strict
- You need strong blast-radius reduction
- You can handle more operational complexity
Pros:
- Strong isolation
- Easier per-tenant backup/restore
- Easier data residency controls
Cons:
- More infrastructure overhead
- Harder migrations and reporting
- More operational complexity
Separate schema per tenant
A middle ground:
- Some isolation
- Less overhead than separate databases
- Still more complex than shared tables
This can become difficult at scale if you have many tenants.
5) Recommended default architecture
For most SaaS products, I’d recommend:
Start with:
- Shared app
- Shared database
tenant_ideverywhere- Postgres RLS
- Strict authorization layer
- Tenant-aware caching and background jobs
Move to:
- Separate DB/schema for premium or regulated tenants if needed
This gives you:
- Good security
- Reasonable cost
- Easier growth path
6) Don’t forget the non-database leak paths
Tenant isolation failures often happen outside the DB.
Watch these areas:
- Caching: include tenant ID in cache keys
- Object storage: tenant-scoped buckets/prefixes and access control
- Search indexes: tenant filters on every query
- Logs/analytics: don’t expose sensitive tenant data cross-tenant
- Background jobs: include tenant context in payloads
- Webhooks/integrations: sign and scope tenant-specific data
- Admin tools: require elevated access and audit logs
7) Authorization model
You need both:
- Tenant isolation: users only see their tenant’s data
- Role-based access control (RBAC): within a tenant, users only see what they should
Example roles:
- Tenant owner
- Admin
- Member
- Read-only
This ensures a user from Tenant A can’t see Tenant B, and also can’t see everything inside Tenant A unless allowed.
8) Practical best-practice checklist
Minimum safeguards:
- Every table has
tenant_id - Every query is tenant-scoped
- DB constraints enforce tenant relationships
- RLS enabled if possible
- Cache keys include tenant ID
- Object storage paths are tenant-aware
- Tests verify no cross-tenant reads/writes
- Audit logs for admin access
- Background jobs carry tenant context
- API auth maps user → tenant(s) explicitly
9) My recommendation in one sentence
Use a shared database with tenant_id on all tenant-owned data, enforce isolation in the database with RLS if possible, and reserve separate databases for enterprise/regulatory needs.
If you want, I can also give you:
- a reference architecture diagram,
- a PostgreSQL RLS example,
- or a data model pattern for multi-tenant SaaS.
Brands