bengaluru

ar.

nov 2026 · 8 min read

If I had to build Supabase today

Supabase made Postgres approachable. The design I'd build makes the multi-tenant version hard to get wrong.

Ambuj Rajdatabases, security, architecture

Supabase is honestly the fastest way I know to get a real Postgres backend. A database, an API, sign-in and storage, all before lunch. But we've all seen what happens next: the app ships, the team grows, and one day someone asks the awkward question. Can customer A see customer B's data? I've spent the last few months designing a backend in this space, and almost every decision I'd make differently starts from that one question.

The promise that keeps getting broken

Here's the thing about most products built on a backend like this: they're multi-tenant. A team signs up, invites a few people, and from that moment exactly one promise matters. One customer never sees another customer's data.

Supabase gives you the tools for that, Postgres row-level security, but not the shape. Organizations, memberships and roles are yours to build, which is why so many boilerplates sell exactly that. And when the safe path is optional, it gets skipped. In 2025, CVE-2025-48757 described apps generated by an AI builder that shipped with missing or weak row-level security; a researcher found 303 exposed endpoints across 170 projects. The database could have stopped every one of them. Nothing made it.

So, my first rule: tenancy is the default, not an add-on.

Tenancy is one switch

Most projects start small: one user, one app, no teams. That's how a project starts here too. When it needs teams, one line in a config file turns organizations on, and marking a table as tenant data does the rest.

[tenancy]
mode  = "organizations"
roles = ["owner", "admin", "member"]

[tables.invoices]
tenant      = true
write_roles = ["owner", "admin"]

That adds an org_id column, an index, and policies for every operation, with WITH CHECK on writes so a row can't be created in, or moved into, another organization.

The database decides, from the database

If I could keep only one decision from this whole design, it'd be this one. Membership and roles live in a platform-owned schema inside the project's own Postgres, and the policies read them from there:

using (org_id in (select platform.my_org_ids()))

The access token carries one fact authoritatively: who you are. Organization and role can appear in the token as hints for the UI, but a linter refuses any policy that reads them. Tokens are a snapshot. If an admin removes someone from a team, that should be true on the very next query, not when a token expires.

The cost is one indexed membership lookup per statement, cached for the statement by Postgres. Supabase's own docs recommend the same select wrapping for exactly that reason.

Refuse to deploy the unsafe thing

Good defaults are great, right up until a migration quietly undoes them. And one will. Someone adds a table late on a Friday and forgets the policy. So deploys have rules:

  • every table in an exposed schema needs FORCE ROW LEVEL SECURITY and at least one policy, or the deploy is refused
  • a linter flags using (true), policies granted to everyone, and tenant tables whose policies ignore org_id
  • generated cross-tenant tests run on every deploy, in a shadow database built from the same migrations, never against live data
Deploy gate: a migration is applied to a shadow database built from the same migrations, never live data. Three checks then run in order: RLS coverage, which needs FORCE row-level security and at least one policy on every exposed table; a policy linter, which flags using (true), grants to everyone and policies without org_id; and cross-tenant tests, which sign in as one organization and try to read another's rows. If all pass, the migration reaches the unit. If any fails, the deploy is refused, nothing changes on the unit, and you see the rule that failed.
Every check runs against a copy. One failure and nothing ships.

The last one is the one I care about most. A policy that reads well in review can still leak; a test that signs in as a member of one organization and tries to read another's rows can't be argued with.

One unit per environment

So where does all of this actually run? Every environment, production or test, gets its own unit: a dedicated Postgres, PostgREST in front of it, a storage service and a small tenancy service, in its own Kubernetes namespace. Network policy forbids traffic between units. Test environments pause when idle, so isolation doesn't have to mean expensive.

Architecture: apps and AI agents reach a shared gateway, which checks the token and rate limits before routing into an isolated unit per environment. Inside the unit, PostgREST, a storage service and a tenancy service sit in front of Postgres, which makes the final access decision with row-level security, and a jobs and functions service runs queues, cron, database triggers and signed webhooks. Keycloak shards and Valkey are shared and keyed by environment, and a Kubernetes control plane provisions each unit.
One request: the gateway checks it, routes it, and Postgres has the final word.

A few things are shared because running one per project would be wasteful: the gateway fleet, the auth servers and the rate-limit store. The rule for every one of them is the same. They're keyed by environment, and nothing shared may read one environment's state while serving another.

The gateway is a first layer, never the only one

Let's follow one request in. It arrives at an environment's hostname. The gateway resolves the environment, verifies the token against that environment's signing keys, applies rate limits, and routes it. A token from one environment is rejected by another by construction, because the issuer and audience differ.

Rate limits are a token bucket (GCRA) with counters in Valkey, keyed by IP, user, API key or a custom claim. If Valkey goes down, the gateway falls back to an in-memory limiter and alerts, rather than letting everything through or blocking everyone.

And then? Postgres makes the final decision anyway. Gateways can be wrong, and when they are, row-level security is still there.

Auth you never have to see

Nobody enjoys wiring up auth, so builders never have to. Under the hood it's Keycloak, one realm per environment, but all they see is hosted, branded sign-in pages and a config file. Authorization Code with PKCE, passkeys, social sign-in that links accounts only when the provider verified the email, and per-organization SSO. Server keys have their own prefix, and the browser SDK refuses to start with one, because the most common way a secret leaks is someone pasting it into the frontend.

No second backend

Here's a pattern we've all seen: the backend is managed, and you still end up running a little server of your own for emails, webhooks and cron jobs. That's only half the job done. So server-side logic lives in each environment too, in two layers.

First an event backbone: a Postgres-backed queue per environment (FOR UPDATE SKIP LOCKED, retries with backoff, a dead-letter list), cron schedules declared in config, database-event triggers that reuse the same outbox the platform already depends on, and signed outgoing webhooks with a delivery log. Most of what people write a backend for, sending an email when a row changes or calling another service on a schedule, needs no code at all.

Then TypeScript functions, sandboxed, triggered over HTTP or by the backbone. They run either as the user who called them or as a scoped role with its own grants and policies. Never as a role that bypasses row-level security, because a function is the easiest place to forget the rule everything else was built to keep.

Built for agents as much as people

Let's be honest: a lot of the building on platforms like this is now done by AI agents working beside people. So everything is declared in a config file that lives in the builder's repo, the CLI speaks JSON, and there's an MCP server. An agent can read the whole setup, change it, and deploy it through the same safety checks a person goes through.

What I'd leave out

What about realtime, direct database connection strings, image transforms? All useful. All later. A first version that keeps one promise completely is worth more than one that keeps ten partly.

Supabase made Postgres approachable, and that's a big deal. If I were building it today, I'd spend the effort making the multi-tenant version hard to get wrong.