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

อธิบาย Saga แบบมีตัวประสานงาน

สำรวจแนวทางแบบมีตัวประสานงาน ซึ่งบริการเฉพาะจะประสานขั้นตอนต่าง ๆ ของธุรกรรมแบบกระจาย

อธิบาย 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 บทเรียน

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

Introducing Orchestration Sagas

Welcome to the Orchestration Saga! In a distributed system, sometimes a business transaction needs to involve multiple services.

An Orchestration Saga is a pattern where a central service, called an orchestrator, takes charge of guiding the entire transaction.

The Role of the Orchestrator

Think of the orchestrator as a project manager for your distributed transaction. It knows all the steps and coordinates them.

  • It sends commands to participant services.
  • It waits for responses (events) from those services.
  • It decides the next step based on the responses.

How it Works: A Simple Flow

Unlike a Choreography Saga where services react to events from each other, here the orchestrator is in control:

  1. Orchestrator sends a command to Service A.
  2. Service A performs its task and sends a completion event back to the orchestrator.
  3. Orchestrator receives the event and sends a command to Service B.
  4. This continues until the transaction is complete.

Example: Online Order Process

Let's use an online order as an example. An order might need to:

  • Create an order in the Order Service.
  • Reserve items in the Inventory Service.
  • Process payment in the Payment Service.
  • Initiate shipping in the Shipping Service.

An orchestrator would manage this sequence.

Orchestrator's State Management

The orchestrator must keep track of the saga's current state and context. This means it needs to store information like:

  • Which steps have completed?
  • What data was returned by previous steps?
  • What is the current status (e.g., PAYMENT_PENDING, INVENTORY_RESERVED)?

This state is crucial for deciding the next action or handling failures.

Commands & Events

The orchestrator communicates using two main types of messages:

  • Commands: Sent from the orchestrator to a service, telling it what to do (e.g., ReserveInventory).
  • Events: Sent from a service back to the orchestrator, reporting the outcome (e.g., InventoryReserved, PaymentFailed).

This clear distinction helps maintain control.

Handling Failures & Compensation

What if a step fails? The orchestrator is responsible for compensation.

If the Payment Service fails, the orchestrator detects the PaymentFailed event. It then knows to send compensation commands to previous services, such as ReleaseInventory to the Inventory Service and CancelOrder to the Order Service.

Orchestrator Flow Demo

Let's look at a simplified Java example. This isn't a full microservice, but it shows how an orchestrator's logic might flow, reacting to simulated service outcomes.

Notice how it tracks sagaState and triggers compensation if payment fails.

public class OrderSagaOrchestrator {

  public static void main(String[] args) {
    System.out.println("Saga Orchestrator Started: Order 123");
    String sagaState = "ORDER_INITIATED";

    System.out.println("Orchestrator: Sending 'CREATE_ORDER' command.");
    // Simulate Order Service success
    sagaState = "ORDER_CREATED";
    System.out.println("Orchestrator: Received 'ORDER_CREATED' event.");

    if (sagaState.equals("ORDER_CREATED")) {
      System.out.println("Orchestrator: Sending 'RESERVE_INVENTORY' command.");
      // Simulate Inventory Service success
      sagaState = "INVENTORY_RESERVED";
      System.out.println("Orchestrator: Received 'INVENTORY_RESERVED' event.");
    }

    if (sagaState.equals("INVENTORY_RESERVED")) {
      System.out.println("Orchestrator: Sending 'PROCESS_PAYMENT' command.");
      // Simulate Payment Service *failure*
      System.out.println("Orchestrator: Received 'PAYMENT_FAILED' event.");
      sagaState = "PAYMENT_FAILED";
    }

    if (sagaState.equals("PAYMENT_FAILED")) {
      System.out.println("Orchestrator: Initiating compensation!");
      System.out.println("Orchestrator: Sending 'CANCEL_ORDER' command.");
      System.out.println("Orchestrator: Sending 'RELEASE_INVENTORY' command.");
      sagaState = "SAGA_COMPENSATED";
      System.out.println("Orchestrator: Saga completed with compensation.");
    } else {
      System.out.println("Orchestrator: Sending 'CONFIRM_ORDER' command.");
      System.out.println("Orchestrator: Sending 'SHIP_ORDER' command.");
      sagaState = "SAGA_COMPLETED_SUCCESSFULLY";
      System.out.println("Orchestrator: Saga completed successfully.");
    }
  }
}

Advantages of Orchestration

Orchestration Sagas offer clear benefits:

  • Centralized Control: Easier to understand the overall flow.
  • Simplified Participant Services: Services don't need to know about the entire saga; they just follow orchestrator commands.
  • Easier Debugging: The orchestrator's state makes it simpler to trace issues.
  • Explicit Compensation: Compensation logic is clearly defined within the orchestrator.

Check Your Understanding

Which of the following statements accurately describe key characteristics of an Orchestration Saga?

Recap: Orchestration Saga

You've learned about the Orchestration Saga pattern!

  • It uses a central orchestrator service to manage a distributed transaction.
  • The orchestrator sends commands to services and receives events as responses.
  • It maintains the saga's state to know where it is in the transaction.
  • It explicitly handles compensation by issuing rollback commands if a step fails.

This pattern provides clear control and simplifies participant services.

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

บทเรียน “อธิบาย Saga แบบมีตัวประสานงาน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “อธิบาย Saga แบบมีตัวประสานงาน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 บทเรียน

บทเรียน “อธิบาย Saga แบบมีตัวประสานงาน” ใช้เวลานานแค่ไหน

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

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

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

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

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