What are compensating transactions, and how are they used in the Saga pattern?

A compensating transaction is an operation that semantically undoes the effect of a previously completed step in a Saga, used when a later step in the same business process fails.

Key Points

Example

In a trip-booking Saga, if reserving a flight and a hotel succeed but the car rental step fails, the compensating transactions cancel the hotel reservation and refund the flight booking, in that reverse order, returning the customer's booking state to what it was before the process began.

// Every forward step has a matching compensating step
public interface SagaStep {
    void execute(Trip trip);
    void compensate(Trip trip);      // semantic undo, e.g. cancel + refund
}

class HotelStep implements SagaStep {
    public void execute(Trip t) { hotelClient.reserve(t.id(), t.hotel()); }
    public void compensate(Trip t) { hotelClient.cancel(t.id()); }
}

class PaymentStep implements SagaStep {
    public void execute(Trip t) { paymentClient.charge(t.id(), t.total()); }
    public void compensate(Trip t) { paymentClient.refund(t.id()); }   // not a DB rollback
}

// Compensations must be idempotent: cancelling an already cancelled
// reservation should succeed, because a step may be retried.

Interview Tip

A concise interview answer is

A compensating transaction is the semantic undo for a step that already committed, like cancelling a reservation instead of rolling back a database transaction. In the Saga pattern, every step that mutates state needs one defined up front, and they run in reverse order when a later step fails, which is how the saga restores consistency without a distributed transaction.

Last updated: 1 November 2026

Previous question: Can you explain the difference between choreography and orchestration in Saga?

Next question: In what types of scenarios would the Saga pattern be useful?

More Microservices Design Patterns interview questions

All Microservices Design Patterns questions