DevHuzaifa's Portfolio
•3 min read•Updated Oct 31, 2026

Designing a Multi-Tenant SaaS Application

Design tenant isolation, tenant-scoped RBAC, and database architecture for SaaS. Carry tenant context through queries, jobs, caches, files, and operational workflows.

#Django#System Design#Code Patterns#RBAC#SaaS

A multi-tenant SaaS application serves several organizations from one product. The difficult part is keeping every data path aligned with the active organization. A filtered list can look correct while a detail endpoint, export job, or cached response leaks another tenant’s record.

A SaaS request authenticates a principal, selects a tenant, checks tenant membership, and scopes data access to that tenant.

Treat tenant identity as part of the operation’s contract. Resolve the authenticated principal, verify membership in the requested organization, authorize the action, then load resources within that tenant. A subdomain or request header can select a tenant; it cannot establish permission to use it.

Step 1: Choose a Database Isolation Model

Shared tables with tenant_id make pooled operations and migrations straightforward, but every access path needs tenant scoping. Separate schemas offer a namespace boundary while adding routing, migration, and connection-state complexity. Separate databases provide a stronger operational boundary for backup, residency, or noisy-neighbor requirements, at the cost of fleet management and cross-tenant reporting.

Choose from the requirements you actually have: restore granularity, tenant count, data volume, deployment model, and operational capacity. A hybrid can place large customers in dedicated databases, but it also creates more than one deployment path to test and operate.

Step 2: Make Shared-Table Access Explicit

python
# Request identity and tenant membership have already been verified.
def get_project(*, tenant_id, project_id):
    return Project.objects.get(
        tenant_id=tenant_id,
        pk=project_id,
    )

# Unsafe: the identifier alone does not enforce tenant ownership.
# Project.objects.get(pk=project_id)

Scope writes and relationship lookups as carefully as reads. When assigning an invoice to a project, verify that both belong to the same tenant. A uniqueness constraint on tenant_id and an external identifier prevents same-tenant duplicates; it does not automatically enforce every cross-table tenant relationship.

Step 3: Scope RBAC to Membership

python
# A user can have a different role in each tenant.
membership = Membership.objects.get(
    tenant_id=tenant_id,
    user_id=request.user.pk,
    is_active=True,
)
authorize(membership, action='project:update', resource=project)
# authorize is the application's policy function.

Avoid a single global user.role for organization-specific authority. Platform administration and tenant administration are different scopes. Make support access explicit, time-bounded where required, and auditable. Test permission checks with two tenants that happen to have similar records.

Related: Handling Complex Role-Based UI Permissions

Defense in Depth with Row-Level Security

PostgreSQL row-level security can enforce row visibility and write policies at the database boundary. Once enabled, a table without an applicable policy defaults to denying normal row access. Table owners, superusers, and roles with BYPASSRLS require special attention because they can bypass ordinary enforcement.

PostgreSQL row security policies

Use a restricted application database role and set tenant context safely for each transaction. Connection pooling makes leaked session state particularly dangerous. Test a missing tenant context, an invalid context, and an attempted cross-tenant write using the real application role; an administrative connection can hide policy mistakes.

The Boundaries Outside the Database

Include tenant identity in cache keys, object-storage paths, job payloads, and export manifests. A worker must re-establish the tenant context rather than inherit a web request’s thread-local assumptions. Recheck authorization for downloadable files; an unguessable URL is not an access policy.

Monitor tenant-specific failures and resource consumption. Exercise backup restoration, tenant deletion, and a migration across all database placements before those operations become emergencies. A tenant architecture is complete only when the operational paths respect the same isolation as the application queries.

OWASP authorization guidance