การออกแบบตัวประสานงาน Saga
เรียนรู้การออกแบบบริการตัวประสานงานเฉพาะที่รับผิดชอบจัดการสถานะและขั้นตอนของ Saga
การออกแบบตัวประสานงาน Saga เป็นบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Microservices Communication Patterns (Saga, Circuit Breaker) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is a Saga Orchestrator?
In an Orchestration Saga, a dedicated service, called the Orchestrator, takes charge of managing the entire distributed transaction.
Think of it as a conductor in an orchestra. Instead of individual musicians (services) reacting to each other, the conductor (orchestrator) tells each musician when to play their part.
Why Use an Orchestrator?
Orchestration offers several advantages, especially for complex business flows:
- Centralized Logic: All saga logic resides in one place, making it easier to understand and manage.
- Easier Changes: Modifying saga steps or adding new ones is simpler as changes are contained within the orchestrator.
- Clearer Debugging: It's easier to trace the flow of a transaction and pinpoint exactly where a failure occurred.
Core Responsibilities
A saga orchestrator has crucial responsibilities to ensure the transaction completes successfully or is properly compensated:
- Initiates Saga: Starts the transaction by sending the first command.
- Tracks State: Maintains the current progress and state of the saga.
- Coordinates Steps: Sends commands to participant services based on the current state and responses.
- Handles Compensation: Triggers rollback actions if any step fails.
Example: Order Placement Saga
Let's consider an 'Order Placement' saga involving multiple services:
- Order Service: Creates the order.
- Payment Service: Processes the payment.
- Inventory Service: Updates stock.
- Shipping Service: Arranges delivery.
The orchestrator will guide the order through these steps.
Orchestrator State Management
The orchestrator must keep track of where the saga is in its lifecycle. This is its state. It typically stores:
sagaId: A unique identifier for the current transaction.currentStep: Which step is currently active or has been completed.status: e.g.,IN_PROGRESS,COMPLETED,FAILED,COMPENSATING.
This state must be persisted (saved) so the orchestrator can recover if it crashes.
Designing the Flow
The orchestrator's design centers around a state machine-like flow:
- Orchestrator sends a command to a participant service (e.g., 'ProcessPayment').
- Participant service performs its action and sends an event back (e.g., 'PaymentProcessed' or 'PaymentFailed').
- Orchestrator receives the event, updates its state, and decides the next command to send, or initiates compensation.
Orchestrator Structure (Code)
Here's a simplified Java example showing how an orchestrator might manage its state and react to responses. This code simulates the flow:
public class OrderSagaOrchestrator {
private String sagaId;
private String currentState;
public OrderSagaOrchestrator(String id) {
this.sagaId = id;
this.currentState = "INITIATED";
System.out.println("Saga " + sagaId + " state: " + currentState);
}
public void startOrderSaga() {
System.out.println("Sending 'Process Payment' command.");
this.currentState = "PAYMENT_PROCESSING";
System.out.println("Saga " + sagaId + " state: " + currentState);
}
public void handlePaymentResponse(boolean success) {
if (success) {
System.out.println("Payment successful. Sending 'Update Inventory' command.");
this.currentState = "INVENTORY_UPDATING";
} else {
System.out.println("Payment failed. Initiating compensation.");
this.currentState = "FAILED";
}
System.out.println("Saga " + sagaId + " state: " + currentState);
}
public String getCurrentState() {
return currentState;
}
public static void main(String[] args) {
OrderSagaOrchestrator orchestrator = new OrderSagaOrchestrator("ORD-123");
orchestrator.startOrderSaga();
orchestrator.handlePaymentResponse(true); // Simulate success
// orchestrator.handlePaymentResponse(false); // Try simulating failure
}
}Orchestrator Communication
For reliable communication, orchestrators typically interact with participant services via a message broker (like Apache Kafka or RabbitMQ).
- Orchestrator publishes commands to queues/topics for specific services.
- Participant services publish events (success or failure) back to topics that the orchestrator subscribes to.
This asynchronous messaging ensures loose coupling and resilience.
Implementing Compensation
If a participant service reports a failure, the orchestrator must initiate compensation. This means reversing any successfully completed steps.
For example, if payment succeeded but inventory update failed, the orchestrator would send a 'RefundPayment' command to the Payment Service.
The orchestrator's persisted state is vital here, as it knows exactly which steps need to be undone.
Quick Check
Which of the following are key responsibilities of a Saga Orchestrator?
Recap: Designing Orchestrators
In this lesson, we learned about designing a saga orchestrator. It's a dedicated service that acts as a central coordinator for distributed transactions.
- It initiates steps, tracks state, and coordinates participant services.
- Key to its design are persistent state management and robust communication via message brokers.
- Orchestrators simplify debugging and managing complex business flows, especially with their built-in compensation logic.
Next, we'll explore how to use state machines to build even more robust orchestrators!
คำถามที่พบบ่อย
บทเรียน “การออกแบบตัวประสานงาน Saga” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การออกแบบตัวประสานงาน Saga” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การออกแบบตัวประสานงาน Saga”
เรียนรู้การออกแบบบริการตัวประสานงานเฉพาะที่รับผิดชอบจัดการสถานะและขั้นตอนของ Saga คุณปฏิบัติ Microservices Communication Patterns (Saga, Circuit Breaker) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Microservices Communication Patterns (Saga, Circuit Breaker) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Microservices Communication Patterns (Saga, Circuit Breaker) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การออกแบบตัวประสานงาน Saga” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) นี้ได้ไหม
ได้ บทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การออกแบบตัวประสานงาน Saga
- เครื่องจักรสถานะสำหรับการประสานงาน
- การนำไปใช้ด้วยเครื่องมือกระบวนงาน
- การทดสอบซากาแบบควบคุมจากศูนย์กลาง