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

การจัดการการชดเชยด้วยเหตุการณ์

นำขั้นตอนการชดเชยใน Saga แบบประสานงานด้วยเหตุการณ์ไปใช้ โดยเผยแพร่เหตุการณ์ย้อนกลับเฉพาะเพื่อยกเลิกการทำงานก่อนหน้า

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

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

Why Compensation is Crucial

In choreography sagas, services react to events. But what happens when one of those services fails to complete its part of a distributed transaction?

We need a way to undo any actions that were successfully performed by previous services. This is where compensation comes in.

What is Compensation?

Compensation is the process of reversing previously completed operations within a distributed transaction. Think of it as a 'rollback' mechanism tailored for microservices.

It ensures that if any step in a multi-service business process fails, the system can return to a consistent state.

Compensation in Choreography

In a choreography saga, there's no central orchestrator. Services communicate directly via events. So, how do we trigger compensation?

  • When a service fails to complete its task, it publishes a specific compensation event.
  • Other services that have successfully completed their part listen for these compensation events.
  • Upon receiving a compensation event, they execute their own rollback logic.

Designing Compensation Events

Compensation events must contain enough information for services to perform their rollback. Key details often include:

  • The original saga ID or transaction ID.
  • Details about the original action that needs to be reversed.
  • Any data required to perform the reversal (e.g., amount to refund, item to unreserve).

Example: Order Processing Saga

Consider an online order process:

  1. Order Service: Creates order (publishes OrderCreated)
  2. Payment Service: Processes payment (publishes PaymentProcessed)
  3. Inventory Service: Reserves stock (publishes InventoryReserved)

What if the Inventory Service fails to reserve stock?

Initiating the Rollback

If the Inventory Service fails to reserve stock, it should:

  • Not publish InventoryReserved.
  • Instead, publish a compensation event, like InventoryReservationFailed.

This event signals to other services that the saga could not complete successfully and they need to undo their actions.

Payment Service's Compensation

The Payment Service is subscribed to events that indicate failures. When it receives InventoryReservationFailed, it knows it needs to act:

  • It will initiate a refund for the payment it previously processed.
  • After refunding, it might publish a new event, like PaymentRefunded, to inform other interested services.

Order Service's Compensation

The Order Service, having created the initial order, also needs to react. It might listen for InventoryReservationFailed or PaymentRefunded.

  • Upon receiving such an event, the Order Service updates the order's status to 'Cancelled' or 'Failed'.
  • This ensures the user sees an accurate status for their order.

Implementing Compensation Logic

Each service must contain logic to handle both successful forward-progress events and specific compensation events. This often means having separate event handlers for each.

Here's a simplified example of how a Payment Service might listen for a compensation event and trigger a refund:

public class PaymentService {

  public static void main(String[] args) {
    System.out.println("Payment Service Started.");
    // Simulate receiving an event from a message broker
    String receivedEvent = "InventoryReservationFailed: Order123";

    if (receivedEvent.startsWith("InventoryReservationFailed")) {
      String orderId = receivedEvent.split(":")[1];
      System.out.println("Received compensation event: " + receivedEvent);
      System.out.println("Initiating refund for Order ID: " + orderId + "...");
      
      // In a real system, call a payment gateway API to refund
      boolean refundSuccess = processRefund(orderId);
      
      if (refundSuccess) {
        System.out.println("Refund processed successfully for " + orderId + ".");
        // Publish 'PaymentRefunded' event for other services
        System.out.println("Publishing PaymentRefunded event.");
      } else {
        System.out.println("Refund failed for " + orderId + ".");
        // Handle refund failure (e.g., alert, manual intervention)
      }
    } else {
      System.out.println("No compensation event received yet.");
    }
    System.out.println("Payment Service Shutting Down.");
  }

  private static boolean processRefund(String orderId) {
    // Simulate external refund API call
    return true; // Assume success for this example
  }
}

Quick Check: Compensation Purpose

In a choreography saga, if the 'Inventory Service' fails to reserve items after 'Payment Service' has processed a payment, what is the primary purpose of the 'Inventory Service' publishing an InventoryReservationFailed event?

Recap: Handling Compensation

You've learned how critical compensation is in choreography sagas to maintain data consistency in distributed systems. Key takeaways:

  • Compensation reverses successful actions when a saga step fails.
  • In choreography, services publish specific compensation events to initiate rollbacks.
  • Each service must implement logic to listen for and react to these events, performing its own undo operations.
  • Clear event design and robust handling are essential for resilient sagas.

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

บทเรียน “การจัดการการชดเชยด้วยเหตุการณ์” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการการชดเชยด้วยเหตุการณ์”

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

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

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