Microservices Communication Patterns (Saga, Circuit Breaker) · บทเรียน

การสร้างตัวรับเหตุการณ์ที่ไม่ขึ้นต่อการทำซ้ำ

ทำให้ผู้เข้าร่วมซากาแบบประสานงานปลอดภัยจากเหตุการณ์ซ้ำและเหตุการณ์ที่มาถึงไม่เป็นลำดับด้วยตัวรับที่ไม่ขึ้นต่อการทำซ้ำ รูปแบบ inbox และรูปแบบ outbox

บทเรียน 4 จาก 413 ขั้นตอน

การสร้างตัวรับเหตุการณ์ที่ไม่ขึ้นต่อการทำซ้ำ เป็นบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Microservices Communication Patterns (Saga, Circuit Breaker) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Events Get Redelivered

In a choreography saga, services react to events from a broker. Brokers deliver at least once, so the same event can arrive twice — and sometimes out of order. Naive handlers double-charge or double-ship.

The Goal: Idempotent Consumers

An idempotent consumer can process the same event repeatedly with no extra effect. This is essential for correctness in any event-driven saga.

Track Processed Event Ids

Give every event a unique id. Before handling, check whether you have already processed that id; if so, skip and acknowledge.

if (inbox.exists(event.id())) { ack(); return; }
handle(event);
inbox.save(event.id());
ack();

The Inbox Pattern

The inbox pattern stores processed event ids in a table inside the same database transaction as the business change. Insert + work commit together, so a redelivery is rejected by the unique constraint.

BEGIN;
  INSERT INTO inbox(event_id) VALUES (?); -- unique
  UPDATE orders SET status = 'PAID' WHERE id = ?;
COMMIT;

Why Atomicity Is Key

If you ack the event but crash before committing the business change, the work is lost. Doing both in one transaction guarantees they succeed or fail together.

The Dual-Write Problem

A participant must often update its DB and publish a new event. Doing these as two separate calls risks one succeeding and the other failing — the classic dual-write problem.

The Outbox Pattern

The outbox pattern solves it: write the outgoing event into an outbox table in the same transaction as the business change. A separate relay publishes from the outbox to the broker.

BEGIN;
  UPDATE orders SET status = 'SHIPPED' WHERE id = ?;
  INSERT INTO outbox(payload) VALUES (?);
COMMIT;

Relaying the Outbox

A poller or change-data-capture (CDC) tool like Debezium reads new outbox rows and publishes them, then marks them sent. Publishing is at-least-once, so downstream consumers must be idempotent too.

Handling Out-of-Order Events

Events may arrive out of order. Use version numbers or sequence ids and ignore stale events, or design handlers that converge to the correct state regardless of order.

if (event.version() <= current.version()) return; // stale

Compensations Must Be Idempotent Too

When a saga fails, compensating events also flow through the broker and can be redelivered. A compensation (e.g. refund) must itself be idempotent, or you refund twice.

Putting It Together

A robust choreography participant: consume via the inbox (dedup), do work and write the outbox in one transaction, relay the outbox to the broker, and make every handler — including compensations — idempotent.

Quick Check

Test your event-consumer knowledge.

Recap

You hardened choreography consumers:

  • Brokers deliver at-least-once, so events repeat and may reorder
  • Idempotent consumers process duplicates safely
  • The inbox pattern dedups within the business transaction
  • The outbox pattern solves the dual-write problem; a relay/CDC publishes it
  • Handle out-of-order events and make compensations idempotent too
เริ่มต้นได้ฟรี

เรียนรู้ Microservices Communication Patterns (Saga, Circuit Breaker) ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การสร้างตัวรับเหตุการณ์ที่ไม่ขึ้นต่อการทำซ้ำ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การสร้างตัวรับเหตุการณ์ที่ไม่ขึ้นต่อการทำซ้ำ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การสร้างตัวรับเหตุการณ์ที่ไม่ขึ้นต่อการทำซ้ำ”

ทำให้ผู้เข้าร่วมซากาแบบประสานงานปลอดภัยจากเหตุการณ์ซ้ำและเหตุการณ์ที่มาถึงไม่เป็นลำดับด้วยตัวรับที่ไม่ขึ้นต่อการทำซ้ำ รูปแบบ inbox และรูปแบบ outbox คุณปฏิบัติ Microservices Communication Patterns (Saga, Circuit Breaker) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Microservices Communication Patterns (Saga, Circuit Breaker) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Microservices Communication Patterns (Saga, Circuit Breaker) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การสร้างตัวรับเหตุการณ์ที่ไม่ขึ้นต่อการทำซ้ำ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) นี้ได้ไหม

ได้ บทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การออกแบบ Saga ที่ขับเคลื่อนด้วยเหตุการณ์
  2. บัสเหตุการณ์และตัวกลางข้อความ
  3. การจัดการการชดเชยด้วยเหตุการณ์
  4. การสร้างตัวรับเหตุการณ์ที่ไม่ขึ้นต่อการทำซ้ำ
← กลับไปที่ Microservices Communication Patterns (Saga, Circuit Breaker)