The Saga pattern addresses data consistency across microservices by breaking a business transaction into a sequence of local transactions, each owned by a different service, with compensating actions to undo prior steps if a later one fails.
In a travel booking saga, if the Flight and Hotel services succeed but the Car Rental service fails, compensating events are sent to cancel the already-confirmed flight and hotel bookings, returning the overall booking to a consistent "not booked" state.
// Choreography saga: each service listens for events and compensates on failure
// Hotel service: books the room after the flight is booked
@KafkaListener(topics = "flight-booked", groupId = "hotel-service")
public void onFlightBooked(FlightBooked e) {
try {
hotelRepository.book(e.tripId());
kafka.send("hotel-booked", e.tripId(), new HotelBooked(e.tripId()));
} catch (NoRoomsException ex) {
kafka.send("hotel-failed", e.tripId(), new HotelFailed(e.tripId()));
}
}
// Flight service: compensates when a later step fails
@KafkaListener(topics = "hotel-failed", groupId = "flight-service")
public void onHotelFailed(HotelFailed e) {
flightRepository.cancel(e.tripId()); // compensating transaction
kafka.send("trip-cancelled", e.tripId(), new TripCancelled(e.tripId()));
}A concise interview answer is
I use the Saga pattern — each service does its local transaction and emits an event, and if a later step fails, compensating transactions undo the earlier steps rather than relying on a distributed transaction. It keeps services independent while still giving the overall business process consistency, at the cost of needing to design compensating actions for every step up front.
Last updated: 1 November 2026
Previous question: You are tasked with creating a microservices architecture that requires service-to-service communication. How would Spring Cloud assist in this setup?
Next question: In microservices, would you choose a database per service or a shared database, and why?