Choreography Saga erklärt
Verstehen Sie den Choreography-Ansatz, bei dem Services direkt über Ereignisse miteinander kommunizieren, ohne zentrale Koordination.
Choreography Saga erklärt ist eine kostenlose Microservices Communication Patterns (Saga, Circuit Breaker)-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Microservices Communication Patterns (Saga, Circuit Breaker)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Microservices Communication Patterns (Saga, Circuit Breaker)-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Choreography Saga Intro
Welcome! In this lesson, we'll explore the Choreography Saga pattern. It's a way to manage complex business transactions that span multiple services in a decentralized way.
Unlike an orchestra with a conductor, choreography is like a dance where each dancer knows their part and reacts to others' movements.
Decentralized Event Flow
In a choreography saga, there's no central coordinator service. Instead, each service involved in the transaction publishes events and listens for events from other services.
- Services react to events.
- They perform their part of the transaction.
- They publish new events to trigger the next step.
Order Processing Example
Let's use a common example: processing a customer order. This involves multiple steps across different services:
- Order Service: Creates the order.
- Payment Service: Handles payment.
- Inventory Service: Updates stock.
How do these services coordinate without a central brain?
Step 1: Order Created Event
When a customer places an order, the Order Service starts the saga. It saves the order and then publishes an OrderCreatedEvent.
This event signals to other services that a new order is ready for processing.
public class OrderService {
public static void processNewOrder(String orderId) {
System.out.println("Order Service: Received new order " + orderId);
System.out.println("Order Service: Saving order " + orderId + " to database...");
// Imagine database interaction here
System.out.println("Order Service: Order " + orderId + " saved.");
System.out.println("Order Service: Publishing 'OrderCreatedEvent' for " + orderId);
}
public static void main(String[] args) {
processNewOrder("ORD789"); // Simulate a new order coming in
}
}Step 2: Payment Service Reacts
The Payment Service is subscribed to OrderCreatedEvents. When it receives one, it processes the payment for that order.
After processing, it publishes either a PaymentProcessedEvent or a PaymentFailedEvent.
public class PaymentService {
public static void handleOrderCreated(String orderId) {
System.out.println("Payment Service: Received 'OrderCreatedEvent' for order " + orderId);
System.out.println("Payment Service: Processing payment for " + orderId + "...");
// Simulate payment gateway interaction
boolean paymentSuccess = true; // For this example, assume success
if (paymentSuccess) {
System.out.println("Payment Service: Payment successful for " + orderId + ".");
System.out.println("Payment Service: Publishing 'PaymentProcessedEvent' for " + orderId);
} else {
System.out.println("Payment Service: Payment failed for " + orderId + ".");
System.out.println("Payment Service: Publishing 'PaymentFailedEvent' for " + orderId);
}
}
public static void main(String[] args) {
handleOrderCreated("ORD789"); // Simulate receiving an event
}
}Step 3: Inventory Service Updates
Next, the Inventory Service listens for PaymentProcessedEvents. Upon receiving one, it reduces the stock for the ordered items.
It then publishes an InventoryUpdatedEvent to indicate its task is complete.
public class InventoryService {
public static void handlePaymentProcessed(String orderId) {
System.out.println("Inventory Service: Received 'PaymentProcessedEvent' for order " + orderId);
System.out.println("Inventory Service: Updating stock for order " + orderId + "...");
// Simulate inventory database update
System.out.println("Inventory Service: Stock updated for order " + orderId + ".");
System.out.println("Inventory Service: Publishing 'InventoryUpdatedEvent' for " + orderId);
}
public static void main(String[] args) {
handlePaymentProcessed("ORD789"); // Simulate receiving an event
}
}The Challenge of Failures
What happens if a step in this flow fails? For example, if the payment fails, we can't update inventory. We also need to undo any previous successful steps.
This is where compensation logic comes in. It's about reversing previously completed actions.
Compensation in Choreography
In a choreography saga, compensation also happens through events. If a service fails, it publishes a compensation event.
Other services listen for these compensation events and perform their own rollback actions.
Compensation Example: Payment Fails
Imagine the Payment Service fails and publishes a PaymentFailedEvent. The Order Service, which started the saga, listens for this event.
Upon receiving it, the Order Service updates the order status to 'Cancelled', effectively rolling back the transaction from its side.
public class OrderService {
public static void handlePaymentFailed(String orderId) {
System.out.println("Order Service: Received 'PaymentFailedEvent' for order " + orderId);
System.out.println("Order Service: Initiating compensation for " + orderId + ".");
// Change order status to 'Cancelled'
System.out.println("Order Service: Updating order " + orderId + " status to 'Cancelled'.");
// Could publish OrderCancelledEvent if other services need to know
}
public static void main(String[] args) {
handlePaymentFailed("ORD789"); // Simulate receiving a compensation event
}
}Pros and Cons of Choreography
Choreography offers benefits but also presents challenges:
- Pros: Highly decoupled services, no central point of failure, simpler to implement for simple flows.
- Cons: Can be harder to monitor the overall transaction flow, complex compensation logic, potential for 'event storms' if not designed carefully.
Choreography Check
Consider a choreography saga where an 'OrderConfirmedEvent' is published. Which statement accurately describes how the next step is initiated?
Recap: Choreography Saga
You've learned about the Choreography Saga pattern:
- It's a decentralized approach for distributed transactions.
- Services communicate by publishing and subscribing to events.
- There's no central coordinator; each service knows its role.
- Compensation for failures is also handled via events, allowing services to roll back their actions.
This pattern promotes loose coupling but requires careful design for monitoring and compensation.
Lerne Microservices Communication Patterns (Saga, Circuit Breaker) mit einem KI-Tutor — kostenlos
Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.
- Kurse
- 12
- Lektionen
- 48
Häufig gestellte Fragen
Ist die Lektion „Choreography Saga erklärt“ kostenlos?
Ja — der vollständige Text von „Choreography Saga erklärt“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Microservices Communication Patterns (Saga, Circuit Breaker)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Microservices Communication Patterns (Saga, Circuit Breaker)-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Choreography Saga erklärt“?
Verstehen Sie den Choreography-Ansatz, bei dem Services direkt über Ereignisse miteinander kommunizieren, ohne zentrale Koordination. Du übst Microservices Communication Patterns (Saga, Circuit Breaker) mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Microservices Communication Patterns (Saga, Circuit Breaker) zu starten?
Keine Vorkenntnisse erforderlich. Microservices Communication Patterns (Saga, Circuit Breaker) auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Choreography Saga erklärt“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Microservices Communication Patterns (Saga, Circuit Breaker)-Lektion Code schreiben und ausführen?
Ja. Jede Microservices Communication Patterns (Saga, Circuit Breaker)-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Was ist das Saga-Pattern?
- Choreography Saga erklärt
- Orchestration Saga erklärt
- Zwischen Choreografie und Orchestrierung wählen