Database schemas
Multi tenant SaaS
Tenant isolation, and every join that could leak across it.
What this board gets wrong on purpose
The tension
`subscriptions.seats` is a number nobody enforces: no constraint ties it to the count of active rows in `memberships`, so an organisation can run twenty people on a five seat plan until billing notices. Two smaller ones are left in on purpose - `memberships` has nothing stopping the same user joining the same org twice, and an archived org keeps its unique slug forever.
Start here
Why this design
Almost every B2B product is this schema underneath, and it is the one where a mistake is a security incident rather than a bug: a query that forgets its tenant filter shows one customer another customer's data, and nothing in development catches it because every row there belongs to the same org. Interviewers use it to see whether you can name an isolation model and defend it, whether you know where a role belongs, and whether you notice the constraints a schema cannot express. Real teams meet it twice - when they design it, and two years later when the biggest customer asks for its own database in Frankfurt.
What you will learn
- Why `org_id` is copied onto every tenant owned row - `documents` and `document_versions` included, where it is derivable - so row level security is one predicate with no join, and the drift that buys.
- Shared schema, schema per tenant and database per tenant compared at 40,000 tenants, and why the answer is "shared, with an escape hatch for the whales".
- Why the role lives on `memberships` and not on `users`, and why an invitation is its own table addressed to an email rather than a half-made membership.
- The RLS failure modes that actually leak data: a session variable surviving into the next tenant's transaction on a pooled connection, a table owner bypassing policies, a new table shipped without one.
- Why 99% of the bytes are in two append-only tables, and how org_id-leading composite indexes make a tenant export, a tenant restore and a tenant move into range scans.
- What `subscriptions.seats` cannot enforce, the three ways to enforce it, and the provisioning path each one tends to miss.
How to read this board
- The tables first, left to right. GLOBAL: NOT OWNED BY ANY TENANT holds `users`, `plans` and `roles` - the rows that belong to no single org.
- THE TENANT AND WHO IS IN IT: `orgs`, `memberships` and `subscriptions`, then the org scoped access records `invitations`, `api_keys` and `audit_log`.
- THE TENANT'S OWN DATA: one ownership chain, `projects` -> `documents` -> `document_versions`, each carrying `org_id`. Press Play for the eight step walkthrough, which reads the tables in the order the model makes sense.
- Then the written design below the tables: 1 · Requirements, 2 · Capacity, 3 · Storage, 4 · Availability, 5 · How it works (one request and one invitation, end to end, across these tables), 6 · Deployment.
- The conversation to have: `subscriptions.seats` is an integer nothing enforces, so a five seat plan can hold twenty members. Two smaller ones are left in on purpose - no UNIQUE on (org_id, user_id) in `memberships`, and an archived org keeps its slug forever.
1 · Requirements
Ask these before drawing anything
- Can one person belong to more than one organisation with one login? For any product sold to agencies, consultants or groups of companies the answer is yes, and it decides that `users` is global and the role lives on the membership.
- What does isolation have to guarantee, and to whom? "No query can return another tenant's row even if the application forgets a filter" is a database level guarantee. "Each customer's data is in a separate database" is a contractual one, and only some customers will pay for it.
- Will any customer require data residency - the EU, a specific country? If yes, the tenant must be movable, and the global tables have to be thought about now.
- Who is allowed to see across tenants? Support staff, billing jobs and analytics all need to, and every one of them is a deliberate, audited bypass rather than a missing filter.
- Are seats a hard limit or an invoice line? A hard limit needs enforcement on every path that creates a membership; an invoice line needs only a count at the end of the period.
- How long is the audit log kept, and can a tenant export it? Enterprise contracts commonly ask for one to seven years and a self-serve export, which decides its storage tier.
Functional
- Tenancy: an organisation (`orgs`) is the unit of isolation, billing and administration. Every tenant owned row records the org it belongs to.
- Accounts: one global `users` row per email. Password or SSO; a person can be a member of many orgs and switches between them without signing in again.
- Roles: four built in roles every org gets - owner, admin, member, billing - plus custom roles an org defines for itself. A role is held per membership, never per user.
- Invitations: an admin invites an email address AS a role; the link expires after 7 days; acceptance creates the account if needed and the membership in one transaction, and records which membership the invitation became.
- Billing: one live subscription per org on a plan from a global catalogue, with a purchased seat count. Suspended members still hold a seat.
- API keys: a machine credential that acts as a specific member and inherits that membership's role; revoking the member revokes the keys.
- Audit: every mutating action and every security event (sign-in, role change, key creation, invitation) is recorded against the acting membership, survives that member being removed, and is exportable by the tenant.
- Tenant data: projects, documents and an immutable version history of each document.
Non-functional
- Isolation: zero cross tenant reads or writes, enforced by the database (row level security) and not only by application code. This is the requirement every other one gives way to.
- Authorisation on every request - resolve the membership and role for (org, user) - under 2 ms p99 including cache.
- Membership or role revocation takes effect within 60 seconds on every node.
- Application reads p95 under 150 ms; document save p95 under 250 ms.
- A tenant can be exported, restored to a point in time, moved to another database or region, and deleted, without touching any other tenant.
- No single tenant can take more than its share: one org's bulk export must not move another org's p99.
Out of scope
- Payment collection and invoicing - the billing provider owns them; `provider_customer_id` is the seam.
- SSO and SCIM provisioning, except as another path that creates memberships (and so another path that must respect seats).
- Hierarchical organisations (a parent company with child orgs). A real request, and a different board: it turns `org_id` from a value into a tree.
The rest of this board
Available on Tier Slate
This page publishes the question. The answer — 5 more written pages, an 8-step narrated walkthrough and a 12-table schema — is the board itself, and it opens in Tierslate.
- 5written pages
- 8walkthrough steps
- 12tables74 columns
5 pages behind this one
- Capacity estimation
- Storage estimation
- Availability
- How it works
- Deployment plan