Multi-Tenancy, Routing, Data, Cache, Jobs & Isolation Architecture
Multi-tenancy is not only a routing concern.
A tenant boundary affects:
hostname / URL
auth/session context
authorization
database queries
cache keys
background jobs
object storage
rate limits
telemetry
feature flags
billing
operations
A safe architecture makes tenant identity explicit from ingress to persistence.
1. Decide the tenant identifier modelβ
Common models include:
subdomain β acme.example.com
path β example.com/acme/...
custom domain β app.customer.com
account selector β one user switches organisations
The public URL model and the internal tenant ID are not necessarily the same.
Prefer a stable internal tenant identifier for authorization/data/cache contracts.
2. Resolve tenant context once, verify repeatedlyβ
Ingress may resolve:
host/path
β tenant slug/domain mapping
β tenant ID
But a user-provided hostname/path is not proof of authorization.
The secure data/action boundary still verifies:
session belongs to tenant
actor has required role/resource access
resource belongs to tenant
Proxy routing is not the final authorization boundary.
3. Keep tenant identity in server contextβ
A typical server request may derive:
{
userId,
tenantId,
role,
requestId
}
Pass the minimum authoritative values to DAL/commands.
Do not let client state decide the tenant for privileged operations without server verification.
4. Tenant-scoped queries should fail closedβ
Safer conceptual query:
where: {
id: projectId,
tenantId: session.tenantId,
}
More dangerous pattern:
const project = await db.project.findUnique({ where: { id: projectId } })
if (project.tenantId !== tenantId) ...
The second pattern retrieves cross-tenant data before policy evaluation and is easier to misuse elsewhere.
Prefer scoping at the query boundary when possible.
5. Tenant identity belongs in cache identityβ
Conceptually:
project:123
is unsafe if project IDs are not globally isolated in every context.
Use identity that includes relevant dimensions:
tenant:acme:project:123
The same applies to:
reports
permissions
feature configuration
billing summaries
search results
6. Public tenant content still needs explicit identityβ
Even publicly cacheable tenant pages need correct host/slug/cache variation.
A CDN or server cache that ignores host/tenant can serve one tenant's branding/content to another.
Test host and path variation in production-like infrastructure.
7. Custom domains require ownership verificationβ
If customers can bind domains:
claim domain
verify ownership
store canonical mapping
provision TLS/DNS/platform mapping
activate
Do not accept arbitrary host headers as trusted tenant identity.
Operational domain onboarding depends on your hosting/provider architecture.
8. Tenant-aware URLs should have a canonical sourceβ
For a request, decide the authoritative tenant URL identity:
custom domain
canonical subdomain
path-based fallback
Metadata, redirects, callback URLs, emails, and generated links should use a trusted URL builder rather than each feature reconstructing hosts independently.
9. Tenant-aware session semanticsβ
A session can represent:
user identity only
user + active tenant
user + memberships
The right choice depends on UX and authorization freshness.
A membership/role change must invalidate or re-check authority according to the security contract.
Do not assume a long-lived role claim is always current.
10. Cross-tenant admin is a separate capabilityβ
Support staff or platform admins often need broader scope.
Do not implement this as:
if admin β skip tenant filter
Prefer an explicit platform-admin use case with:
separate permission
strong audit trail
explicit tenant selection
reason/context where required
narrow tooling
This reduces accidental privilege expansion.
11. Tenant data modelsβ
Common persistence strategies:
Shared database, shared schemaβ
rows carry tenant_id
Benefits: operational simplicity.
Risk: every query must preserve isolation.
Shared database, separate schemaβ
More isolation, greater operational/schema-management complexity.
Database per tenantβ
Strong isolation and lifecycle control, but connection/migration/analytics complexity rises substantially.
Architecture should choose based on regulatory, scale, blast radius, cost, and operational capability.
12. Tenant ownership belongs in constraints tooβ
Application filters are necessary but database design can add defense in depth.
Examples:
composite keys including tenant_id
unique constraints scoped by tenant
foreign keys preserving tenant ownership
row-level security where architecture supports it
The exact mechanism is database-specific.
13. Background jobs must carry tenant contextβ
A queued payload should contain enough stable identity to enforce scope:
{
tenantId,
jobType,
resourceId,
actorId?,
correlationId
}
The worker should not infer tenant from mutable client-facing data.
14. Scheduled jobs need bounded tenant fan-outβ
Bad:
cron
β load every tenant
β run every expensive operation concurrently
Better:
scheduler
β enqueue bounded tenant work
β worker concurrency controls
β per-tenant isolation/retry
This reduces noisy-neighbour failure.
15. Rate limits may need tenant dimensionsβ
Possible dimensions:
IP
user
API key
tenant
operation
A single abusive tenant should not consume all shared capacity unless the product contract intentionally allows it.
16. Object storage must preserve tenant scopeβ
Conceptual key:
tenants/{tenantId}/projects/{projectId}/file
But key naming alone is not authorization.
Signed upload/download capabilities must also enforce ownership, expiry, content policy, and access conditions.
17. Search/vector indexes are tenant data stores tooβ
When using:
search engine
vector database
analytics warehouse
cache
include tenant isolation in indexing/query contracts.
Do not secure the primary DB while leaving secondary data stores globally queryable.
18. Events need tenant identityβ
Business event example:
{
"type": "project.archived",
"tenantId": "t_123",
"projectId": "p_456",
"eventId": "e_789"
}
Consumers can then preserve tenant scope in downstream systems.
Event payloads should contain minimal necessary data rather than full sensitive records.
19. Feature flags may be tenant-scopedβ
A flag evaluation can depend on:
release environment
tenant
user cohort
plan
region
Evaluate authoritative server-side flags for security/business decisions.
Client flags are suitable for presentation/experienceβnot bypassing server policy.
20. Tenant-aware observabilityβ
Useful telemetry dimensions can include a safe internal tenant identifier.
Be careful with:
tenant names
emails
custom domains
sensitive payloads
Use data-minimised identifiers and retention policy.
For high-cardinality telemetry, control cost and aggregation design.
21. Noisy-neighbour architectureβ
Shared resources include:
DB pool
CPU
queue workers
cache
third-party quotas
search cluster
A large tenant can degrade everyone.
Possible controls:
per-tenant quotas
concurrency limits
fair queues
resource budgets
tiered capacity
backpressure
22. Billing and entitlement should have one ownerβ
Avoid every feature implementing plan checks:
if (plan === 'pro') ...
Prefer an entitlement contract:
canUseFeature({ tenantId, feature: 'advanced-reports' })
This centralizes plan/override/trial logic.
The server still enforces entitlement at privileged operations.
23. Tenant deletion is a distributed lifecycleβ
Deleting a tenant may involve:
primary DB
object storage
cache
search index
queue work
analytics exports
provider resources
backups/retention policy
Model lifecycle explicitly:
active
suspended
pending deletion
deleted/retained according to policy
Do not implement tenant deletion as one SQL statement.
24. Tenant migration between regions/shardsβ
At scale, tenant placement may become dynamic.
A routing directory can map:
tenant β region/shard/database
Moves need compatibility and dual-read/write or controlled cutover strategy.
Do not hardcode tenant placement throughout feature code.
25. Testing multi-tenancyβ
High-value regression matrix:
tenant A user β A resource β allowed
tenant A user β B resource β denied
platform admin β explicit B context β audited allowed
cache warmed by A β B request β never A data
job for A β never mutate B
custom domain A β correct tenant identity
These are architecture tests, not only security unit tests.
26. Senior review questionsβ
Is subdomain routing enough to make an app multi-tenant?β
No. Routing chooses a tenant context; isolation must continue through session authorization, DB queries, cache, jobs, storage, and integrations.
Should tenant ID come from the client request body?β
It may be a requested target, but the server must resolve/verify it against authenticated authority. Client-provided tenant identity is not trusted authorization context.
When should you choose database-per-tenant?β
When isolation, regulatory, lifecycle, scale, or customer requirements justify the operational complexity.
Production checklistβ
- internal tenant identity is stable and explicit
- ingress tenant resolution is not treated as authorization
- DAL queries are tenant-scoped
- cache/CDN identity preserves tenant variation
- custom domains are verified and canonically mapped
- admin cross-tenant capability is explicit/audited
- jobs/events/storage/search carry tenant scope
- noisy-neighbour controls exist for shared resources
- entitlement logic has an authoritative owner
- tenant deletion/migration lifecycle is designed
- cross-tenant negative tests exist at every stateful boundary
Exerciseβ
Design a multi-tenant project-management SaaS supporting:
custom domains
organisation switching
shared database
per-tenant billing
background exports
search
object storage
Map tenant identity through every layer from HTTP request to queue worker.