What is the difference between Choreography and Orchestration?
Topic: microservices, Architecture. Level: Intermediate.
Answer
- **Choreography**: no central coordinator; each service reacts to events and emits its own. Decoupled and scalable, but the end-to-end flow is implicit and hard to trace/monitor.
- **Orchestration**: a central orchestrator explicitly tells each service what to do and tracks progress. Easier to visualize, monitor, and change flow logic, but the orchestrator is a coupling point and potential bottleneck.
Choose choreography for simple, loosely-coupled reactions; orchestration for complex, multi-step business processes needing visibility (often implemented as an orchestrated Saga).
```java
// CHOREOGRAPHY — services react to each other's events
// order-service: emit OrderPlaced
// payment-service: on OrderPlaced -> charge -> emit PaymentCompleted
// shipping-service:on PaymentCompleted -> ship
// ORCHESTRATION — one coordinator drives the steps
class OrderOrchestrator {
void place(Order o) {
payment.charge(o); // step 1
inventory.reserve(o); // step 2
shipping.schedule(o); // step 3 (with compensation on failure)
}
}
```