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


