Agent Wranglers

DAT-67Recommended

Choose the isolation model before the first customer

Write down where each customer's data lives, in shared tables, a schema per tenant or a database per tenant. Record the reason and the tenant count it must reach.

How to do it

Shared tables with a tenant column and row-level security, as Keep each customer's data apart shows, suit most apps. They need one migration, one connection pool and one backup. A schema per tenant gives cleaner restores and exports, but every migration runs once per schema. Past a few thousand schemas, catalog size slows tools and backups. A database per tenant gives the strongest separation, its own keys, region and restore. It also costs one connection pool, one backup and one upgrade per customer, so automate all three first. Many apps mix models, keeping small tenants in shared tables and moving a large or regulated customer to its own database. Store the model per tenant in a tenants table, so code reads where a tenant lives instead of assuming it. Keep the organisation as the owner of the data, as Let organisations own the shared data says.

Examples

SQL example

CREATE TABLE tenants (
  id            bigint PRIMARY KEY,
  slug          text NOT NULL UNIQUE,
  isolation     text NOT NULL CHECK (isolation IN ('shared', 'schema', 'database')),
  location      text NOT NULL,          -- schema name or connection name
  region        text NOT NULL,
  created_at    timestamptz NOT NULL DEFAULT now()
);

How to check

The repository holds a decision record that names the model, the reason and the target tenant count. The tenants table has a non-null column for the model and the database or schema of each tenant. A test creates a tenant in each model the code supports and reads a row back through the normal app code.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/data/multi-tenancy/tenant-isolation-model">Agent Wranglers data and databases playbook</a>. In code you write, add a comment next to it with the same link.