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

Capstone Project

Capstone: a QR Payment Service

Design case แบบ end-to-end ของระบบจ่ายเงินด้วย QR สำหรับ e-wallet ในไทย ตั้งแต่ requirements, estimation, architecture, idempotency, ledger, reconciliation, security, observability และ rollout จนถึง ADRs และ proposal section ภาษาไทยและอังกฤษ พร้อม rubric

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

Capstone นี้รวมทุกบทเข้าด้วยกันเป็นงานแบบที่ solution architect ทำจริงใน pursuit หนึ่งงาน: ลูกค้าคือ e-wallet ในไทยที่ต้องการ ระบบจ่ายเงินด้วย QR ใหม่ ให้ทำตาม method 4 steps จากบทที่ 1 (The Solution Architect Role & a Design Method) แล้วส่ง deliverables ตามท้ายบท

The brief

  • Users สแกน Thai QR ของร้านค้าเพื่อจ่ายจาก wallet balance และร้านค้าแสดง dynamic QR ที่มียอดเงินฝังอยู่
  • Payments ที่ปลายทางอยู่ต่างธนาคารหรือ e-wallet อื่น ต้องผ่าน national switch (ระบบกลางของการโอนและจ่ายเงินระหว่างผู้ให้บริการ)
  • ร้านค้าต้องได้รับ notification ภายในไม่กี่วินาที และมี refunds
  • ช่วง payday, ตลาดนัด และเทศกาล traffic สูงกว่าปกติมาก

Step 1: Requirements & NFRs

NFRTarget (assumption)
Correctnessไม่มีการตัดเงินซ้ำ และเงินไม่หายแม้ระบบใดล่ม
Latencyp99 < 1 วินาทีสำหรับส่วนของเรา (ไม่รวมเวลาของ switch)
Availability99.95% ต่อเดือน
DurabilityLedger: RPO = 0 เมื่อ availability zone ล่ม (sync replica ภายใน region), RPO ≤ 5 นาทีเมื่อทั้ง region ล่ม (async ข้าม region); RTO ≤ 30 นาที
SecurityPDPA, encryption ทุก layer, MFA/biometric สำหรับยอดสูง
Auditเก็บประวัติธุรกรรมตามระยะเวลาที่ regulation กำหนด (ต้องตรวจฉบับปัจจุบัน)

Step 2: Estimate

  • 3 ล้าน DAU × 2 payments/วัน = 6 ล้าน payments/วัน → 6 × 10⁶ / 10⁵ = 60 TPS เฉลี่ย
  • Peak (เที่ยงวัน payday) 10 เท่า → 600 TPS ระดับนี้ relational database ที่ design ดีรับได้ ยังไม่ต้อง shard ledger แต่ต้องวางแผนไว้
  • Ledger: payment ละ 2 entries (debit/credit) รวมประมาณ 1 KB → 6 GB/วัน → ประมาณ 2.2 TB/ปี ก่อน replication ต้องวาง partitioning ตามเวลาและ archive

Step 3: High-level design

Mobile app เรียก API gateway (auth, rate limit) ไปยัง payment service ที่ตรวจ idempotency key แล้วบันทึก debit/credit ลง ledger DB แบบ double-entry และส่งคำสั่งไป national switch ผ่าน QR/interbank ส่วน events จาก ledger ผ่าน event broker ไปยัง notification service และ reconciliation เทียบ settlement file จาก switch กับ ledger ทุกวัน
Reference design: synchronous เฉพาะเส้นทางของเงิน ที่เหลือเป็น async

Core API

POST /v1/payments            Idempotency-Key: <uuid>
  { "qr_payload": "...", "amount": "120.00", "currency": "THB" }
→ 201 { "payment_id": "pay_8f2", "status": "SUCCEEDED" }
→ 202 { "payment_id": "pay_8f2", "status": "PENDING" }   (switch ยังไม่ตอบ)
GET  /v1/payments/{payment_id}

จำนวนเงินส่งเป็น decimal string (หรือเป็นสตางค์แบบ integer เช่น 12000) ไม่ใช่ JSON number เพราะ floating point ปัดเศษผิดได้ และเงินผิดแม้หนึ่งสตางค์ไม่ได้

Step 4: Deep dives

Idempotency และ state machine (บทที่ 8 (API Design))

  • App สร้าง Idempotency-Key ต่อหนึ่งความตั้งใจจ่าย payment service บันทึก key ด้วย unique constraint
  • Payment มี states: CREATED → PENDING → SUCCEEDED | FAILED และเมื่อไม่รู้ผล PENDING → UNKNOWN → SUCCEEDED | FAILED (UNKNOWN ไม่ใช่สถานะสุดท้าย ต้อง resolve ด้วย inquiry หรือ reconciliation)
  • ถ้า FAILED หลังตัดเงินจาก wallet ไปแล้ว ให้ลง entry กลับรายการ จากบัญชีพักของ switch คืน wallet

Timeout กับ switch: ห้าม retry ตรง ๆ

  • ถ้า switch ไม่ตอบภายใน timeout เราไม่รู้ว่าเงินโอนแล้วหรือยัง ให้ตั้งสถานะ UNKNOWN แล้ว ถามสถานะ (inquiry) จาก switch จนได้คำตอบ หรือรอ reconciliation
  • การส่งคำสั่งจ่ายใหม่โดยไม่รู้ผลของครั้งแรก = เสี่ยงจ่ายซ้ำ

Double-entry ledger (บทที่ 5 (Data Stores, Indexing & Transactions))

  • ทุก transaction มี entries ที่ ผลรวมเป็นศูนย์ (debit wallet ผู้ซื้อ 120 = credit บัญชีพักของ switch 120)
  • Append-only ไม่ update หรือ delete entries แก้ผิดด้วย entry กลับรายการ
  • ยอดคงเหลือ = ผลรวม entries (เก็บ materialized balance พร้อม atomic update หรือ version check กัน lost update)
  • เลือก consistency (CP) สำหรับ ledger (บทที่ 4 (Consistency, CAP & Replication)): ใช้ sync replica ภายใน region เพื่อ RPO = 0 เมื่อ zone ล่ม ส่วน DR ข้าม region เป็น async (RPO ไม่ใช่ศูนย์) ซึ่งเป็น trade-off ที่ควรเขียนเป็น ADR

Async ส่วนที่เหลือ (บทที่ 7 (Messaging & Event-driven Design))

  • Ledger เขียน event ผ่าน outbox ไป event broker → notification, loyalty points, analytics
  • Notification ล้มเหลวต้อง ไม่ ทำให้ payment rollback

Reconciliation

  • ทุกวันเทียบ settlement file จาก switch กับ ledger รายการที่ไม่ตรงกัน (เช่น UNKNOWN ที่ switch บอกว่าสำเร็จ) ถูกปรับด้วย entries ใหม่และรายงานให้ทีม operations

Step 5: Security, compliance and operations

  • Auth: OAuth Authorization Code + PKCE, device binding, step-up authentication (biometric) เมื่อยอดสูงหรือ risk score สูง (บทที่ 9 (Security by Design))
  • mTLS กับ switch, keys ใน HSM/KMS, ข้อมูลส่วนบุคคลเข้ารหัสระดับ field
  • Fraud & abuse: velocity limits ต่อ user/device, rate limiting ที่ gateway, rules + ML scoring แบบ async ที่ไม่บล็อก payment path เกิน budget
  • Observability: SLOs ของ success rate และ latency, metrics แยกตาม switch response code, alert เมื่อ UNKNOWN สูงผิดปกติ
  • Rollout: เปิดให้ร้านค้ากลุ่มเล็กก่อนด้วย feature flags แล้วขยายแบบ canary (บทที่ 10 (Observability, Resilience & Safe Delivery))

Deliverables

  1. Design document ตาม steps 1–5 พร้อม architecture diagram (C4 ระดับ Container)
  2. Estimates: ตาราง assumptions + QPS, storage และ capacity ของ components หลัก
  3. ADRs 3 เรื่อง เช่น
    • Relational database สำหรับ ledger แทน NoSQL
    • Orchestrated saga สำหรับ refunds
    • Inquiry + reconciliation แทนการ retry เมื่อ switch timeout
  4. Risk register อย่างน้อย 5 risks พร้อม likelihood, impact และ mitigation
  5. Proposal section 1 หน้า ทั้งภาษาไทยและอังกฤษ สำหรับผู้บริหารที่ไม่ใช่ technical (ตัวอย่างย่อหน้าแรกด้านล่าง)

Proposed solution (EN): We propose a payment platform that moves money synchronously only where it must and handles everything else asynchronously. Every payment is protected by an idempotency key and recorded in a double-entry ledger, so customers are never charged twice and every baht can be traced. A daily reconciliation with the national switch catches every mismatch, fixes it with traceable entries and reports it to operations.

แนวทางที่เสนอ (TH): เราเสนอ payment platform ที่ทำงานแบบ synchronous เฉพาะเส้นทางที่เงินเคลื่อนที่ ส่วนงานอื่นทำแบบ asynchronous ทุกรายการมี idempotency key ป้องกันการตัดเงินซ้ำ และบันทึกใน ledger แบบ double-entry ที่ตรวจสอบย้อนกลับได้ทุกบาท พร้อม reconciliation กับ national switch ทุกวันที่จับทุกรายการที่ไม่ตรงกัน แก้ด้วย entries ที่ตรวจสอบได้ และรายงานให้ทีม operations

Rubric

เกณฑ์4 (ดีมาก)2 (พอใช้)0
Requirements & NFRsวัดได้ทุกข้อ มี scope และ out of scopeมีแต่บางข้อวัดไม่ได้ไม่มี
Estimatesassumptions ชัด ตัวเลขถูก order of magnitude และนำไปใช้ตัดสินใจมีตัวเลขแต่ไม่ได้ใช้ไม่มี
Correctness of moneyidempotency, timeout handling, double-entry และ reconciliation ครบขาดหนึ่งเรื่องretry แบบอาจจ่ายซ้ำ
Trade-offs & ADRsADRs 3 เรื่องมี options และเหตุผลมี ADR แต่ไม่มี alternativesไม่มี
Security & operabilityauth, encryption, fraud, SLOs, rolloutมีบางส่วนไม่ได้พูดถึง
Communicationdiagram อ่านง่าย proposal ทั้งสองภาษาเข้าใจได้โดยผู้บริหารtechnical เกินไปไม่มี

แบบทดสอบ

3 ล้าน DAU คนละ 2 payments ต่อวัน ใช้ 1 วัน ≈ 100,000 วินาที average TPS เท่ากับเท่าไร

ส่งคำสั่งจ่ายไป national switch แล้ว timeout ควรทำอย่างไร
คุณสมบัติสำคัญของ double-entry ledger คืออะไร
Notification ถึงร้านค้าควร design อย่างไร
Ledger ของ e-wallet ควรเลือก consistency แบบใด