A saga orchestrator centralizes coordination of a system command that spans multiple services. It invokes saga participants, tracks state, and chooses the next operation from each outcome. If a forward transaction fails, it invokes compensating transactions for preceding steps. Steps typically run sequentially, although independent transactions can sometimes run concurrently. section 10.0 section 10.2
Creating an order
An orchestrator embedded in the Order Service creates an order, requests credit reservation from the Customer Service, requests inventory reservation from the Inventory Service, and approves the order. It performs local order operations directly and sends commands to the other services. If inventory is unavailable, it releases the reserved credit and rejects the order. section 10.2.1
Model the orchestrator as a state machine: states represent operations in progress and final outcomes, entry actions invoke participants, and transitions reflect operation outcomes. Persist the saga state durably, including application data needed by subsequent participants. This gives the execution an explicit, trackable form. section 10.2.2 section 10.2.6
Figure 10.13 The state machine for a saga orchestrator that invokes the participants using request/asynchronous-response. The state entry actions send command messages. The state transitions are triggered by reply messages.
The state machine shows how command-sending entry actions and reply-triggered transitions coordinate an asynchronous saga. section 10.2.4
Choose how the orchestrator invokes participants:
- Asynchronous messaging uses participant command channels and an orchestrator reply channel. Use request/asynchronous-response when the outcome is needed; one-way notifications can suit operations that return no data and cannot fail because of business-rule violations. section 10.2.4
- Synchronous request/response can use HTTP. The orchestrator must retry transient communication failures; idempotent endpoints use a key to detect duplicate requests and return the original response. section 10.2.5
Place the orchestrator in a separate service or embed it in a participant. A separate service simplifies participants and reduces their design-time coupling, but adds architectural complexity and typically increases runtime coupling. Embedding reduces the service count and can permit an earlier response, but gives the entry-point service coordination responsibilities and can increase its design-time coupling. section 10.2.3
Orchestration makes a multistep saga easier to understand and keeps coordination out of participant business logic. It is more complex than event-driven choreography and requires an orchestration framework. Keep business decisions, such as checking available credit, in the responsible participant rather than building a smart orchestrator with dumb participants. For a simple saga, choreography may be the simpler choice. section 10.2.6 section 10.3
Prefer an existing orchestration framework over building the infrastructure yourself. Eventuate and Temporal are examples; an Eventuate-based Create Order Saga uses its framework for state management, messaging, and recovery. section 10.2.2 section 11.1.2
