Multi-Tenant SaaS Architecture: A Practical Guide
How to design multi-tenant SaaS architecture in practice: choosing an isolation model, enforcing tenant scope in middleware, and handling roles, billing and per-tenant features.
Getting multi-tenant SaaS architecture right early saves you from the worst kind of bug: one customer seeing another customer's data. It also decides how easily you can add billing plans, roles and per-tenant features later.
I have built and worked on multi-tenant systems on both MongoDB and Postgres: Client Mitra for photography studios, the Antoc real-estate CRM, and the HNEFA gaming cafe POS. Here is the practical playbook I follow, without the theory-heavy diagrams.
TL;DR
Start with a shared database and a tenant ID on every tenant-owned record. Enforce tenant scope in one central layer (middleware in a Node/MongoDB stack, row-level security in Postgres), never by remembering to add a filter. Attach roles, plans and feature flags to the tenant. Then audit the places isolation usually leaks: background jobs, file storage, caches and admin tools.
key takeaways
- For most early-stage SaaS products, a shared database with a tenant ID on every row is the right default.
- Tenant scoping must be enforced in one central place, such as middleware or database policies, never remembered per query.
- Roles, billing plans and feature flags all hang off the tenant, so model the tenant entity carefully from day one.
- Background jobs, file storage and caches are where tenant isolation most often leaks.
Choosing an isolation model
There are three common models. The right one depends on your customers, not on what sounds most secure.
| Model | How it works | Best for | Main cost |
|---|---|---|---|
| Shared database, shared schema | Every row carries a tenantId |
Most SMB SaaS, early stage | Discipline in enforcing scope |
| Schema per tenant | Each tenant gets its own schema in one database | Moderate tenant counts with custom fields | Migrations multiply per tenant |
| Database per tenant | Separate database per customer | Regulated or very large enterprise tenants | Operational overhead, connection limits |
For Client Mitra, Antoc and HNEFA, the shared model was the right call. Hundreds of small businesses with similar data shapes fit it perfectly, and one migration updates everyone.
If you later land an enterprise customer who needs dedicated infrastructure, you can carve them out. Starting with database-per-tenant "just in case" mostly buys you pain.
Enforcing tenant scope centrally
The rule I follow: no developer should ever have to remember to add tenantId to a query. If they have to remember, someone eventually forgets.
In a Node and MongoDB stack
The request flow looks like this:
- Auth middleware verifies the JWT and loads the user.
- Tenant middleware resolves the active tenant from the token or an organization header and verifies the user's membership.
- The tenant ID is attached to the request context.
- Data access goes through helpers or Mongoose plugins that inject
{ tenantId }into every find, update and delete, and stamp it on every insert. - Role checks run after tenant resolution, so permissions are always evaluated within a tenant.
Add a compound index that starts with tenantId on every hot collection. It keeps queries fast and makes the access pattern explicit. The MongoDB indexing docs cover compound index ordering well.
In Postgres
Postgres gives you a stronger option: row-level security. Policies attached to each table filter rows by the current tenant, so even a buggy query cannot cross tenants. I cover this in detail in row-level security for multi-tenant apps.
Modeling tenants, users and memberships
The most common modeling mistake is putting tenantId and role directly on the user. It works until someone needs access to two organizations.
A cleaner model:
- Tenant: name, plan, subscription status, settings, branding (logo, invoice details).
- User: identity only, such as email, phone, auth providers.
- Membership:
userId,tenantId,role,permissions,status.
Antoc supports multi-business organization switching for exactly this reason. Owners who run more than one business switch context without logging out, and every request carries the active tenant.
Plans, billing and feature locks
Once tenants exist, billing attaches to them. A plan defines limits (seats, records, messages) and which features are unlocked. Every gated feature should check the plan on the server, not just hide a button in the UI.
Antoc uses plan-based feature locking so higher tiers expose more modules. Client Mitra ties subscriptions to the tenant whether they come from Razorpay on the web or Apple in-app purchase on iOS, with server-side receipt verification deciding the tenant's status.
Where isolation leaks
- Cron jobs and queues. A reminder job that loads "all pending tasks" must still process them per tenant and never mix recipients.
- File storage. Prefix object keys by tenant and use time-limited signed URLs rather than public links.
- Caches. Include the tenant ID in every cache key, including client-side query caches.
- Search and exports. CSV exports and reports are frequent offenders because they are written as one-off queries.
- Admin panels. Super-admin tools bypass tenant scope by design, so give them separate auth and logging.
How I built this in practice
Client Mitra is a multi-tenant CRM I built solo for photography studios. Tenant isolation lives in Express middleware: after JWT verification, the tenant is resolved and every data access across the 15 Mongoose models is scoped to it. Roles (owner, manager, staff) are evaluated inside the tenant, which is how staff can manage bookings without seeing financials.
Cron reminders via node-cron iterate per tenant. Invoices generated with PDFKit pull the tenant's logo and UPI details, so branding never crosses accounts. On the frontend, TanStack Query's persisted cache is keyed per tenant and cleared on logout or organization switch.
On HNEFA, built with the Exlaso team, the same goals are met differently: Supabase Postgres with row-level security per tenant, so isolation is enforced by the database itself.
Common mistakes
- Storing role and tenant on the user record instead of a membership.
- Filtering by tenant in the UI layer and trusting the client.
- Forgetting tenant scope in cron jobs, exports and webhooks.
- Choosing database-per-tenant before there is a customer who needs it.
- Not writing tests that try to read another tenant's data on purpose.
keep reading
FAQ
What is multi-tenant SaaS architecture?+
It is a design where one running application and usually one database serve many customer organizations, called tenants. Each tenant sees only its own data, users and settings, even though the infrastructure is shared.
Should each tenant get its own database?+
Usually not at the start. Database-per-tenant adds operational overhead for migrations, backups and connections. It makes sense for regulated enterprise customers or when a single tenant is very large.
How do you prevent one tenant from seeing another tenant's data?+
Enforce scoping centrally. In MongoDB I use middleware that injects the tenant ID into every query; in Postgres, row-level security policies do this at the database level. Then test it with automated cross-tenant access checks.
How do you handle users who belong to multiple organizations?+
Separate the user from the membership. A user can have memberships in several tenants, each with its own role, and the active tenant is chosen at login or via an organization switcher.
Does multi-tenancy affect billing?+
Yes. Subscriptions, plan limits and feature locks attach to the tenant, not the individual user. Every gated feature should check the tenant's current plan on the server.
Devang Gandhi
AI & full stack developer at Passdn, building CRMs, SaaS, AI agents and Flutter apps for clients in the US, UK, Australia, UAE and India. More about me