2024-07-27 6 min read

Database-per-Tenant vs Shared Schema: Your SaaS Architecture Decision

Choose the wrong multi-tenancy model and you'll pay for it in complexity, cost, or security. We break down the tradeoffs that matter.

Database-per-Tenant vs Shared Schema: Your SaaS Architecture Decision

You're building a SaaS product. Your first customer signs. Then your tenth. Then your hundredth. Now you're asking: should each tenant get their own database, or should they all share one?

This decision cascades through your entire system—your deployment pipeline, your backup strategy, your data isolation guarantees, and your ops overhead. Get it wrong, and you're rewriting core infrastructure months in. Pick the right one, and you've got a foundation that scales cleanly.

There's no universal answer. But there are clear tradeoffs, and your product's constraints will tell you which direction to lean.

Database-per-Tenant Architecture

Give each customer their own database instance (or logical database on a shared server). Complete isolation, zero query cross-contamination, straightforward compliance audits.

Advantages

Security and isolation: A bug in your query logic can't leak one tenant's data to another. A compromised connection affects only that customer. Compliance requirements around data residency become trivial—store each tenant's database in the region they specify.

Performance predictability: One tenant's runaway query doesn't slow down everyone else. Resource contention is eliminated by design.

Debugging simplicity: When something breaks for a customer, you connect to their database and investigate. No cross-tenant noise.

The Cost

Operational overhead. Here's what you actually have to manage:

bash
# Every schema migration now requires orchestration
for tenant_id in $(list_all_tenants); do
  migrate_database "tenant_$tenant_id"
  if [ $? -ne 0 ]; then
    rollback_database "tenant_$tenant_id"
  fi
done

Every deployment becomes a choreography. Hundreds of databases means hundreds of backup jobs, hundreds of monitoring rules, hundreds of disaster recovery scenarios. Your database bill climbs linearly with customer count—not ideal.

Shared Schema Architecture

Everyone lives in the same database. Add a

code
tenant_id
column to every table. Filter queries by tenant. Simple.

Advantages

Operational simplicity: One database to manage. One backup. One set of migrations. One monitoring dashboard.

Cost efficiency: Database fees don't scale with customer count (until you hit scale where you'd shard anyway).

Feature rollout speed: Release features to all customers at once, on the same schema version.

The Risk

A single

code
WHERE
clause mistake can expose data across tenants. Your entire security model depends on discipline.

typescript
// Disaster waiting to happen
async function getOrders(tenantId: string, userId?: string) {
  return db.query(
    'SELECT * FROM orders WHERE user_id = $1',
    [userId]  // Forgot to filter by tenant_id!
  );
}

// Correct version
async function getOrders(tenantId: string, userId: string) {
  return db.query(
    'SELECT * FROM orders WHERE tenant_id = $1 AND user_id = $2',
    [tenantId, userId]
  );
}

You also need defensive patterns—row-level security policies, query-building abstractions, automated tests that verify no query runs without a tenant filter. The simplicity is deceptive.

The Hybrid Approach

Many teams ship with shared schema early (easy to build, fast to deploy), then migrate critical tenants to isolated databases as they grow large or have specific compliance needs. This buys you time to understand your actual operational capacity.

At LavaPi, we've seen teams successfully run both models—the choice depends entirely on your customer profile, compliance requirements, and team size.

What Matters Most

If you have five enterprise customers paying six figures, isolation is worth the operational cost. Their data sensitivity and security expectations justify separate databases.

If you have 500 SMB customers paying per-seat, shared schema with aggressive testing and row-level security is the right call. The ops burden would strangle your engineering team.

Start by asking: What's your largest customer willing to accept? What's your smallest customer able to afford? The distance between those answers tells you which model fits.

Then build the other one into your roadmap for later.

Share
LP

LavaPi Team

Digital Engineering Company

All articles