Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Saga / Choreography Pattern

Introduction to Saga / Choreography Pattern

The Saga / Choreography Pattern is used to manage long-running distributed transactions in microservices architectures. Unlike centralized orchestration, choreography relies on each service emitting and reacting to events to coordinate the transaction. Each service performs its part of the transaction and publishes events to trigger the next step. If a failure occurs, compensating actions are triggered to undo previous steps, ensuring consistency. This sequence diagram illustrates a saga for an order fulfillment process, showing event-driven coordination and compensating actions.

Choreography avoids a central coordinator, promoting loose coupling and scalability in distributed transactions.

Saga / Choreography Pattern Diagram

The sequence diagram below visualizes an order fulfillment saga. The Orders Service initiates the transaction by creating an order and publishing an OrderCreated event. The Payment Service, Inventory Service, and Shipping Service react to events, perform their tasks, and emit new events. If the Inventory Service fails (e.g., out of stock), it triggers a compensating action, prompting the Payment Service to refund and the Orders Service to cancel the order. Arrows are color-coded: yellow (dashed) for event flows and red (dashed) for compensating flows.

sequenceDiagram participant OS as Orders Service participant EB as Event Bus participant PS as Payment Service participant IS as Inventory Service participant SS as Shipping Service OS->>EB: 🟑 Publish OrderCreated Event EB-->>PS: 🟑 OrderCreated Event PS->>EB: 🟑 Publish PaymentProcessed Event EB-->>IS: 🟑 PaymentProcessed Event IS->>EB: 🟑 Publish InventoryReserved Event EB-->>SS: 🟑 InventoryReserved Event SS->>EB: 🟑 Publish ShippingScheduled Event Note over EB: βœ… Successful Saga Completion alt Inventory Failure IS->>EB: πŸ”΄ Publish InventoryFailed Event EB-->>PS: πŸ”΄ InventoryFailed Event PS->>EB: πŸ”΄ Publish PaymentRefunded Event EB-->>OS: πŸ”΄ PaymentRefunded Event OS->>OS: πŸ”΄ Cancel Order Note over EB: πŸ” Compensating Actions end
Compensating actions ensure consistency by undoing completed steps when a saga fails.

Key Components

The core components of the Saga / Choreography Pattern include:

  • Services: Microservices (e.g., Orders, Payment, Inventory, Shipping) that perform transaction steps and emit events.
  • Event Bus: Routes events between services to coordinate the saga.
  • Events: Messages (e.g., OrderCreated, PaymentProcessed) that trigger the next step in the saga.
  • Compensating Actions: Operations (e.g., RefundPayment, CancelOrder) that undo steps during failures.

Benefits of Saga / Choreography Pattern

  • Decoupling: Services coordinate via events, eliminating the need for a central orchestrator.
  • Scalability: Each service can scale independently, and the event bus handles coordination.
  • Resilience: Compensating actions maintain consistency despite failures.
  • Flexibility: New services can join the saga by subscribing to relevant events.

Implementation Considerations

Implementing the Saga / Choreography Pattern requires careful planning:

  • Event Design: Define clear, versioned event schemas to ensure interoperability.
  • Compensating Logic: Implement robust compensating actions to handle all failure scenarios.
  • Broker Reliability: Use a durable event bus (e.g., Kafka, RabbitMQ) to ensure event delivery.
  • Monitoring: Track saga progress and compensating actions with observability tools.
  • Idempotency: Ensure services handle duplicate events to maintain consistency.
A well-designed event bus and robust compensating actions are critical for reliable saga choreography.