CodeGym /Courses /Module 5. Spring /Lecture 213: Saga Coordination — Orchestration and Choreo...

Lecture 213: Saga Coordination — Orchestration and Choreography

Module 5. Spring
Level 14 , Lesson 2
Available

If you picture a distributed system as a big musical orchestra, there are two ways to run the show: orchestration and choreography.

  • Orchestration — this is when you have a conductor (a centralized component) that controls and directs every instrument (microservice). It decides when and who should play their part.
  • Choreography — think of it like an improvisational dance where the instruments (microservices) interact directly with each other without instructions from a conductor. Each service just reacts to events happening in the system.

Differences between orchestration and choreography

Aspect Orchestration Choreography
Approach Centralized control Decentralized interaction
Coordinator There is a main coordinator No single main coordinator
Flexibility Less flexible — changes require editing the conductor Highly flexible — you can easily add new events
Debugging simplicity Easier to debug — changes are centralized Harder to debug due to decentralization
Examples BPMN, Camunda Kafka, RabbitMQ

Saga Orchestration

In orchestration there's one central component that manages all the steps of the saga. It's basically the "saga coordinator". It knows what actions need to be taken, in what order, and what to do if something goes wrong.

For example, if you want to implement an order flow, the coordinator will manage:

  1. Creating the order.
  2. Reserving items in inventory.
  3. Issuing an invoice to the customer.

If one of these steps fails, the coordinator will be like, "Okay, cancel the order."

Advantages of orchestration

  1. Easier management: all steps are managed centrally, which makes the overall process easier to understand.
  2. Easier to debug: the coordinator logs help you track where something failed.
  3. Good for complex processes: convenient when you have a complex process with many steps.

Disadvantages of orchestration

  1. Centralized single point of failure: if the coordinator goes down, the process stops.
  2. Less flexibility: adding new steps or changes requires modifying the coordinator.

Tools for orchestration

Some popular orchestration tools:

  1. Camunda: a powerful BPMN engine that lets you model and manage processes.
  2. Zeebe: a more modern tool from the same team as Camunda, but lighter and microservice-oriented.

Saga Choreography

In the choreography model you don't have a single control center. Instead, microservices react to events happening around them. For example, in the order flow:

  1. The order service publishes an OrderCreated event.
  2. The inventory service receives that event and tries to reserve items. If successful, it publishes a StockReserved event.
  3. The billing service handles the StockReserved event and issues an invoice to the customer.

Each service is responsible for its own slice of work, and they connect via event exchange.

Advantages of choreography

  1. Flexibility: you can easily add a new service that reacts to existing events.
  2. High scalability: there's no single point of failure.
  3. Asynchronicity: allows each part of the process to be handled independently.

Disadvantages of choreography

  1. Debugging complexity: it's harder to figure out where things went wrong because the process is distributed.
  2. Lack of centralized "knowledge": it's tough to get a complete picture of the current state of the saga.

Tools for choreography

Tools that work well for event-driven coordination:

  • Apache Kafka: provides a pub/sub platform for events.
  • RabbitMQ: another popular messaging system.
  • Eventuate: a platform specifically built for implementing event-driven choreography.

When to use orchestration and when choreography?

Both approaches have their pros and cons. The right choice depends on your business case, system scale, and fault-tolerance requirements.

Use orchestration when:

  • Your business logic is complex and you need strict control over process steps.
  • You need centralized control over the process.

Use choreography when:

  • Your processes are fairly autonomous and you want to minimize service coupling.
  • Flexibility matters and you want to add/change steps without altering the whole system.

Implementation example: Order processing

Scenario

Let's say we have an order processing flow made of these steps:

  1. Order creation.
  2. Reserving items.
  3. Issuing an invoice.
  4. Sending a notification to the customer.

Orchestration implementation


@Component
public class OrderSagaOrchestrator {

    // Injecting services to perform the steps
    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private BillingService billingService;

    @Autowired
    private NotificationService notificationService;

    public void startOrderSaga(Order order) {
        try {
            // Step 1: Reserve items
            inventoryService.reserveStock(order);

            // Step 2: Create invoice
            billingService.createInvoice(order);

            // Step 3: Notify the customer
            notificationService.sendOrderConfirmation(order);

        } catch (Exception e) {
            // If something goes wrong, perform compensation
            inventoryService.releaseStock(order);
        }
    }
}

Choreography implementation


// Order service publishes an event
@Component
public class OrderService {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void createOrder(Order order) {
        // Logic for creating the order...

        // Publish the event
        kafkaTemplate.send("order-topic", "OrderCreated", order.getId());
    }
}

// Inventory service handles the "OrderCreated" event
@Component
@KafkaListener(topics = "order-topic")
public class InventoryService {

    @KafkaHandler
    public void handleOrderCreated(String orderId) {
        // Logic for reserving items...

        // Publish the "StockReserved" event
        kafkaTemplate.send("inventory-topic", "StockReserved", orderId);
    }
}

// Billing service handles the "StockReserved" event
@Component
@KafkaListener(topics = "inventory-topic")
public class BillingService {

    @KafkaHandler
    public void handleStockReserved(String orderId) {
        // Logic for issuing an invoice...

        // Publish the "InvoiceCreated" event
        kafkaTemplate.send("billing-topic", "InvoiceCreated", orderId);
    }
}

Takeaways

Orchestration and choreography are two different ways to manage complex processes in a microservice architecture. Each has its strengths and weaknesses, and the choice depends on the specific business case. To build a reliable, scalable, and fault-tolerant system, teams often use both approaches within a single application. For example, you might manage complex processes via orchestration and keep simpler flows as choreography.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION