liveBook · Manning

Compensating transaction

As explained in Microservices Patterns, Second Edition

A transaction that semantically undoes the effects of a previously committed transaction.

A compensating transaction semantically undoes the effects of a previously committed transaction. It is used when an operation spans multiple local transactions: once a step commits, a later failure cannot roll it back automatically. section 9.2 section 9.4

In a saga, a step that updates state and is followed by a step that can fail due to a business rule violation needs compensation. A business failure, such as insufficient credit, causes compensation; technical failures such as service crashes are treated as temporary delays. section 9.2.1 section 9.4

Rejecting an order

A Create Order saga first creates an order, then reserves customer credit. If the customer ID is invalid or credit is insufficient, rejectOrder() compensates for createOrder() by marking the order rejected. The committed creation is not erased; its business effect is reversed. section 9.2.1

Table 9.2 The second version of the Create Order Saga that adds a compensating transaction to step 1 that undoes the creation of the Order if credit cannot be reserved in step 2.

Saga canvas

System operation:

createOrder(customerID, productSpecification, quantity)

Step

Participant

Transaction

Compensating transaction

1

Order Service

(orderId, orderTotal) = createOrder(…​)

rejectOrder(orderId)

2

Customer Service

reserveCredit(customerId, orderTotal)

-

The saga canvas pairs the order-creation transaction with its compensating operation and shows the subsequent credit-reservation step. section 9.2.1

Compensation adds design complexity, especially because sagas lack isolation. Concurrent operations can see or affect intermediate state, so application-level countermeasures such as semantic locks may be needed. section 9.2.4 section 9.4

Avoid compensation when a simpler transaction boundary meets the requirements. For example, placing order creation and credit validation in one transaction and notification in a second can avoid compensation when business-rule violations occur only in the first transaction. A transaction per domain module reinforces loose coupling, but compensation can make it substantially harder to implement. section 6.2.4

How Microservices Patterns, Second Edition covers it

How deep it goes
Defines when compensation is needed and explains its complexity, isolation concerns, and transaction-boundary alternatives.
Examples
Uses order creation, credit reservation, and order rejection to illustrate compensation after a business-rule violation.
What you can do after
Evaluate when saga steps need compensation and when different transaction boundaries can avoid it.
How it connects
Connects sagas and local ACID transactions to rollback, isolation, and semantic locks.

Also explained in

Compare how 3 books explain compensating transaction →

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.