liveBook · Manning

Finite-state machine

As explained in Microservices Patterns, Second Edition

A state machine is a computational model consisting of states and defined transitions between them in response to events or conditions.

A finite-state machine makes an execution flow explicit as states and transitions triggered by events. It has an initial state and one or more final states; states can have entry and exit actions, and transitions can have actions. Modeling a saga orchestrator this way makes its design, implementation, and testing easier. section 10.2.2

Create Order Saga

The orchestrator maps forward and compensating transactions to states such as CREATING_ORDER, RESERVING_CREDIT, and REJECTING_ORDER. Each working state has an entry action that invokes a participant, with transitions reflecting the operation’s possible outcomes. Final states record whether the order was approved, rejected, or failed to be created. section 10.2.2

Figure 10.10 The state machine model for the Create Order Saga
orchestration generic state machine

Follow the working states, outcome transitions, and final states of the Create Order Saga. section 10.2.2

The execution mechanism determines what actions and events mean in practice. In either approach, the orchestrator must maintain durable saga instance state, including application data passed between participants. section 10.2.2 section 10.2.4 section 10.2.5

Two execution approaches are:

  • Asynchronous messaging: entry actions send commands, and replies trigger transitions. A transactional outbox sends commands, while an idempotent consumer detects and discards duplicate replies. section 10.2.4
  • Synchronous request/response: entry actions make HTTP requests, and responses trigger transitions. If communication fails without a response, the machine remains in its state until a timeout triggers a transition back to that state and retries the request. An idempotency token helps the participant handle duplicates. section 10.2.5
Figure 10.16 The state machine for a saga orchestrator that invokes the participants using HTTP. HTTP responses trigger state machines. If there’s a communication failure, the state is unchanged and a transition to the same state occurs after a timeout.
orchestration http state machine

See how HTTP responses and retry timeouts drive transitions in the synchronous version. section 10.2.5

For an orchestration-based saga, use the model to make participant calls, outcomes, and recovery paths explicit. An orchestration framework such as Eventuate Platform or Temporal is a likely implementation choice; understanding the underlying state machine still helps when evaluating how it works. section 10.2.2 section 10.2.5

How Microservices Patterns, Second Edition covers it

How deep it goes
Explains states, actions, transitions, persistent instance state, and HTTP-driven execution with retries.
Examples
Illustrates a Create Order Saga with order creation, credit reservation, rejection, and distinct final outcomes.
What you can do after
Evaluate how to execute saga orchestration with persistent state, retries, and duplicate handling.
How it connects
Connects saga orchestration with transactional outbox, idempotent consumer, and idempotent REST endpoints.

Also explained in

Compare how 13 books explain finite-state machine →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.