Production-ready
Observability, Resilience & Safe Delivery
Logs, metrics และ traces ด้วย OpenTelemetry, SLI/SLO และ error budget, alerting, resilience patterns (timeouts, retries, circuit breaker, bulkhead), Kubernetes, infrastructure as code, CI/CD, blue-green และ canary พร้อมตาราง map services บน AWS, Azure และ GCP
Design ที่ดีบน whiteboard ยังไม่พอ ระบบต้อง operate ได้: รู้ว่าพังตรงไหนภายในไม่กี่นาที ทนต่อ dependency ที่ล่ม และ deploy ได้ทุกวันโดยไม่ทำให้ users เดือดร้อน บทนี้รวมสามเรื่องนี้ไว้ด้วยกันเพราะมันพึ่งพากัน: ถ้าไม่มี metrics ก็ทำ canary ไม่ได้
Logs, metrics, traces
| Signal | ตอบคำถาม | Practice |
|---|---|---|
| Logs | เกิดอะไรขึ้นใน request นี้ | structured JSON, ใส่ trace_id และ user_id (ไม่ใส่ personal data เกินจำเป็น) |
| Metrics | ระบบโดยรวมเป็นอย่างไร | RED สำหรับ services: Rate, Errors, Duration / USE สำหรับ resources: Utilization, Saturation, Errors |
| Traces | request นี้ช้าที่ service ไหน | OpenTelemetry สร้าง spans ต่อ hop และส่ง trace context ไปกับทุก call (header traceparent) |
Trace ที่ขาด context propagation แม้แค่ service เดียว จะขาดเป็นท่อน ๆ ทันที ให้ใช้ OpenTelemetry SDK/agent ทุก service ตั้งแต่วันแรก
SLI, SLO and error budgets
- SLI: ตัวชี้วัด เช่น สัดส่วน requests ที่สำเร็จและเร็วกว่า 300 ms
- SLO: เป้าหมาย เช่น 99.9% ต่อ 30 วัน
- Error budget = 100% − SLO เช่น 0.1% ของ 30 วัน = 43.2 นาที หรือถ้ามี 10 ล้าน requests ก็ fail ได้ 10,000 requests
Error budget ใช้ตัดสินใจ: budget เหลือ → deploy features ได้เต็มที่ budget ใกล้หมด → ชะลอ releases แล้วแก้ reliability ก่อน
Alerting: alert ที่ symptoms ที่ users รู้สึก (error rate, latency ตาม SLO) ไม่ใช่ทุก CPU spike และใช้ burn-rate alerts (budget หมดเร็วกว่าปกติกี่เท่า) แทน threshold ตายตัว เพื่อลด alert ที่ปลุกคนกลางดึกโดยไม่จำเป็น
Resilience patterns
| Pattern | ใช้ทำอะไร | ระวัง |
|---|---|---|
| Timeout | ไม่รอ dependency ไม่สิ้นสุด | ตั้งให้ timeout รวมทั้ง call chain ไม่เกิน budget ของ request (deadline propagation) |
| Retry + backoff + jitter | ผ่าน transient errors | retry เฉพาะ idempotent calls และระวัง retry amplification |
| Circuit breaker | หยุดเรียก dependency ที่พัง | states: closed (ปกติ) → open (ตอบ fallback ทันที) → half-open (ลองส่ง requests บางส่วน) |
| Bulkhead | กันไม่ให้ dependency เดียวกิน resources ทั้งหมด | แยก connection/thread pools ต่อ dependency |
| Load shedding | ปฏิเสธบาง requests เมื่อ overload | ตัดงานสำคัญน้อยก่อน (เช่น recommendations) |
| Graceful degradation | ระบบยังใช้ได้บางส่วน | เช่น ปิด “สินค้าแนะนำ” แต่ checkout ยังทำงาน |
Retry amplification: ถ้า 3 layers ต่อกันและแต่ละ layer ลอง 3 ครั้ง request เดียวอาจกลายเป็น 3 × 3 × 3 = 27 calls ที่ layer ล่างสุด ซึ่งทำให้ระบบที่กำลังป่วยล่มสนิท จึงควร retry ที่ layer เดียว (มักเป็นชั้นนอกสุด) และใช้ retry budget
Delivery: containers, Kubernetes, IaC, CI/CD
- Containers ทำให้ artifact เดียวกันวิ่งได้ทุก environment
- Kubernetes: Deployment (จำนวน replicas), Service (load balancing ภายใน), HorizontalPodAutoscaler (scale ตาม metrics), readiness/liveness probes (ไม่ส่ง traffic ให้ pod ที่ยังไม่พร้อม และ restart pod ที่ค้าง)
- Infrastructure as code (Terraform หรือเครื่องมือของแต่ละ cloud): environments ทุกชุดสร้างจาก code ที่ review ได้ ไม่มีการคลิกใน console
- CI/CD pipeline: build → unit tests → security scans (dependencies, containers, secrets) → deploy staging → integration tests → progressive delivery ไป production
Deployment strategies
| Strategy | วิธี | ข้อดี | ข้อเสีย |
|---|---|---|---|
| Rolling | ทยอยแทน pods ทีละส่วน | ไม่ต้องมี capacity เพิ่ม | rollback ช้า, มีสอง versions พร้อมกัน |
| Blue-green | มีสองชุดเต็ม สลับ traffic ทีเดียว | rollback ทันที (สลับกลับ) | ต้องมี capacity สองเท่าช่วง deploy |
| Canary | ส่ง traffic ส่วนน้อยไป version ใหม่ก่อน | เสี่ยงต่ำ วัดผลกับ traffic จริง | ต้องมี metrics และ automation ที่ดี |
| Feature flags | deploy code ปิดไว้ แล้วเปิดตาม users | แยก deploy ออกจาก release | flags ค้างใน code ต้องตามลบ |
- v1 รับ traffic 100%
- Deploy v2 แล้วส่ง traffic ไป 5%
- เทียบ error rate และ p99 latency ของ v2 กับ v1: ผ่าน
- ขยาย v2 เป็น 50%
- v2 รับ 100% (ถ้า metrics แย่ลงเมื่อไร rollback ทันที)
Database migrations ต้องเข้ากันได้กับทั้งสอง versions ระหว่าง deploy ใช้ expand/contract: เพิ่ม column ใหม่ (expand) → deploy code ที่เขียนทั้งสองที่ → backfill → ย้าย reads → ลบ column เก่าใน release ถัดไป (contract)
Mapping to AWS, Azure and GCP (ณ ปี 2026)
| Building block | AWS | Azure | GCP |
|---|---|---|---|
| Kubernetes | EKS | AKS | GKE |
| Serverless containers | ECS on Fargate | Container Apps | Cloud Run |
| Managed relational DB | RDS, Aurora | Azure Database for PostgreSQL/MySQL | Cloud SQL, AlloyDB |
| NoSQL | DynamoDB | Cosmos DB | Firestore, Bigtable |
| Redis-compatible cache | ElastiCache | Azure Managed Redis | Memorystore |
| Messaging / streaming | SQS, SNS, MSK (Kafka) | Service Bus, Event Hubs | Pub/Sub, Managed Service for Apache Kafka |
| Object storage | S3 | Blob Storage | Cloud Storage |
| CDN / edge | CloudFront | Front Door | Cloud CDN |
| API gateway | API Gateway | API Management | Apigee, API Gateway |
| Keys & secrets | KMS, Secrets Manager | Key Vault | Cloud KMS, Secret Manager |
| Observability | CloudWatch, X-Ray | Azure Monitor, Application Insights | Cloud Monitoring, Cloud Trace |
ชื่อ services เปลี่ยนบ่อย ให้ตรวจ documentation ปัจจุบันก่อนใส่ใน proposal ส่วน principles ทั้งหมดในบทนี้ใช้ได้ทุก cloud
แบบทดสอบ
SLO availability 99.9% ระบบรับ 20 ล้าน requests ต่อเดือน fail ได้กี่ requests ก่อน budget หมด
Call chain มี 3 layers แต่ละ layer พยายามเรียก layer ถัดไปได้สูงสุด 3 ครั้ง (รวมครั้งแรก) ในกรณีแย่ที่สุด request เดียวจาก user ทำให้ layer ล่างสุดถูกเรียกกี่ครั้ง