Multi-tenant SaaS & sharding
Shard workspaces, roles and ledgers by customer.
Define organizations, roles, enterprise SSO and credit ledgers in one schema. Required links place each customer’s rows together on one shard.
Read the SaaS guide- Replaces
- Vitess / Citus composite keys
- Oso / Zanzibar authz service
- External billing ledger DB
- Per-table RLS join boilerplate
Roles and shard roots
Domain links carry roles and shard placement.
Required links from Document to Workspace to Organization enforce role checks and keep each customer’s rows on one shard. Records keep plain UUID keys and need no organization_id column.
Multi-tenant B2B SaaSselect Organization { id, name, slug, my_role := assert_single((select .members filter .identity ?= global caller)).role,}order by .nameTransactional invites
Invites and emails commit together.
Insert the Invite row and queue the email job in one statement. Both commit together, so every saved invite sends an email and rollbacks never leak broken links.
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 }Owner guardrails and SSO
Enterprise SSO and owner checks run in SQL.
Bind an organization to an OIDC issuer so verified employees join on sign-in. Query conditions block any update that removes or demotes the last owner.
update Membershipfilter .id = <uuid>$member and (.role != Role.Owner or count((select .org.members filter .role = Role.Owner)) > 1)set { role := <Role>$role }Credit ledgers
Credits and charges commit in one transaction.
Append a ledger entry and debit the customer balance in one statement. Unique idempotency keys block duplicate charges on retry, and schema constraints reject negative balances.
Subscriptions and usage billing# Debit credits atomically; aborts if idempotency_key repeats or balance drops below 0with 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,}Online shard moves
Move or split customer shards online.
Split a busy shard, move a large customer onto a dedicated shard or pin an EU customer to a regional shard from the CLI. Reads and writes stay online during the move.
napalm placement split acme --shard shard-k3m9x2p7qanapalm placement move acme --root $ORG --shard shard-eu-1Per-customer rewind
Rewind one customer’s records in place.
Restore one organization’s root to an earlier second after a bad import or bulk delete. Other customers on the cluster keep reading and writing.
napalm placement restore-root acme $ORG \ --to 2026-11-10T17:20:00Z