In this lesson, you will learn how to manage long-running distributed transactions by choosing between decentralized choreography and centralized orchestration.
When executing a multi-step business process across sharded databases—such as an e-commerce order—you must ensure data consistency without locking resources across nodes. Since we’ve already covered the Saga Pattern, we now need to decide how to manage the sequence of those local transactions.
Choreography: The Decentralized Approach
In choreography, there is no central controller. Each service involved in the process performs its local transaction and publishes an event (e.g., OrderCreated). Other services listen for these events, execute their logic, and emit their own events (e.g., InventoryReserved).
- Pros: High decoupling; services don't need to know about each other’s internal workflows.
- Cons: Becomes difficult to track when processes grow complex; debugging the "flow" is hard because it’s distributed across event logs.
Orchestration: The Centralized Approach
In orchestration, you implement a centralized "Orchestrator" service (or state machine) that acts as the brain of the workflow. It tells each participant when to perform its action and handles the sequence logic.
-- Conceptual Orchestration logic
BEGIN TRANSACTION;
-- The Orchestrator dictates the flow
EXECUTE InventoryService.ReserveItem(orderId);
EXECUTE PaymentService.ProcessCharge(orderId);
EXECUTE ShippingService.ScheduleDelivery(orderId);
COMMIT;
- Pros: Centralized visibility; the state of the long-running process is easy to monitor and easier to debug.
- Cons: The Orchestrator can become a bottleneck or a single point of failure; it risks tight coupling if the orchestrator needs deep knowledge of participant internals.
When to choose?
Use choreography for simple workflows where services are naturally decoupled and high performance/scalability is the priority. Use orchestration when your business logic is complex, involves many services, or requires strict monitoring and auditability of the process state. Because orchestration keeps the state in one place, it is often preferred when the sequence of operations frequently changes or requires intricate error handling (compensating transactions) that would be opaque in a purely event-driven choreographic system.
Key Takeaway: Choreography prioritizes loose coupling through decentralized event flow, whereas Orchestration prioritizes visibility and control through a centralized state machine.