liveBook · Manning

Saga state

As explained in Microservices Patterns, Second Edition

Saga state is the durable data recording a saga instance's execution position and information needed to continue it.

Saga state is the durable record of a saga instance’s current execution position and the application data needed to invoke later steps or return a result. section 10.2.2

A saga state typically holds:

  • The current state-machine position, updated as participant responses drive transitions. section 10.2.2
  • Request parameters and participant results used as later command arguments or included in the system-command response. section 10.2.2
  • A durable database record with a unique ID, so the orchestrator can reload and resume execution after a crash. section 10.2.2
Listing 11.5 CreateOrderSagaState stores the application-specific saga state
public class CreateOrderSagaState {

  private Logger logger = LoggerFactory.getLogger(getClass());

  private long consumerId;
  private long restaurantId;
  private DeliveryInformation deliveryInformation;
  private List<MenuItemIdAndQuantity> lineItems;

  private Long orderId;
  private OrderDetails orderDetails;
  private long ticketId;


  private CreateOrderSagaState() {
  }

  public CreateOrderSagaState(long consumerId, long restaurantId,       #1
                              DeliveryInformation deliveryInformation,
                              List<MenuItemIdAndQuantity> lineItems) {
    this.consumerId = consumerId;
    this.restaurantId = restaurantId;
    this.deliveryInformation = deliveryInformation;
    this.lineItems = lineItems;
  }

  // Getters for the fields                         #2
  ...

  // Methods that update the state                  #3

  public void setOrderId(Long orderId) {
    this.orderId = orderId;
  }

  public void setOrderDetails(OrderDetails orderDetails) {
    this.orderDetails = orderDetails;
  }

  public void setTicketId(long ticketId) {
    this.ticketId = ticketId;

  void handleCreateTicketReply(CreateTicketReply reply) {
    logger.debug("getTicketId {}", reply.getTicketId());
    setTicketId(reply.getTicketId());
  }

The CreateOrderSagaState class illustrates application-specific fields initialized from the request, with getters and update methods used during saga execution. section 11.2.3

How Microservices Patterns, Second Edition covers it

How deep it goes
Traces state initialization, updates, persistence, and crash recovery, and illustrates application-specific state in a class.
Examples
Uses the Create Order Saga and CreateOrderSagaState, including request fields and participant-returned order data.
What you can do after
Build it from scratch by initializing application state from request parameters, exposing command arguments, and updating state with participant responses.
How it connects
Connects saga orchestration with state machines, saga participants, and system commands.

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.