Database schema design for multi-tenant SaaS auth
Designing a robust database schema for multi-tenant SaaS applications is crucial for ensuring scalability, performance, and security. This blog post explores the key considerations and trade-offs when designing a database schema for multi-tenant environments.
Row Isolation
Row isolation is the process of ensuring that changes made to data in one tenant do not affect data in another tenant. This is a fundamental aspect of multi-tenant architecture, as it prevents data leakage and ensures that each tenant's data remains isolated.
Trade-offs
- Cheapest to run: one schema, one set of migrations, one connection pool.
- Every query must filter by tenant_id, and one missing filter leaks data between tenants, so enforce it in the database with row-level security, not only in application code.
- Tenants share tables and indexes, so a heavy tenant slows the others, and restoring one tenant's data alone is hard.
Schema Isolation
Schema isolation is the process of ensuring that each tenant has its own database schema. This is a common approach to multi-tenant architecture, as it allows each tenant to have its own data model and business logic.
Trade-offs
- Stronger separation: a query without a tenant filter cannot reach another tenant's tables, as long as each request sets its own search_path.
- Every migration runs once per tenant, so deploys slow down as tenants grow, and thousands of schemas bloat the system catalog.
- Backing up or restoring one tenant becomes practical.
Database Isolation
Database isolation is the process of ensuring that each tenant has its own database. This is a common approach to multi-tenant architecture, as it allows each tenant to have its own data storage and management.
Trade-offs
- Strongest isolation: separate data files, credentials and backups per tenant, and a large tenant can move to its own server.
- Highest cost and effort: a connection pool, migrations and monitoring for every database.
- Reporting across tenants needs a separate data pipeline.
Bastionary as a Solution
Bastionary is a self-hosted platform that provides a comprehensive solution for SaaS auth, billing, licensing, and feature flags. It is designed to be highly scalable, performant, and secure, and it provides a robust database schema design that supports multi-tenant environments.
Designing a robust database schema for multi-tenant SaaS applications is a complex task that requires careful consideration of trade-offs. By understanding the key considerations and trade-offs, you can design a database schema that supports multi-tenant environments and provides a secure and scalable solution.
Run it yourself
Bastionary is free, open-source software. Follow the quick start to have it running on your own server, or browse the releases on Git.