Integration
Messaging & Event-driven Design
ใช้ message queues และ event streaming อย่างถูกที่, RabbitMQ vs Kafka, delivery semantics, idempotent consumers, retries กับ DLQ, ordering, transactional outbox, distributed locks และ sagas
ลูกค้ากดจ่ายเงินแล้ว ระบบต้องตัด 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) | |
|---|---|---|
| Model | message ถูกลบเมื่อ consumer ack | event เก็บตาม 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 ทีหลัง
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 ย้อนสิ่งที่ทำไปแล้ว
- Step 1: ตัดเงิน 500 THB สำเร็จและ commit แล้ว
- Step 2: จองสินค้าล้มเหลว เพราะของหมด
- Compensate: สั่ง refund 500 THB ย้อน step 1
- Order จบที่สถานะ CANCELLED โดยไม่มี distributed transaction
| Orchestration | Choreography | |
|---|---|---|
| วิธี | มี 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) รวมเวลารอทั้งหมดกี่วินาที