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:
- Creating the order.
- Reserving items in inventory.
- Issuing an invoice to the customer.
If one of these steps fails, the coordinator will be like, "Okay, cancel the order."
Advantages of orchestration
- Easier management: all steps are managed centrally, which makes the overall process easier to understand.
- Easier to debug: the coordinator logs help you track where something failed.
- Good for complex processes: convenient when you have a complex process with many steps.
Disadvantages of orchestration
- Centralized single point of failure: if the coordinator goes down, the process stops.
- Less flexibility: adding new steps or changes requires modifying the coordinator.
Tools for orchestration
Some popular orchestration tools:
- Camunda: a powerful BPMN engine that lets you model and manage processes.
- 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:
- The order service publishes an
OrderCreatedevent. - The inventory service receives that event and tries to reserve items. If successful, it publishes a
StockReservedevent. - The billing service handles the
StockReservedevent 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
- Flexibility: you can easily add a new service that reacts to existing events.
- High scalability: there's no single point of failure.
- Asynchronicity: allows each part of the process to be handled independently.
Disadvantages of choreography
- Debugging complexity: it's harder to figure out where things went wrong because the process is distributed.
- 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:
- Order creation.
- Reserving items.
- Issuing an invoice.
- 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.
GO TO FULL VERSION