A saga coordinates a sequence of local transactions across services that own separate data stores. Each step updates one service; a later step can be triggered by a message or directed by an orchestrator. This provides a way to manage consistency when one transaction cannot cover the whole workflow. Two-phase commit is another approach to distributed transactions. section 2.3.2
Choose how the steps are coordinated:
- Choreography: each service commits its local transaction and publishes an event that triggers the next service. A command channel targets the next service and can specify a reply channel, but its publisher must know that service’s location. With pub/sub, interested services consume domain events. section 2.3.3
- Orchestration: an orchestrator directs participants through command channels or request/response calls and chooses the next step from each response. A request/response design requires services to know the addresses needed to connect. section 2.3.4
Creating an order
In a choreographed flow, Order Service creates an order as PENDING and publishes an order-created event. Payment Service attempts the charge and reports success or failure. A failure marks the order FAILED; a successful payment allows Shipping Service to proceed. Shipping’s result ultimately marks the order FAILED or SUCCESS. In an orchestrated flow, a shipping failure instead prompts compensating transactions in reverse order: refund the payment, then cancel the order. section 2.3.3 section 2.3.4
Plan how the client learns the outcome of a saga that does not finish immediately. The service can return the result on completion, return an identifier for polling, or send the result over a WebSocket connection. section 2.3.3
Figure 2.5 An orchestrator for an order creation creates an order and payment process, and the Shipping service starts to complete the order flow. It runs a compensation transaction in case of failure, and this is executed from the bottom up.
The order-flow diagram shows the orchestrator’s steps and the reverse compensation path after failure. section 2.3.4
