Saga State & Compensation
The deep mechanics behind the saga: where the orchestrator keeps state so it survives a crash, how compensation unwinds committed steps in reverse, and how to thread one step's output to the next without coupling the services.
The Data chapter drew the happy path of a saga. This appendix is the three hard parts that picture glosses over: where the state lives, how compensation unwinds a failure, and how step B gets data that only step A produced, without coupling B to A.
Where the saga keeps its state
The orchestrator must survive a crash mid-flow, so in-memory state is a
non-starter: a pod restart would strand every in-flight saga with no idea what to
compensate. The workhorse option is a DB-backed state machine: one row per saga
instance holding its status, a step_index, and a context blob, with you
owning the transitions. The state update and the next command commit
together in one transaction: the same outbox discipline from the Data chapter,
applied to the saga’s own progress.
// SagaOrchestrator.java — DB-backed state machine; advance one step per call
private static final List<String> STEPS =
List.of("charge_payment", "reserve_stock", "book_shipping");
@Transactional // state + command commit together
public void advance(String sagaId) {
Saga s = repo.findForUpdate(sagaId); // row lock (SELECT … FOR UPDATE)
if (!"RUNNING".equals(s.getStatus())) return; // idempotent: already done/failed
String step = STEPS.get(s.getStepIndex());
var result = invoke(step, s.getContext()); // call the owning service
s.getContext().put(step, result); // accumulate output into context
s.setStepIndex(s.getStepIndex() + 1);
if (s.getStepIndex() == STEPS.size()) s.setStatus("COMPLETED");
}
// SagaService.java (excerpt), examples/17-sagas/quarkus: one transaction per step
// STEPS = charge_payment, reserve_stock, book_shipping; each SagaStep also names its inverse
private Outcome advanceOneStep(String sagaId) { // QuarkusTransaction.requiringNew()
SagaEntity saga = SagaEntity.findById(sagaId, LockModeType.PESSIMISTIC_WRITE); // row lock
if (saga.stepIndex >= STEPS.size()) { saga.status = "COMPLETED"; return Outcome.DONE; }
SagaStep step = STEPS.get(saga.stepIndex);
Map<String, Object> context = new HashMap<>(saga.context);
try {
var result = stepExecutor.execute(step.name(), context); // call the owning service
SagaLogEntity.of(sagaId, step.name(), "execute", result).persist();
context.put(step.name(), result); // accumulate output into the context
saga.context = context;
saga.stepIndex++; // one commit: state + log row
return Outcome.CONTINUE;
} catch (Exception e) {
SagaLogEntity.of(sagaId, step.name(), "failed", Map.of("error", e.getMessage()))
.persist();
saga.status = "COMPENSATING"; // hand off to the unwinder
return Outcome.COMPENSATE;
}
}
// SagaOrchestrator.cs — DB-backed state machine; advance one step per call
private static readonly string[] Steps =
["charge_payment", "reserve_stock", "book_shipping"];
public async Task AdvanceAsync(string sagaId)
{
await using var tx = await db.BeginTransactionAsync();
var s = await db.GetSagaForUpdateAsync(sagaId); // row lock
if (s.Status != "RUNNING") return; // idempotent
var step = Steps[s.StepIndex];
var result = await InvokeAsync(step, s.Context); // call the owning service
s.Context[step] = result; // accumulate output
s.StepIndex++;
if (s.StepIndex == Steps.Length) s.Status = "COMPLETED";
await tx.CommitAsync(); // state + command commit together
}
STEPS = ["charge_payment", "reserve_stock", "book_shipping"]
async def advance(saga_id: str):
async with db.transaction(): # state + command commit together
s = await db.get_saga(saga_id, for_update=True) # row lock
if s.status != "RUNNING":
return # idempotent: already done/failed
step = STEPS[s.step_index]
result = await invoke(step, s.context) # call the owning service
s.context[step] = result # accumulate output into context
s.step_index += 1
if s.step_index == len(STEPS):
s.status = "COMPLETED"
// saga_orchestrator.cc — DB-backed state machine; advance one step per call
constexpr std::array steps = {"charge_payment", "reserve_stock", "book_shipping"};
void advance(PGconn* conn, std::string_view saga_id) {
PQclear(PQexec(conn, "BEGIN")); // libpq: state + command commit together
auto s = get_saga_for_update(conn, saga_id); // row lock (SELECT … FOR UPDATE)
if (s.status != "RUNNING") return; // idempotent: already done/failed
auto step = steps[s.step_index];
auto result = invoke(step, s.context); // call the owning service
s.context[step] = result; // accumulate output into context
++s.step_index;
if (s.step_index == steps.size()) s.status = "COMPLETED";
update_saga(conn, s);
PQclear(PQexec(conn, "COMMIT"));
}
// saga_orchestrator.go — DB-backed state machine; advance one step per call
var steps = []string{"charge_payment", "reserve_stock", "book_shipping"}
func advance(ctx context.Context, pool *pgxpool.Pool, sagaID string) error {
tx, _ := pool.Begin(ctx) // state + command commit together
defer tx.Rollback(ctx)
s, _ := getSagaForUpdate(ctx, tx, sagaID) // row lock
if s.Status != "RUNNING" {
return nil // idempotent: already done/failed
}
step := steps[s.StepIndex]
result, _ := invoke(ctx, step, s.Context) // call the owning service
s.Context[step] = result // accumulate output into context
s.StepIndex++
if s.StepIndex == len(steps) {
s.Status = "COMPLETED"
}
updateSaga(ctx, tx, s)
return tx.Commit(ctx)
}
Because advance is idempotent and the row is locked, resuming after a crash is
trivial: on startup, scan for sagas still RUNNING and call advance again. It
picks up exactly where it left off.
Compensation unwinds a failed saga
There is no cross-service rollback: when book_shipping fails, the charge_payment
that already committed in the payment service is real money. So compensation
runs a defined semantic inverse for each step that completed (charge → refund,
reserve → release), in reverse order, and only for the steps that
finished.
static final Map<String,String> COMPENSATIONS = Map.of( // inverse per step
"charge_payment", "refund_payment",
"reserve_stock", "release_stock",
"book_shipping", "cancel_shipping");
@Transactional
public void compensate(String sagaId) {
Saga s = em.find(Saga.class, sagaId, LockModeType.PESSIMISTIC_WRITE);
// undo only the steps that actually completed, newest first
for (String step : reversed(STEPS.subList(0, s.getStepIndex()))) {
invoke(COMPENSATIONS.get(step), s.getContext()); // semantic inverse
}
s.setStatus("COMPENSATED");
}
// SagaService.java (excerpt), examples/17-sagas/quarkus: compensate in reverse
public void compensate(String sagaId) {
Saga saga = getSaga(sagaId); // stepIndex = completed steps
for (int i = saga.stepIndex() - 1; i >= 0; i--) { // newest first
SagaStep step = STEPS.get(i);
try {
var result = stepExecutor.execute(step.compensate(), saga.context());
logStep(sagaId, step.compensate(), "compensate", result); // own transaction
} catch (Exception e) {
logStep(sagaId, step.compensate(), "failed", Map.of("error", e.getMessage()));
}
}
QuarkusTransaction.requiringNew().run(() -> {
SagaEntity entity = SagaEntity.findById(sagaId);
entity.status = "COMPENSATED"; // Hibernate flushes the change at commit
});
}
static readonly Dictionary<string,string> Compensations = new() // inverse per step
{
["charge_payment"] = "refund_payment",
["reserve_stock"] = "release_stock",
["book_shipping"] = "cancel_shipping",
};
public async Task CompensateAsync(string sagaId, CancellationToken ct)
{
await using var tx = await db.Database.BeginTransactionAsync(ct);
var s = await db.Sagas.FromSqlInterpolated(
$"SELECT * FROM sagas WHERE id={sagaId} FOR UPDATE").SingleAsync(ct);
// undo only completed steps, newest first
foreach (var step in s.CompletedSteps().Reverse())
await Invoke(Compensations[step], s.Context, ct); // semantic inverse
s.Status = "COMPENSATED";
await tx.CommitAsync(ct);
}
COMPENSATIONS = { # inverse for each forward step
"charge_payment": "refund_payment",
"reserve_stock": "release_stock",
"book_shipping": "cancel_shipping",
}
async def compensate(saga_id: str):
async with db.transaction():
s = await db.get_saga(saga_id, for_update=True)
# undo only the steps that actually completed, newest first
for step in reversed(STEPS[: s.step_index]):
await invoke(COMPENSATIONS[step], s.context) # semantic inverse
s.status = "COMPENSATED"
// main.cpp (excerpt): COMPENSATING undoes the completed steps, newest first
if (status == "COMPENSATING") {
for (int i : cndp::saga::compensation_order(progress.step_index)) { // n-1 .. 0
auto& step = g_steps[i]; // charge_payment -> refund_payment, ...
if (!logStep(db.conn, sagaId, step.compensationName, "compensate",
step.compensate(context))) { abandon(); return; }
}
sm.process_event(cndp::saga::Compensated{});
setState(db.conn, sagaId, cndp::saga::status_of(sm), progress.step_index, nullptr);
exec(db.conn, "COMMIT"); // one transaction for the unwind
}
// A forward step that throws is logged, then StepFailed moves the saga
// from RUNNING to COMPENSATING; the next advance() runs the block.
catch (const std::exception& e) {
logStep(db.conn, sagaId, step.name, "failed", failResult);
sm.process_event(cndp::saga::StepFailed{});
setState(db.conn, sagaId, cndp::saga::status_of(sm), progress.step_index, nullptr);
}
// compensations — the semantic inverse for each forward step
var compensations = map[string]string{
"charge_payment": "refund_payment",
"reserve_stock": "release_stock",
"book_shipping": "cancel_shipping",
}
func compensate(ctx context.Context, pool *pgxpool.Pool, sagaID string) error {
tx, err := pool.Begin(ctx)
if err != nil {
return err
}
defer tx.Rollback(ctx)
s, err := getSagaForUpdate(ctx, tx, sagaID) // row lock
if err != nil {
return err
}
// undo only the steps that actually completed, newest first
done := steps[:s.StepIndex]
for i := len(done) - 1; i >= 0; i-- {
if err := invoke(ctx, compensations[done[i]], s.Context); err != nil { // inverse
return err
}
}
s.Status = "COMPENSATED"
return tx.Commit(ctx)
}
Compensations themselves can fail, so each must be idempotent and retryable: a refund that times out gets retried until it sticks, and a saga stuck compensating is an alert, not a silent loss.
When step B needs data only step A produced
A common source of bad designs: the orchestrator calls A, then B, but B needs a
value only A produced (a reservation_id, a warehouse region). The tempting wrong
answer is to have B call A directly, which recreates the exact service-to-service
coupling the architecture exists to avoid, and hides a dependency the orchestrator
can’t see.
The right answer is already in the code: A’s result goes into the saga’s
context blob (s.context[step] = result), and the orchestrator passes that
context into B. The data flows through the orchestrator, the only component that
legitimately knows the whole flow. B stays unaware of A, and because the context is
persisted with the saga state, it survives a crash too: resume rehydrates it for
free.
Build, run, observe
Force the unhappy path. Run the saga with book_shipping rigged to fail, and
confirm the database shows release_stock then refund_payment executing in that
order, and that cancel_shipping does not run, because shipping never completed.
Then kill the orchestrator pod mid-flow and restart it: the RUNNING saga should
resume from its persisted step_index and context, not start over. Reverse-order
inverses over only the completed steps, plus a clean resume, shows the saga
works.
cd examples/17-sagas/python # or any of the six language stacks
docker compose up -d --build --wait
cd ..
./verify.sh
verify.sh runs the language-agnostic end-to-end checks against the running stack
(11/11 in each of the six languages), asserting the happy-path saga runs
charge_payment, reserve_stock, and book_shipping in order with no compensation,
a saga with book_shipping rigged to fail ends COMPENSATED and compensates in
reverse order (release_stock then refund_payment), cancel_shipping never runs
because shipping never completed, and a saga force-set to RUNNING in Postgres
resumes to COMPLETED after the orchestrator container is stopped and restarted.