In a microservices architecture, if a step in your Saga fails, how would you ensure data consistency across all services involved? Can you provide a specific example of a compensation action?

In a Saga, if one step fails, data consistency is maintained by running compensating actions that logically undo the effects of previously completed steps, since there's no shared transaction to simply roll back.

Key Points

Example

In a travel booking saga covering flight, hotel, and car rental, if the car rental step fails after the flight and hotel were successfully booked, the saga triggers a "cancel flight" compensating action and a "cancel hotel" compensating action, returning the whole booking to an unbooked state.

// Compensate completed steps in REVERSE order, and make every
// compensation idempotent so it is safe to retry
public void bookTrip(Trip trip) {
    Deque<Runnable> compensations = new ArrayDeque<>();
    try {
        flights.book(trip);
        compensations.push(() -> flights.cancel(trip.id()));

        hotels.book(trip);
        compensations.push(() -> hotels.cancel(trip.id()));

        cars.book(trip);                     // fails
        compensations.push(() -> cars.cancel(trip.id()));
    } catch (Exception e) {
        while (!compensations.isEmpty()) {
            compensations.pop().run();       // hotel cancelled, then flight
        }
        tripRepository.markFailed(trip.id(), e.getMessage());
    }
}

Interview Tip

A concise interview answer is

When a saga step fails, I rely on predefined compensating actions for every previously completed step — for example, if a car rental booking fails after flight and hotel succeeded, I'd trigger cancellation events for both the flight and hotel to logically undo them. Since there's no shared transaction, consistency comes from these business-level compensations rather than a database rollback.

Last updated: 1 November 2026

Previous question: How would you handle distributed transactions in a microservices architecture? Explain the concept of the Saga pattern.

Next question: Describe how you dockerized a Spring Boot application. What were the steps, challenges, and benefits of moving to a containerized environment?

More Microservices - Level II interview questions

All Microservices - Level II questions