liveBook · Manning

Saga

As explained in Microservices Patterns, Second Edition

A distributed transaction pattern that coordinates a sequence of local transactions and, when necessary, compensating actions across services.

A saga implements a distributed command as a sequence of local transactions across services. Each step runs business logic within a service’s transaction boundary, typically using an ACID transaction. Because completed steps have already committed, a saga cannot rely on a database to roll back the whole operation or isolate it from concurrent operations. section 9.2 section 9.0

Use a saga when a command must update data owned by multiple services. For an operation confined to one service, a local ACID transaction is simpler. Compared with XA/2PC, sagas reduce runtime coupling and suit ephemeral service instances, but require explicit compensation and application-level concurrency controls. section 9.4 section 9.2.4

If a step fails because of a business-rule violation, coordination executes compensating transactions for earlier committed steps in reverse order. A compensation undoes a business effect; it need not erase every trace of the original action. Technical failures such as service crashes can instead be treated as temporary delays. section 9.2.1 section 9.2.3

Creating an order

The Order Service creates an order and calculates its total; the Customer Service then reserves credit. If the customer is invalid or lacks credit, the Order Service compensates by marking the order rejected. If credit is reserved, a final step approves the order. A semantic lock protects the order while the saga is in progress. section 9.2.1

Table 9.3 The third and final version of Create Order Saga implements a semantic lock that prevents other sagas or transactions from accessing the Order until the saga has completed.

Saga canvas

System operation:

createOrder(customerID, productId)

Step

Participant

Transaction

Compensating transaction

1

Order Service

(orderId, orderTotal) = createOrder(…​)

rejectOrder(orderId)

2

Customer Service

reserveCredit(customerId, orderTotal)

-

3

Order Service

approveOrder(orderId)

-

The saga canvas lays out the participants, forward transactions, and compensation for the order flow. section 9.2.1

A useful way to structure the steps is around the last possible business failure:

  • Compensatable transactions precede the pivot and have actions that undo their committed effects. section 9.2.2
  • The pivot is the last step that can fail a business rule. It is the go/no-go point and does not need compensation. section 9.2.2
  • Retriable transactions follow the pivot and are guaranteed to complete; transient technical failures are retried. section 9.2.2

Coordination invokes participants, passes data from the request and earlier steps, and selects the next forward or compensating action. Choreography distributes these decisions through events and works well for simple sagas, but becomes harder to follow as steps and participants multiply. Orchestration centralizes decisions in an orchestrator, making larger flows easier to understand at the cost of greater implementation complexity and an orchestration framework. section 9.2.3 section 10.3

For a synchronous request, waiting for the saga gives the client a complete response but increases runtime coupling. Returning a partial response avoids that coupling, but requires another way for the client to learn the final outcome. Sequential steps are easier to reason about; parallel steps can improve performance. section 9.2.3 section 9.4

A saga is not isolated, even if each local transaction is ACID. Concurrent sagas can produce anomalies such as lost updates and dirty reads. Design countermeasures for the business operation, such as semantic locks or updates that can be applied in any order, rather than assuming database isolation covers the entire flow. section 9.4

How Microservices Patterns, Second Edition covers it

How deep it goes
Explains compensation, pivot-based step structure, coordination choices, isolation countermeasures, and response trade-offs.
Examples
Uses an order-creation flow with credit reservation, rejection, approval, and a semantic lock.
What it assumes
Assumes familiarity with transactions and service boundaries while explaining why distributed commands need different transaction management.
What you can do after
Evaluate and design distributed command flows using compensation, coordination choices, and isolation countermeasures.
How it connects
Connects sagas to ACID transactions, eventual consistency, asynchronous messaging, semantic locks, and distributed transactions.

Also explained in

Compare how 3 books explain saga →

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.