Saga Pattern
Managing long-running business transactions across services with local transactions and compensations, comparing orchestration and choreography with a Java/Temporal example.
Intent
A saga is a sequence of local transactions, each in one service. If a step fails, the saga executes compensating transactions to semantically undo previous steps. It replaces distributed ACID transactions in microservice architectures. See Distributed Transactions for the broader context.
Orchestration vs choreography
| Orchestration | Choreography | |
|---|---|---|
| Control | Central orchestrator tells services what to do | Services react to each other's events |
| Visibility | Single place to see saga state | State spread across services |
| Coupling | Orchestrator knows participants | Participants know events |
| Best for | Complex flows, many steps, compliance | Simple flows, few steps |
sequenceDiagram participant O as Trip booking orchestrator participant F as Flights participant H as Hotels participant C as Cars O->>F: book flight F-->>O: booked O->>H: book hotel H-->>O: booked O->>C: book car C-->>O: failed (no availability) O->>H: cancel hotel (compensation) O->>F: cancel flight (compensation) O-->>O: saga FAILED, notify customer
Java example with Temporal (durable execution)
@WorkflowInterface
public interface TripBooking { @WorkflowMethod String book(TripRequest req); }
public class TripBookingImpl implements TripBooking {
private final BookingActivities act = Workflow.newActivityStub(BookingActivities.class,
ActivityOptions.newBuilder()
.setStartToCloseTimeout(Duration.ofSeconds(30))
.setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(5).build())
.build());
@Override
public String book(TripRequest req) {
Saga saga = new Saga(new Saga.Options.Builder().setParallelCompensation(false).build());
try {
String flight = act.bookFlight(req);
saga.addCompensation(act::cancelFlight, flight);
String hotel = act.bookHotel(req);
saga.addCompensation(act::cancelHotel, hotel);
String car = act.bookCar(req);
saga.addCompensation(act::cancelCar, car);
return "CONFIRMED";
} catch (ActivityFailure e) {
saga.compensate(); // runs registered compensations in reverse order
return "FAILED";
}
}
}Temporal persists workflow state, so the saga survives process crashes and resumes exactly where it stopped. AWS Step Functions, Azure Durable Functions and Camunda offer similar capabilities.
Design rules
- Each step and compensation must be idempotent.
- Some actions cannot be undone (sending an email): order steps so that irreversible steps come last ("pivot transaction").
- Use semantic locks (status PENDING) so other operations see in-progress state.
- Handle compensation failure: retry, then alert for manual resolution.
Pros and cons
| Pros | Cons |
|---|---|
| No distributed locks; services stay autonomous | Eventual consistency; intermediate states visible |
| Works across organizations and protocols | Compensation logic is business logic to design and test |
| Durable orchestrators make failures recoverable | Debugging choreography-based sagas is hard |
Interview questions
What is a pivot transaction?
The step after which the saga can no longer be rolled back, only completed forward. Steps before it are compensatable; steps after it must be retryable until success.
How do you test sagas?
Unit-test workflow logic with time skipping and mocked activities (Temporal test framework), contract-test each participant, and run failure-injection tests for each step's failure and compensation.