Multi-tenant data
0-day streakReview

Your progress

Level 1

0 XP100 XP to level 2
0day streak
0best streak
0topics started
0cards reviewed
    Review cards

    Progress is stored only in this browser. No account is required.

    Multi-tenant data: the 60-second story

    Multi-tenant data

    One UPDATE forgot its tenant filter. Every tenant’s invoices got archived.

    Shared tables are cheapest, but the wall between tenants becomes a WHERE clause. Most pool leaks are a forgotten one.

    Where does the wall between tenants sit?

    A database each, a schema each, or a tenant_id on every row of shared tables. A sharded pool adds a catalog saying where each tenant lives.

    Is the repository filter enough?

    Bad: Filter in code only

    Raw SQL, an ORM escape hatch or an admin endpoint skips it.

    Good: Plus row-level security

    The database filters every row too. One bug isn’t one breach.

    The query forgot its tenant. PostgreSQL didn’t.

    A row-level security policy checks every row before the query’s own conditions. Four overdue invoices matched; only acme’s two came back.

    Request A sets tenant acme on a pooled connection. Request B, for zenmart, borrows it and forgets to set one.

    What B sees

    Acme’s rows. Use SET LOCAL: the tenant dies with the transaction.

    200,000

    connections for 1,000 tenant databases

    Pools of 10 per tenant database on 20 app instances. Over 10 servers, still 20,000 each; the default limit is typically 100. A pooler sized to busy tenants: ≈450.

    Bad import at 10:43. Put back one tenant as of 10:42.

    Its own database: restore 2 GB, repoint the catalog, minutes. A shared database: restore all 2 TB to a side copy, extract, merge. Hours.

    Grow into a hybrid

    1. Shared tables, tenant_id and RLS from day one
    2. A catalog, even with one database
    3. Place whales alone and EU tenants in the EU
    4. One schema everywhere, so moves are data copies

    Remember

    Shared tables with tenant_id leading every key, row-level security set per transaction, and a catalog that gives whales their own database.