System Design for Solution Architects: from Whiteboard to Production
บทที่ 10/12

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

⏱ ประมาณ 40 นาที

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
Tracesrequest นี้ช้าที่ 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 errorsretry เฉพาะ 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 flagsdeploy code ปิดไว้ แล้วเปิดตาม usersแยก deploy ออกจาก releaseflags ค้างใน code ต้องตามลบ
traffic split at the load balancer / service mesh v1 100% v1 95%v2 5% v1 50%v2 50% v2 100% metricv1v2error rate0.2%0.2%✓p99 latency180 ms175 ms✓ ถ้า metrics แย่ลง → rollback v2 กลับเป็น 0% ทันที
  1. v1 รับ traffic 100%
  2. Deploy v2 แล้วส่ง traffic ไป 5%
  3. เทียบ error rate และ p99 latency ของ v2 กับ v1: ผ่าน
  4. ขยาย v2 เป็น 50%
  5. v2 รับ 100% (ถ้า metrics แย่ลงเมื่อไร rollback ทันที)
Canary ใช้ metrics จริงเป็นตัวตัดสิน ไม่ใช่ความรู้สึก

Database migrations ต้องเข้ากันได้กับทั้งสอง versions ระหว่าง deploy ใช้ expand/contract: เพิ่ม column ใหม่ (expand) → deploy code ที่เขียนทั้งสองที่ → backfill → ย้าย reads → ลบ column เก่าใน release ถัดไป (contract)

Mapping to AWS, Azure and GCP (ณ ปี 2026)

Building blockAWSAzureGCP
KubernetesEKSAKSGKE
Serverless containersECS on FargateContainer AppsCloud Run
Managed relational DBRDS, AuroraAzure Database for PostgreSQL/MySQLCloud SQL, AlloyDB
NoSQLDynamoDBCosmos DBFirestore, Bigtable
Redis-compatible cacheElastiCacheAzure Managed RedisMemorystore
Messaging / streamingSQS, SNS, MSK (Kafka)Service Bus, Event HubsPub/Sub, Managed Service for Apache Kafka
Object storageS3Blob StorageCloud Storage
CDN / edgeCloudFrontFront DoorCloud CDN
API gatewayAPI GatewayAPI ManagementApigee, API Gateway
Keys & secretsKMS, Secrets ManagerKey VaultCloud KMS, Secret Manager
ObservabilityCloudWatch, X-RayAzure Monitor, Application InsightsCloud 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 ล่างสุดถูกเรียกกี่ครั้ง

Circuit breaker ที่ open อยู่ เมื่อครบเวลา cool-down จะเข้าสู่ state ใด
ต้องการ rollback ได้ทันทีด้วยการสลับ traffic กลับ ควรใช้ strategy ใด
Trace ของ request ขาดเป็นท่อน ๆ ระหว่าง services ต้องแก้อะไร
เปลี่ยนชื่อ column ใน database โดยไม่มี downtime ควรทำอย่างไร