Post

Designing Multi-Tenant SaaS Architecture

Designing Multi-Tenant SaaS Architecture

Multi-tenant systems host multiple customers on shared infrastructure. The core challenge is balancing efficiency with strict tenant isolation and predictable performance.

Tenant Isolation Models

  • Shared database, shared schema: lowest cost, highest risk.
  • Shared database, separate schemas: better isolation, moderate cost.
  • Separate databases: strongest isolation, highest operational overhead.

Key Design Concerns

Identity and Access

Every request must be scoped to a tenant. Use tokens that carry tenant identifiers and enforce them at the data access layer.

Noisy Neighbor Control

Enforce per-tenant rate limits, concurrency limits, and quotas. Use resource tagging to ensure fairness.

Data Partitioning

Sharding by tenant ID improves performance and isolates hot tenants. Use consistent hashing to minimize rebalancing.

Spring Boot Example: Tenant Context Propagation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Component
public class TenantFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain filterChain) throws ServletException, IOException {
        String tenantId = request.getHeader("X-Tenant-Id");
        TenantContext.setTenantId(tenantId);
        try {
            filterChain.doFilter(request, response);
        } finally {
            TenantContext.clear();
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class TenantContext {
    private static final ThreadLocal<String> CURRENT = new ThreadLocal<>();

    public static void setTenantId(String tenantId) {
        CURRENT.set(tenantId);
    }

    public static String getTenantId() {
        return CURRENT.get();
    }

    public static void clear() {
        CURRENT.remove();
    }
}

Operational Practices

  • Track per-tenant usage metrics.
  • Run tenant-specific backups and restores.
  • Provide data residency controls for regulated tenants.

Summary

Multi-tenant SaaS architecture requires strong isolation controls, transparent resource governance, and operational tooling to keep tenants safe and predictable.

This post is licensed under CC BY 4.0 by the author.