awesome-everything RU
↑ Back to the climb

Databases

Schema-based sharding and multi-tenancy alternatives

Crux Citus 12 schema-based sharding, database-per-tenant, application-level routing, and RLS are four valid multi-tenancy architectures — each with a specific tenant count and isolation-requirement range.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 12 min

A B2B SaaS with 200 enterprise customers needs contractual data isolation — each customer’s data must never be co-mingled in the same table. Row-based Citus sharding puts multiple tenants in the same physical shard. The product team asks: is there a stronger isolation model that Postgres can provide?

Four multi-tenancy architectures

ArchitectureIsolation unitTenant countOperational cost
RLS on single PostgresRow-level (policy enforcement)Unlimited until single-PG saturatesLow; policy bugs silently expose data
Citus row-based shardingShard (multiple tenants per shard)Thousands to tens of thousandsMedium; Citus handles routing
Citus schema-based (12+)Schema (one per tenant)Low thousands (ceiling)Medium; schema is the deployment unit
Database-per-tenantFull Postgres databaseDozens to ~500High; N backups, N upgrades, N pools

Citus 12 schema-based sharding

Introduced in July 2023, Citus 12 added schema-based sharding: each tenant gets a dedicated Postgres schema, and Citus distributes schemas across workers.

-- Tenant onboarding: create a schema, Citus assigns it to a worker
CREATE SCHEMA acme;
CREATE TABLE acme.orders (id BIGINT, ...);
CREATE TABLE acme.users  (id BIGINT, ...);

-- Application: scope connection to tenant's schema
SET search_path TO acme, public;
-- All subsequent table references resolve within acme's schema
-- Citus routes to acme's worker transparently

Benefits over row-based sharding:

  1. No shared distribution key column required: any query within the schema runs on its worker — joins between any tables in the same schema are always local.
  2. Schema is the deployment unit: CREATE SCHEMA for onboarding, DROP SCHEMA CASCADE for offboarding, move schema between workers for rebalancing.
  3. Clean tenant isolation: table names are different per schema — one tenant cannot accidentally read another’s data.
  4. Per-tenant migrations: ALTER TABLE acme.orders ADD COLUMN ... only touches that schema.

Limits:

  • Tenant count ceiling: Citus recommends fewer than a few thousand schemas. Schema metadata is loaded into the planner on every query; thousands of schemas slow planning.
  • Cross-tenant queries are still cross-shard: analytics across multiple tenants still require fan-out.
  • Reference tables do not apply within schemas: lookup tables need to be in each schema or accessed from a shared schema.

Sweet spot: B2B SaaS with 50–2000 enterprise tenants where contractual or regulatory data isolation is required.

Application-level routing

Skip Citus entirely: your application code maintains a shard map (a table or service mapping tenant_id → connection_string) and routes queries explicitly.

// Example: routing layer reads tenant from request context
const shard = await shardMap.get(req.tenantId);
const db = connectionPool(shard.connectionString);
return db.query('SELECT * FROM orders WHERE ...', [req.tenantId]);

Pros: explicit, no extra database product, maximum control, per-tenant routing logic (region, plan tier). Cons: every cross-shard concern becomes app code — joins, transactions, fan-out, rebalancing, failover. The routing layer is critical-tier-1 infrastructure that you now own.

Common when shard count is small (10–100) and queries are highly tenant-scoped. At 5000 tenants with complex queries, Citus is usually less engineering effort over a multi-year horizon.

Database-per-tenant

Each tenant gets a dedicated Postgres database (or instance). Maximum isolation: tenants literally cannot share any infrastructure at the database level.

Operational model:

  • PgBouncer pool per (tenant, database)
  • Backups per tenant database
  • Postgres minor and major upgrades per tenant database
  • Schema migrations deployed per tenant (or batched)

Practical ceiling: ~100–500 tenants before operational overhead dominates. Below that, database-per-tenant is often simpler than Citus for regulated industries (financial, healthcare) where contractual isolation is hard requirement.

Why this works

Why does Citus recommend fewer than a few thousand schemas for schema-based sharding? The planner loads schema metadata (pg_namespace, pg_class entries) into working memory at planning time. With 10 schemas this is negligible. With 10,000 schemas, every query plan includes processing thousands of namespace entries, adding 10–50ms to planning time — dominating OLTP query execution. This is why schema-based sharding has a ceiling and row-based sharding (which does not load per-tenant metadata into the planner) scales to tens of thousands of tenants.

Quiz

A B2B SaaS has 200 tenants, contractual data isolation per customer is required, and the engineering team has no Citus expertise. Which architecture fits best?

Quiz

What is the main tenant-count ceiling of Citus schema-based sharding and why?

Recall before you leave
  1. 01
    What did Citus 12 (July 2023) add and what problem does it solve that row-based sharding does not?
  2. 02
    For a 5000-tenant B2B SaaS where the top 5% generate 80% of traffic and all queries carry tenant_id, which multi-tenancy architecture is most likely the right choice and why?
  3. 03
    When is application-level routing (custom shard map) the right choice over Citus?
Recap

Four multi-tenancy architectures exist on Postgres, each fitting a different tenant count and isolation requirement: RLS on single Postgres (unlimited tenants, logical isolation until capacity hits), Citus row-based sharding (thousands to tens of thousands, shard-level isolation), Citus schema-based sharding (low thousands, per-schema structural isolation, introduced Citus 12/July 2023), and database-per-tenant (dozens to ~500, maximum isolation, highest operational cost). Schema-based sharding eliminates the need for a shared distribution key and provides clean per-tenant table namespacing, but its planner overhead ceiling is a few thousand schemas. Application-level routing with a custom shard map is a viable alternative for small shard counts with simple routing needs. The choice must match tenant count, isolation requirement, and operational maturity.

Connected lessons
appears again in140
Continue the climb ↑Online resharding, 2PC, and the operational cost of sharding
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.