Multi-tenant SaaS architecture: decisions to get right early
Multi-tenant SaaS architecture decisions that are hard to reverse: tenancy models, isolation, identity and SSO, entitlements, audit logs and what can wait.
- Written by
- DevTech365 Engineering
- Published
- Reading time
- 7 minutes
Some SaaS architecture decisions are easy to change later. Others end up in every table, query and integration, and changing them means a migration that touches the whole product. It pays to know which is which before you write the first line of a multi-tenant system.
This article covers the decisions worth getting right early: how tenants share infrastructure, how isolation is enforced and tested, how identity, billing and audit logs are modeled, how to stop one tenant from slowing down the rest, and which decisions can safely wait.
Choose a tenancy model on purpose
Every multi-tenant product has to decide how tenants share storage. There are three common models, and a mature product often uses more than one.
- Shared schema with tenant IDs. All tenants share the same tables, and every row carries a
tenant_id. It is the cheapest to run and the simplest to migrate, but isolation depends on every query filtering correctly. - Schema per tenant. Each tenant gets its own schema in a shared database. Isolation is stronger, but every migration runs once per tenant, and very large schema counts strain backups and database tooling.
- Database per tenant. Each tenant gets a dedicated database. Isolation is strongest and data residency is simpler, at the highest cost and operational effort.
| Concern | Shared schema with tenant IDs | Schema per tenant | Database per tenant |
|---|---|---|---|
| Isolation | Logical, enforced in code and optionally row-level security | Stronger, by schema | Strongest, by database |
| Cost per tenant | Lowest | Moderate | Highest |
| Migrations | Run once | Run once per tenant | Run once per tenant, across servers |
| Onboarding a tenant | Insert rows | Create and migrate a schema | Provision a database |
| Restoring one tenant | Hard | Possible | Straightforward |
| Cross-tenant reporting | Simple queries | Needs aggregation | Needs a separate pipeline |
| Noisy-neighbor risk | Highest | High, resources are shared | Low, depends on placement |
For most new products, a shared schema is the right start. If you use PostgreSQL, add row-level security as a second line of defense. Keep the option to move a large or regulated customer onto dedicated infrastructure later, a hybrid of what are often called the pool and silo models. That move is only practical if tenant_id is on every table from day one, which is the part of this decision you cannot defer.
Make tenant isolation enforceable, then test it
Few bugs hurt a SaaS business more than one customer seeing another customer's data. Isolation should not depend on every developer remembering a filter.
- Resolve the tenant once, at the edge, from the authenticated session or token. If users can switch organizations, check the requested tenant against their memberships instead of trusting a header or URL.
- Carry the tenant context through the request into the data layer, so repositories apply the tenant filter automatically.
- Use row-level security or tenant-scoped connections as a backstop, so a missed filter returns nothing instead of everything.
- Include the tenant in everything that stores data: cache keys, search indexes, object storage paths, queue messages and analytics events.
Then test tenant isolation like the security control it is. Create two tenants in your test suite and, for every endpoint, try to read and modify tenant B's resources with tenant A's credentials. Expect a 404 rather than a 403, so you do not reveal that the resource exists. Generate these tests from your route table so new endpoints are covered automatically.
Model identity and roles with SSO in mind
Separate the person from their access. A user is an identity: an email address and a way to sign in. A membership links that user to an organization with a role. This lets one person belong to several organizations, which consultants, agencies and outside accountants all need.
Create an organization for every account, even if most have a single user today. Retrofitting organizations into a product built around individual accounts is a painful migration.
In code, check permissions rather than role names. A check like can(user, "invoices.approve") survives the day a customer asks for a custom role. A check like role === "admin" does not.
Plan for enterprise single sign-on before you build it. Larger customers will ask for SAML or OpenID Connect, user provisioning through SCIM, and a way to require SSO for everyone on a verified domain. If authentication settings live per organization and identities are linked by a stable provider ID rather than by email alone, SSO becomes a feature instead of a rewrite.
Keep billing and entitlements out of business logic
SaaS pricing changes often: new plans, trials, discounts and negotiated enterprise deals. If plan names are scattered through the code, every pricing change becomes an engineering project.
Split the problem in two. Your billing provider owns subscriptions, invoices and payment methods. Your application owns entitlements: a per-tenant record of which features are enabled and which limits apply. Plans map to default entitlements, sales can add overrides for one customer, and billing webhooks update the record when a subscription changes.
Application code then asks one question: is this tenant entitled to this feature, or within this limit? It never asks which plan the tenant is on. Trials, legacy plans and negotiated enterprise deals become data changes instead of code changes.
If you charge for usage, record usage events as they happen, with the tenant and a timestamp, and aggregate them for billing. Reconstructing usage from logs at the end of a billing period is fragile.
Build in audit logs, export and deletion
Business customers will ask who changed what and when, often during a security review. An audit log answers that, and it is far easier to build in than to reconstruct.
- Record the actor (user, API key or support staff), the action, the target resource, the tenant, the time and the source IP, plus before and after values for sensitive changes.
- Write each entry in the same transaction as the change, or through an outbox, so no event is lost.
- Keep audit logs append-only, separate from application logs and visible to the customer's admins.
Export and deletion depend on the same discipline. Privacy laws such as the GDPR give individuals rights to obtain and erase their personal data, and customers who leave will expect a complete export. Both are only practical if you know everywhere tenant data lives: the primary database, object storage, search indexes, caches, analytics tools and third-party processors. Keep that inventory current, build export and deletion as tested jobs, and decide how deleted data ages out of backups.
Keep one tenant from slowing down the rest
In a shared system, one tenant can degrade service for everyone: a huge import, a report across years of data, a runaway integration. Background jobs are usually where this noisy-neighbor problem shows up first.
- Put the tenant ID in every job payload and cap concurrency per tenant.
- Schedule fairly, for example round-robin across per-tenant queues, instead of one first-in, first-out queue.
- Split large imports and exports into small, idempotent chunks that can be retried individually.
- Run heavy job types on separate worker pools, so they cannot starve time-sensitive work such as emails and webhooks.
You also need to see which tenant is causing the load. Tag logs and traces with the tenant ID from the start, so you can answer which tenant is driving errors, latency or cost. Be careful with metrics: a tenant label on every metric can explode cardinality in a time-series database. Keep full detail in logs and traces, and break out metrics only for your largest tenants.
Decisions that can safely wait
Not everything needs settling on day one. These can usually wait until customers or load demand them:
- Microservices. A modular monolith with clear boundaries serves most early SaaS products well.
- A dedicated database for every tenant. Move individual tenants when a contract requires it.
- Multi-region deployment, unless a launch customer needs data residency.
- The SSO integration itself, as long as the identity model is ready for it.
- Custom roles, white-labeling and custom domains.
- Sharding, read replicas and caching beyond the basics.
What cannot wait is anything that touches every row or every request: tenant IDs, tenant context propagation, the user and membership model, the entitlements check and audit events. These are cheap to add at the start and expensive to retrofit.
Getting the foundations right
A SaaS product does not need an elaborate architecture at launch. It needs a few foundations in the right places, so that growth means configuration and capacity rather than a rewrite. Choose a tenancy model on purpose, make isolation testable, and keep identity, pricing and audit concerns out of your core business logic. DevTech365 builds SaaS products on these foundations, so early shortcuts do not turn into later migrations.
- SaaS development
- multi-tenancy
- software architecture
DevTech365 Engineering
Software engineering team
The engineers who build DevTech365's mobile apps, web platforms, APIs and AI systems, writing about the decisions behind production software.
