Implementing Multi-Tenancy
1. Configuring Tenant Isolation Strategies
| Model | Trade-off |
|---|---|
| Silo (per-tenant infra) | Max isolation, high cost |
| Pool (shared infra, logical sep) | Low cost, noisy-neighbor risk |
| Bridge (per-tenant DB, shared app) | Balanced |
| Row-level (RLS) | Single DB + tenant_id WHERE |
2. Using Tenant Identification
| Source | Example |
|---|---|
| Subdomain | acme.api.example.com |
| Path prefix | /tenants/acme/orders |
| Header | X-Tenant-Id: acme |
| JWT claim | tid or tenant_id |
| API key prefix | acme_sk_... |
3. Implementing Tenant-Based Routing
Example: Subdomain-based routing
map $http_host $tenant {
~^(?<t>[^.]+)\.api\.example\.com$ $t;
default unknown;
}
server {
listen 443 ssl;
server_name ~^[^.]+\.api\.example\.com$;
location / {
proxy_set_header X-Tenant-Id $tenant;
proxy_pass http://backend-$tenant;
}
}
4. Implementing Tenant Authentication
| Pattern | Detail |
|---|---|
| Per-tenant IdP | Each tenant brings own OIDC |
| Federated SSO | SAML, OIDC discovery per tenant |
| Shared IdP + tenant claim | One Auth0/Cognito tenant |
| SCIM provisioning | Sync users from tenant IdP |
5. Setting Up Tenant-Specific Rate Limits
| Tier | Limit |
|---|---|
| Free | 100 req/min |
| Starter | 1000 req/min |
| Pro | 10000 req/min |
| Enterprise | Custom, contract-based |
6. Configuring Tenant Data Segregation
| Strategy | Implementation |
|---|---|
| Schema per tenant | PostgreSQL SET search_path |
| DB per tenant | Connection-string-by-tenant |
| RLS policies | USING (tenant_id = current_setting('app.tid')) |
| Bucket prefix | s3://bucket/tenant/{tid}/... |
| Encryption per tenant | KMS key per tenant |
7. Implementing Tenant-Specific Configurations
| Config | Per-Tenant Override |
|---|---|
| Feature flags | Beta access per tenant |
| Branding | Logo, colors via theme API |
| Limits | Storage quota, seats |
| Webhooks | Tenant-managed endpoints |
| Webhook signing key | Per-tenant secret |
8. Setting Up Tenant Billing and Metering
| Metric | Bill By |
|---|---|
| API calls | Per 1000 requests |
| Data transfer | Per GB |
| Storage | Per GB/month |
| Compute | Per CPU-second |
| Active users | MAU/DAU seats |
9. Configuring Tenant Resource Quotas
| Quota | Enforcement |
|---|---|
| Storage GB | Pre-write check |
| API calls/month | Counter + 429 |
| Concurrent jobs | Semaphore |
| Webhook endpoints | Create-time limit |
| Seats | SCIM provision deny |
10. Using Tenant Migration Strategies
| Phase | Action |
|---|---|
| 1. Snapshot | Backup source tenant |
| 2. Replicate | Stream changes to target |
| 3. Cutover window | Freeze writes, final sync |
| 4. Switch routing | Update tenant→region map |
| 5. Verify | Checksum, smoke test |
| 6. Decommission | Retain backup N days |