Multi-tenant SaaS & sharding

Each customer’s workspaces, roles and billing stay isolated on one shard.

Link workspaces and documents to an Organization once, and Napalm places each tenant’s rows together while checking membership on every query. When a customer grows or needs EU residency, move that tenant to a dedicated or regional shard online.

Read the SaaS guide
Replaces
  • Vitess or Citus composite keys
  • Oso or Zanzibar authz service
  • Separate billing ledger database
  • Per-table RLS join boilerplate

Organizations and roles

Schema links replace per-table organization_id columns and join policies.

Links from Document to Workspace to Organization keep each customer’s rows on one shard and check role membership on every query. Tables keep standard UUID keys with no composite shard columns to maintain.

Multi-tenant B2B SaaS
dbschema/queries/myOrganizations.edgeql
select Organization {  id,  name,  slug,  my_role := assert_single((select .members filter .identity ?= global caller)).role,}order by .name

Workspace invites

An invite row and its email job commit together.

Insert the Invite record and queue sendInviteEmail in a single statement. Every saved invite sends its email, and a rolled-back transaction never sends a broken link.

dbschema/queries/inviteMember.edgeql
with  invite := (    insert Invite {      org := (select Organization filter .id = <uuid>$org),      email := str_lower(<str>$email),      role := <Role>$role,    }  ),  email := (    insert ext::napalm::Job {      query := 'sendInviteEmail',      variables := <json>(invite := <str>invite.id),    }  ),select invite { id, email, role, expires }
Checkout, Stripe and outboxes

Enterprise SSO

Customer OIDC sign-in and owner rules run in the database.

Point an organization at its Okta or Entra issuer so verified employees join on sign-in. In role-change queries, a single filter leaves the last owner untouched so no workspace is ever orphaned.

dbschema/queries/changeRole.edgeql
update Membershipfilter .id = <uuid>$member  and (.role != Role.Owner or count((select .org.members filter .role = Role.Owner)) > 1)set { role := <Role>$role }
Sign-in and enterprise OIDC

Credit ledgers

Wallet balances and ledger entries update in one statement.

Insert a LedgerEntry with an exclusive idempotency_key and subtract the credits from CreditWallet together. Retried requests fail cleanly on duplicate keys, and constraints block negative balances.

Subscriptions and usage billing
dbschema/queries/spendCredits.edgeql
# Debit credits atomically, rolling back if idempotency_key repeats or balance drops below zerowith  amount := <int64>$amount,  target := assert_single((select CreditWallet filter .org_slug = <str>$org_slug)),  entry := (    insert LedgerEntry {      wallet := target,      delta_credits := -amount,      reason := <str>$reason,      idempotency_key := <str>$key,    }  ),  debited := (    update target    set { balance_credits := .balance_credits - amount }  ),select {  entry_id := entry.id,  remaining_credits := debited.balance_credits,}

Moving shards

Move a busy customer or EU workspace to its own shard online.

Split a crowded shard or move one organization’s root to a dedicated or regional shard from the CLI. Live reads and writes keep succeeding while the data moves.

Terminal
napalm placement split acme --shard shard-k3m9x2p7qanapalm placement move acme --root $ORG --shard shard-eu-1
Distribution API

Tenant restore

Rewind one customer to an earlier second while others stay live.

Recover a single organization’s records after a bad import or accidental bulk delete without rolling back the rest of the cluster.

Terminal
napalm placement restore-root acme $ORG \  --to 2026-11-10T17:20:00Z
Recovery and tenant restore