The SaaS Founder's Guide to Multi-Tenant Architecture | Deployxa

Multi-tenant architecture is the foundation of every SaaS. Here is the founder's guide to choosing the right model: shared, isolated, or hybrid.

← Back to Dispatch Articles
Engineering Log

The SaaS Founder's Guide to Multi-Tenant Architecture

Multi-tenant architecture is the foundation of every SaaS. Here is the founder's guide to choosing the right model: shared, isolated, or hybrid.

The SaaS Founder's Guide to Multi-Tenant Architecture

Key Facts

  • Direct answer: The direct answer is that multi-tenant architecture has three models: shared database (all tenants in one database, isolated by tenant_id), isolated schema (each tenant has its own schema), and dedicated database (each tenant has its own database).

  • Model 1: Shared Database (Recommended for Most SaaS): In the shared database model, all tenants share a single database.

  • Model 2: Isolated Schema: In the isolated schema model, each tenant has its own database schema (a set of tables).

  • Model 3: Dedicated Database: In the dedicated database model, each tenant has its own database instance.

  • Row-Level Security (RLS): The Best of Both Worlds: Postgres supports Row-Level Security (RLS), which enforces tenant isolation at the database level (not just the application level).

Multi-tenant architecture is how your SaaS serves multiple customers (tenants) from a single application. Each tenant's data needs to be isolated (tenant A cannot see tenant B's data), but the application is shared (one codebase, one deployment). Choosing the right multi-tenant model is one of the earliest and most consequential architectural decisions for a SaaS. This article is the founder's guide to choosing the right model.

The direct answer is that multi-tenant architecture has three models: shared database (all tenants in one database, isolated by tenant_id), isolated schema (each tenant has its own schema), and dedicated database (each tenant has its own database). For most early-stage SaaS, the shared database model is the right choice — it is the simplest, the cheapest, and the most scalable. For more on database architecture, see our article on the SaaS founder's guide to choosing a database.

Model 1: Shared Database (Recommended for Most SaaS)

In the shared database model, all tenants share a single database. Each table has a tenant_id column that identifies which tenant each row belongs to. The application filters all queries by tenant_id, which ensures tenant A cannot see tenant B's data.

How it works

-- Every table has a tenant_id column
CREATE TABLE projects (
  id SERIAL PRIMARY KEY,
  tenant_id INTEGER NOT NULL REFERENCES tenants(id),
  name VARCHAR(255) NOT NULL,
  created_at TIMESTAMP DEFAULT NOW()
);

-- Every query filters by tenant_id
SELECT * FROM projects WHERE tenant_id = 123;

Pros

  • Simple. One database, one schema, one connection string.
  • Cheap. One database instance serves all tenants.
  • Scalable. Adding a new tenant is just inserting a row in the tenants table.
  • Easy to query across tenants. Analytics, admin dashboards, and reporting are easy because all data is in one database.

Cons

  • Data isolation is application-level. If the application forgets to filter by tenant_id, tenant A can see tenant B's data. This is the biggest risk.
  • Noisy neighbor. If tenant A runs a heavy query, it might slow down tenant B (because they share the same database).

When to choose

For most early-stage SaaS (1-1000 tenants), the shared database model is the right choice. It is the simplest, the cheapest, and the most maintainable.

Model 2: Isolated Schema

In the isolated schema model, each tenant has its own database schema (a set of tables). All schemas are in the same database instance, but each tenant's tables are separate.

How it works

-- Tenant A's schema
CREATE SCHEMA tenant_a;
CREATE TABLE tenant_a.projects (...);

-- Tenant B's schema
CREATE SCHEMA tenant_b;
CREATE TABLE tenant_b.projects (...);

Pros

  • Better isolation. Each tenant's data is in a separate schema, which reduces the risk of cross-tenant data leaks.
  • Per-tenant backup. You can back up or restore individual tenants.

Cons

  • More complex. You need to manage multiple schemas (create, migrate, drop).
  • Harder to query across tenants. Analytics and reporting require querying multiple schemas.
  • Schema migration is complex. Each migration needs to be applied to all tenant schemas.

When to choose

Choose isolated schema if you need better data isolation (e.g., for compliance reasons) but do not want the cost of a separate database per tenant.

Model 3: Dedicated Database

In the dedicated database model, each tenant has its own database instance. This provides the highest level of isolation but is the most expensive and complex.

Pros

  • Maximum isolation. Each tenant's data is in a separate database, which eliminates cross-tenant data leaks.
  • Per-tenant scaling. You can scale each tenant's database independently.
  • Per-tenant backup. You can back up or restore individual tenants.

Cons

  • Expensive. Each tenant needs a separate database instance, which multiplies the cost.
  • Complex. You need to manage multiple database instances (provision, migrate, monitor, back up).
  • Harder to query across tenants. Analytics and reporting require querying multiple databases.

When to choose

Choose dedicated database if you have enterprise customers who require maximum isolation (e.g., for HIPAA, SOC 2, or contractual reasons) and are willing to pay for it.

Row-Level Security (RLS): The Best of Both Worlds

Postgres supports Row-Level Security (RLS), which enforces tenant isolation at the database level (not just the application level). With RLS, even if the application forgets to filter by tenant_id, the database enforces the isolation.

-- Enable RLS
ALTER TABLE projects ENABLE ROW LEVEL SECURITY;

-- Create a policy: users can only see rows where tenant_id matches their current tenant
CREATE POLICY tenant_isolation ON projects
  USING (tenant_id = current_setting('app.current_tenant_id')::INTEGER);

With RLS, the shared database model becomes much safer, because the database enforces isolation even if the application does not. For more on RLS, see our article on Deployxa vs Supabase.

Common Pitfalls and Troubleshooting

The first pitfall is choosing the dedicated database model too early. Many founders choose it because it sounds more scalable, but for a SaaS with 100 tenants, a shared database is more than sufficient. The fix is to start with the shared database model and switch only when you have a specific need.

The second pitfall is not enforcing tenant isolation. If the application forgets to filter by tenant_id, tenant A can see tenant B's data, which is a security incident. The fix is to use RLS (Postgres) or to use an ORM that enforces tenant isolation automatically.

The third pitfall is not indexing the tenant_id column. Without an index, queries that filter by tenant_id are slow (full table scan). The fix is to add an index on tenant_id for every table.

The fourth pitfall is not testing cross-tenant isolation. If you do not test, you do not know if the isolation works. The fix is to write tests that verify tenant A cannot access tenant B's data.

The fifth pitfall is not planning for migration. If you start with the shared database model and later need to switch to isolated schema or dedicated database, the migration is complex. The fix is to design your schema with a tenant_id column from the start, which makes the migration easier.

Conclusion: Start Shared, Switch When Needed

For most early-stage SaaS, the shared database model (with RLS for safety) is the right choice. It is the simplest, the cheapest, and the most maintainable. Start with it, and switch to isolated schema or dedicated database only when you have a specific need (e.g., enterprise customers requiring maximum isolation). Do not over-engineer the architecture — start simple and switch when the product demands it.

Ready to design your multi-tenant architecture? Choose the shared database model, add a tenant_id column to every table, enable RLS, and index the tenant_id column. For more, see the SaaS founder's guide to choosing a database and how to scale your SaaS from MVP to first customers. Explore our free developer tools to speed up your workflow.

Ready to deploy with Deployxa?

Deploy your apps globally with automatic SSL and AI diagnostics.

Start Free Now