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

Integration

Messaging & Event-driven Design

ใช้ message queues และ event streaming อย่างถูกที่, RabbitMQ vs Kafka, delivery semantics, idempotent consumers, retries กับ DLQ, ordering, transactional outbox, distributed locks และ sagas

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

ลูกค้ากดจ่ายเงินแล้ว ระบบต้องตัด stock, ส่ง email, ให้แต้ม และแจ้ง warehouse ถ้าทำทั้งหมดแบบ synchronous ใน request เดียว service ไหนช้าหรือล่ม การจ่ายเงินก็ล้มตาม Messaging แยกงานเหล่านี้ออกจากกัน ระบบจึงทนต่อ failure และ spike ได้ดีขึ้น แต่ก็มาพร้อมคำถามใหม่: message ซ้ำ, หาย หรือสลับลำดับไหม?

Why go asynchronous

  • Decoupling: producer ไม่ต้องรู้ว่าใครใช้ event บ้าง เพิ่ม consumer ใหม่ได้โดยไม่แก้ producer
  • Buffering: queue รับ spike ไว้ consumers ค่อย ๆ ทำตาม capacity (load leveling)
  • Fan-out: event เดียว หลาย consumers
  • ต้องแลกกับ: eventual consistency, debugging ยากขึ้น และต้องมี monitoring ของ lag

Queue vs log

Message queue (RabbitMQ, SQS)Event log / streaming (Kafka)
Modelmessage ถูกลบเมื่อ consumer ackevent เก็บตาม retention แต่ละ consumer group จำ offset ของตัวเอง
Replayไม่ได้ใน classic queues (RabbitMQ Streams ทำได้)ได้ อ่านย้อนจาก offset เก่า
Orderingไม่รับประกันเมื่อมีหลาย consumersรับประกัน ภายใน partition
Scaling consumersเพิ่ม consumers ได้อิสระparallelism สูงสุด = จำนวน partitions ต่อ consumer group (แบบ classic; share groups ใน Kafka รุ่นใหม่ผ่อนข้อจำกัดนี้)
เหมาะกับtask queue, งานที่ต้อง routing ซับซ้อนevent streaming, หลาย consumers, analytics, CDC

Delivery semantics

Semanticsความหมายราคาที่จ่าย
At-most-onceส่งครั้งเดียว อาจหายเสีย messages ได้
At-least-onceไม่หาย แต่อาจ ซ้ำconsumer ต้อง idempotent
Exactly-onceไม่หายและไม่ซ้ำทำได้จริงแค่ในขอบเขตจำกัด (เช่น Kafka transactions ภายใน Kafka)

ในทางปฏิบัติ “exactly-once” ที่ business ต้องการคือ effectively-once = at-least-once delivery + idempotent consumer

Idempotent consumer: เก็บ message_id ที่ประมวลผลแล้วในตารางที่มี unique constraint และ insert ใน transaction เดียวกับงานหลัก ด้วย INSERT … ON CONFLICT DO NOTHING ถ้าไม่มีแถวถูก insert แปลว่าเป็น message ซ้ำ consumer ก็ข้ามไป

Retries, DLQ and ordering

  • Retry with exponential backoff + jitter: รอ 1, 2, 4, 8 … วินาที บวกสุ่มเล็กน้อย เพื่อไม่ให้ทุก consumer retry พร้อมกัน
  • จำกัดจำนวนครั้ง เกินแล้วส่งไป dead-letter queue (DLQ) พร้อม alert ให้คนตรวจ poison message (message ที่ผิดรูปแบบ) จะได้ไม่ block queue
  • Ordering: ถ้า events ของ order เดียวกันต้องเรียงลำดับ ให้ใช้ order_id เป็น partition key events ของ order เดียวกันจะอยู่ partition เดียวและเรียงกัน ส่วน orders ต่างกันยังประมวลผลขนานได้

The dual-write problem and the outbox

ถ้า service เขียน database แล้วค่อย publish event แยกกัน อาจสำเร็จแค่ฝั่งเดียว: DB commit แล้วแต่ publish fail (event หาย) หรือ publish แล้วแต่ DB rollback (phantom event) Transactional outbox แก้โดยเขียน event ลงตาราง outbox ใน transaction เดียวกับข้อมูล แล้วให้ relay publish ทีหลัง

Order service เขียน row ลง orders table และ event ลง outbox table ใน DB transaction เดียวกัน จากนั้น outbox relay (poll หรือ CDC) อ่าน outbox แล้ว publish ไป Kafka topic ให้ consumers เช่น email และ stock service ประมวลผลแบบ at-least-once
Outbox: event ไม่หายและไม่มี phantom event เพราะเกิดใน transaction เดียวกับข้อมูล

Relay อาจ publish ซ้ำได้ (เช่น ล่มหลัง publish แต่ก่อน mark ว่าส่งแล้ว) consumers จึงยังต้อง idempotent อยู่ดี

Distributed locks: use with care

Lock ข้ามเครื่องด้วย Redis (SET lock:item42 <token> NX PX 30000) ทำได้ แต่มีกับดัก: process อาจหยุดชั่วคราว (เช่น GC pause) นานจน lock หมดอายุ แล้วอีก process ได้ lock ไป ทั้งสองจึงคิดว่าตัวเองถือ lock

  • ใช้ fencing token (เลขที่เพิ่มขึ้นทุกครั้งที่ได้ lock) และให้ storage ปฏิเสธ token ที่เก่ากว่า
  • ถ้าทำได้ ให้ใช้ database constraint หรือ atomic update แทน lock (บทที่ 5 (Data Stores, Indexing & Transactions)) ซึ่งถูกต้องกว่าและง่ายกว่า

Sagas: transactions across services

แต่ละ service มี database ของตัวเอง จึงไม่มี ACID transaction ข้าม services Saga แบ่งเป็น local transactions ต่อกัน และถ้า step ไหนล้มเหลว ให้รัน compensating actions ย้อนสิ่งที่ทำไปแล้ว

Order (saga)PaymentInventory charge 500 THBOK (committed) reserve itemFAILED: out of stock refund 500 THB (compensate)OK order CANCELLED
  1. Step 1: ตัดเงิน 500 THB สำเร็จและ commit แล้ว
  2. Step 2: จองสินค้าล้มเหลว เพราะของหมด
  3. Compensate: สั่ง refund 500 THB ย้อน step 1
  4. Order จบที่สถานะ CANCELLED โดยไม่มี distributed transaction
Saga ไม่ rollback แต่ทำ action ใหม่เพื่อย้อนผล
OrchestrationChoreography
วิธีมี orchestrator สั่งทีละ stepแต่ละ service ฟัง events แล้วทำต่อเอง
ข้อดีเห็น flow ทั้งหมดที่เดียว, debug ง่ายไม่มีศูนย์กลาง, coupling ต่ำ
ข้อเสียorchestrator เป็นจุดที่ต้องดูแลมากflow กระจาย, ยากเมื่อ steps เยอะ

Compensations ต้อง idempotent และ design ให้ business ยอมรับได้ เช่น ส่ง email ไปแล้วย้อนไม่ได้ ก็ต้องส่ง email แก้ไขแทน

แบบทดสอบ

Kafka topic มี 12 partitions และ consumer group มี 20 consumers มี consumers ที่ได้รับ partitions ไปประมวลผลจริงกี่ตัว

Retry 5 ครั้งด้วย exponential backoff เริ่มที่ 1 วินาทีและเพิ่มเป็นสองเท่าทุกครั้ง (ไม่คิด jitter) รวมเวลารอทั้งหมดกี่วินาที

ถ้า broker รับประกัน at-least-once consumer ต้องมีคุณสมบัติอะไร
Service commit order ลง DB แล้ว publish event ไม่สำเร็จ ทำให้ event หาย ควรใช้ pattern ใด
Events ของ order เดียวกันต้องประมวลผลตามลำดับ แต่ระบบยังต้อง scale ได้ ควรทำอย่างไร
ใน saga payment commit ไปแล้วแต่ step ถัดไปล้มเหลว ต้องทำอย่างไร