Systems & Backend

Saga Pattern

also: compensating transactions

A distributed transaction as a chain of local ones — undo by compensation, not rollback.

A saga splits a cross-service transaction (book flight → charge card → reserve hotel) into local steps, each with a compensating action (refund, cancel). If step 4 fails, run the compensations for 1–3. Orchestrated (a coordinator drives) or choreographed (services react to events), sagas trade 2PC’s atomicity for availability — leaning hard on idempotency because steps and compensations will retry.

Worked example: instead of one distributed transaction, a saga runs local transactions in sequence (charge, reserve, book), and on a later failure runs compensating actions (refund, release) to undo the earlier ones. Gotcha: sagas are eventually consistent and expose intermediate states, and you must design a compensator for every step — there is no automatic rollback.