name: Tenant-Aware Ops
description: Operational patterns for multi-tenant systems: tenant-safe debugging and monitoring, per-tenant maintenance and migrations, fairness controls, and compliance-aware access
Tenant-Aware Ops
Overview
วิธีการทำ operations (debugging, monitoring, maintenance) ใน multi-tenant environment โดยไม่กระทบ tenants อื่น และมี visibility per tenant
Why This Matters
- Isolation: แก้ปัญหา tenant หนึ่งไม่กระทบอื่น
- Visibility: Debug per tenant ได้
- Fairness: ป้องกัน noisy neighbor
- Compliance: Data access per tenant
Core Concepts
1. Tenant Context Propagation
- ทุก request/job/event ต้องมี
tenantId ใน context (ไม่ใช่แค่ใน UI)
- propagate ไป logs/traces และใช้สำหรับ authz + data scoping
- อ้างอิง:
62-scale-operations/multi-tenancy-saas/SKILL.md
2. Observability per Tenant (อย่างระมัดระวัง)
- logs/traces filter by tenantId ได้ (debug ได้เร็ว)
- metrics labels ต้องคุม cardinality: ใช้
tenantTier/plan แทน tenantId ถ้าจำนวน tenants สูง
3. Tenant-Scoped Operations
- maintenance/backfill/migrations ต้องสามารถ “ทำเฉพาะ tenant” และ pause/resume ได้
- support tooling ต้อง enforce authorization ก่อนอนุญาตดูข้อมูล tenant
4. Noisy Neighbor Controls
- rate limiting, concurrency caps, queue fairness
- quotas (storage/requests/jobs) และ alert เมื่อใกล้เพดาน
Tenant-Aware Logging
// All logs include tenant context
logger.info('User created', {
tenantId: ctx.tenantId,
userId: user.id,
action: 'user.created',
});
// Query logs by tenant
// Loki: {tenantId="tenant-123"}
// CloudWatch: @tenantId="tenant-123"
Tenant-Aware Metrics
// Metrics with tenant label
counter.inc({
tenant: ctx.tenantId,
endpoint: '/api/users',
status: 200,
});
// Dashboard filter by tenant
// Grafana: sum(http_requests_total{tenant="tenant-123"})
Quick Start
- บังคับ
tenantId ใน request/job context และผ่าน authz ตรวจสิทธิ์ก่อนเสมอ
- ทำ logging/tracing ให้ค้นหาได้ด้วย
tenantId แต่ทำ redaction ของ PII/secret
- ออกแบบ tenant-scoped backfills/maintenance ที่หยุด/จำกัดความเร็วได้
- ใส่ guardrails กัน noisy neighbor (rate limits, quotas, fair scheduling)
Tenant-Aware Operations
| Operation |
Tenant-Aware Approach |
| Debug |
Filter logs/traces by tenantId |
| Maintenance |
Scope to specific tenant |
| Migration |
Run per-tenant, monitor impact |
| Scaling |
Per-tenant resource limits |
| Alerts |
Include tenant in alert context |
Noisy Neighbor Prevention
// Rate limiting per tenant
const limiter = new RateLimiter({
keyGenerator: (req) => req.tenantId,
points: 1000,
duration: 60, // per minute
});
// Resource quotas per tenant
const quotas = {
'tenant-123': { maxRequests: 10000, maxStorage: '10GB' },
'tenant-456': { maxRequests: 50000, maxStorage: '50GB' },
};
Production Checklist
Anti-patterns
- Tenant labels everywhere: metric cardinality ระเบิด
- Tenant-blind jobs: background jobs ลืม scope ทำให้ข้อมูลปน
- Support shortcuts: bypass authz เพื่อ “แก้เร็ว” แล้วกลายเป็น data breach
- Global maintenance: ทำ migration/backfill ทั้งหมดพร้อมกันจนกระทบทุก tenant
Integration Points
- Authentication/authorization (tenant mapping, support access workflows)
- Observability stack (tenant filtering, dashboards, alert enrichment)
- Job queues (per-tenant concurrency, priorities, fairness)
- Billing/entitlements (quota enforcement, plan tiers)
Further Reading
1---2name: tenant-aware-ops3description: วิธีการทำ operations (debugging, monitoring, maintenance) ใน multi-tenant environment โดยไม่กระทบ tenants อื่น และมี visibility per tenant4---5
6---
7name: Tenant-Aware Ops
8description: Operational patterns for multi-tenant systems: tenant-safe debugging and monitoring, per-tenant maintenance and migrations, fairness controls, and compliance-aware access
9---
10
11# Tenant-Aware Ops
12
13## Overview
14
15วิธีการทำ operations (debugging, monitoring, maintenance) ใน multi-tenant environment โดยไม่กระทบ tenants อื่น และมี visibility per tenant
16
17## Why This Matters
18
19- **Isolation**: แก้ปัญหา tenant หนึ่งไม่กระทบอื่น
20- **Visibility**: Debug per tenant ได้
21- **Fairness**: ป้องกัน noisy neighbor
22- **Compliance**: Data access per tenant
23
24---
25
26## Core Concepts
27
28### 1. Tenant Context Propagation
29
30- ทุก request/job/event ต้องมี `tenantId` ใน context (ไม่ใช่แค่ใน UI)
31- propagate ไป logs/traces และใช้สำหรับ authz + data scoping
32- อ้างอิง: `62-scale-operations/multi-tenancy-saas/SKILL.md`
33
34### 2. Observability per Tenant (อย่างระมัดระวัง)
35
36- logs/traces filter by tenantId ได้ (debug ได้เร็ว)
37- metrics labels ต้องคุม cardinality: ใช้ `tenantTier`/`plan` แทน `tenantId` ถ้าจำนวน tenants สูง
38
39### 3. Tenant-Scoped Operations
40
41- maintenance/backfill/migrations ต้องสามารถ “ทำเฉพาะ tenant” และ pause/resume ได้
42- support tooling ต้อง enforce authorization ก่อนอนุญาตดูข้อมูล tenant
43
44### 4. Noisy Neighbor Controls
45
46- rate limiting, concurrency caps, queue fairness
47- quotas (storage/requests/jobs) และ alert เมื่อใกล้เพดาน
48
49## Tenant-Aware Logging
50
51```typescript
52// All logs include tenant context
53logger.info('User created', {
54 tenantId: ctx.tenantId,
55 userId: user.id,
56 action: 'user.created',
57});
58
59// Query logs by tenant
60// Loki: {tenantId="tenant-123"}
61// CloudWatch: @tenantId="tenant-123"
62```
63
64## Tenant-Aware Metrics
65
66```typescript
67// Metrics with tenant label
68counter.inc({
69 tenant: ctx.tenantId,
70 endpoint: '/api/users',
71 status: 200,
72});
73
74// Dashboard filter by tenant
75// Grafana: sum(http_requests_total{tenant="tenant-123"})
76```
77
78## Quick Start
79
801. บังคับ `tenantId` ใน request/job context และผ่าน authz ตรวจสิทธิ์ก่อนเสมอ
812. ทำ logging/tracing ให้ค้นหาได้ด้วย `tenantId` แต่ทำ redaction ของ PII/secret
823. ออกแบบ tenant-scoped backfills/maintenance ที่หยุด/จำกัดความเร็วได้
834. ใส่ guardrails กัน noisy neighbor (rate limits, quotas, fair scheduling)
84
85## Tenant-Aware Operations
86
87| Operation | Tenant-Aware Approach |
88|-----------|----------------------|
89| Debug | Filter logs/traces by tenantId |
90| Maintenance | Scope to specific tenant |
91| Migration | Run per-tenant, monitor impact |
92| Scaling | Per-tenant resource limits |
93| Alerts | Include tenant in alert context |
94
95## Noisy Neighbor Prevention
96
97```typescript
98// Rate limiting per tenant
99const limiter = new RateLimiter({
100 keyGenerator: (req) => req.tenantId,
101 points: 1000,
102 duration: 60, // per minute
103});
104
105// Resource quotas per tenant
106const quotas = {
107 'tenant-123': { maxRequests: 10000, maxStorage: '10GB' },
108 'tenant-456': { maxRequests: 50000, maxStorage: '50GB' },
109};
110```
111
112## Production Checklist
113
114- [ ] Tenant isolation verified (data, cache, queues, background jobs)
115- [ ] Support tooling มี audit + authz (ไม่ให้ดูข้อมูล tenant แบบ “ใครก็ได้”)
116- [ ] Logs/traces tenant-filterable และ scrub PII/secret
117- [ ] Metrics ไม่ใช้ tenantId เป็น label ถ้า tenant count สูง
118- [ ] Per-tenant maintenance/migration playbook + ability to pause/resume
119- [ ] Rate limits/quotas/fairness controls พร้อม alerts
120
121## Anti-patterns
122
1231. **Tenant labels everywhere**: metric cardinality ระเบิด
1242. **Tenant-blind jobs**: background jobs ลืม scope ทำให้ข้อมูลปน
1253. **Support shortcuts**: bypass authz เพื่อ “แก้เร็ว” แล้วกลายเป็น data breach
1264. **Global maintenance**: ทำ migration/backfill ทั้งหมดพร้อมกันจนกระทบทุก tenant
127
128## Integration Points
129
130- Authentication/authorization (tenant mapping, support access workflows)
131- Observability stack (tenant filtering, dashboards, alert enrichment)
132- Job queues (per-tenant concurrency, priorities, fairness)
133- Billing/entitlements (quota enforcement, plan tiers)
134
135## Further Reading
136
137- [Multi-Tenant Observability](https://www.datadoghq.com/blog/multi-tenant-monitoring/)
138- [Tenant Isolation Patterns](https://docs.microsoft.com/azure/architecture/guide/multitenant/)