Sagas
1. Executive Summary
A saga is a long-lived business transaction decomposed into a sequence of local transactions, each committing independently within its own service boundary. If a later step fails, the saga executes compensating transactions to undo or logically reverse the effects of prior steps—because there is no distributed rollback across already-committed local transactions.
Sagas were introduced by Garcia-Molina and Salem (1987) as an alternative to distributed locking protocols like 2PC. Two coordination styles dominate modern architectures: choreography (each service reacts to events and knows what to do next) and orchestration (a central coordinator directs steps and compensations). Sagas trade strong atomicity for availability, loose coupling, and scalability across microservices.
This chapter covers saga semantics, compensating vs semantic undo, choreography vs orchestration tradeoffs, idempotency and ordering requirements, failure modes (poison messages, incomplete compensation, lost events), comparison with 2PC and outbox patterns, production implementations (Temporal, Camunda, custom event-driven), and principal-level interview framing.
Try it: §25 Hands-On — Lab 010 saga orchestrator on :8093; then engineer guide for runtime walkthrough. Pairs with Lab 009 outbox.
2. Why This Topic Matters
Principal interviews frequently pose: "How do you maintain consistency across microservices without 2PC?" The saga pattern is the standard answer—but weak answers stop at "use events."
Strong candidates explain:
- Compensating transactions are not always true rollbacks—they are semantic reversals (issue refund, not delete row).
- Choreography scales teams but obscures global flow; orchestration centralizes visibility but risks a god-service.
- Every step must be idempotent because messages are at-least-once.
- Sagas are eventually consistent—intermediate states are visible.
Production failures include double refunds, orphaned orders, compensations that fail silently, and "saga stuck" states blocking customer support. Architects who design sagas without state machines, timeouts, and reconciliation build distributed workflows that are harder to debug than monolith transactions.
3. Problems Being Solved
| Problem | Monolith ACID | Saga |
|---|---|---|
| Cross-service atomic commit | Single transaction | Sequence of local commits |
| Service independence | Coupled schema | Autonomous bounded contexts |
| Availability during partial failure | Rollback all | Forward progress + compensate |
| Long-running workflows | Long DB locks | Async steps with durable state |
| Heterogeneous technology | One database | Polyglot persistence |
Sagas solve cross-service business process coordination without distributed locks. They do not provide instant global consistency, isolation of intermediate states from external readers, or automatic compensation if business rules forbid undo (e.g., shipped physical goods).
4. Assumptions and System Model
Assume microservices with private databases and asynchronous messaging (or synchronous calls with durable saga log):
- Each local transaction is ACID within one database.
- Messages are delivered at-least-once unless using exactly-once infrastructure (rare end-to-end).
- Compensations are application-defined and may be partial or delayed.
- Failures: Process crash, network partition, duplicate messages, slow consumers.
- Not Byzantine unless discussing fraud detection separately.
Not assumed: Compensations restore identical prior state—only business-equivalent state. Global serializability. Automatic saga completion without monitoring.
5. Essential Terminology
| Term | Definition |
|---|---|
| Saga | Sequence of local txs with compensating txs on failure. |
| Compensating transaction | Semantic undo of a completed local step. |
| Choreography | Decentralized: services react to events without central controller. |
| Orchestration | Central saga manager issues commands and tracks state. |
| Forward recovery | Retry failed step (transient error). |
| Backward recovery | Run compensations for completed steps. |
| Semantic lock | Business rule preventing conflicting actions during saga (e.g., no ship until paid). |
| Saga log | Durable record of saga state and step outcomes. |
| Pivot transaction | Last step after which compensation is impossible—switch to manual resolution. |
| Idempotency key | Dedupes retried step execution. |
Mnemonic: Saga = Steps forward, Compensate backward, Never global lock.
6. Core Mechanism
Orchestrated saga (happy path)
Figure 1: Orchestrator drives sequential local commits; each step durable before next.
Compensation path
Figure 2: Backward recovery—compensations run in reverse order of successful forward steps.
Choreography vs orchestration
Figure 3: Choreography flows through events; orchestration centralizes control and state.
7. Step-by-Step Walkthrough
Scenario: Travel booking—flight, hotel, car. Orchestrated saga.
| Step | Action | State if next fails |
|---|---|---|
| 1 | Book flight (committed) | Compensate: cancel flight |
| 2 | Book hotel (committed) | Compensate: cancel hotel, then flight |
| 3 | Book car fails | Run: cancel hotel, cancel flight |
| 4 | Saga ends COMPENSATED | Customer charged $0 net; may see brief flight hold |
Choreography equivalent:
| Event | Consumer action |
|---|---|
TripRequested | Flight svc books, emits FlightBooked |
FlightBooked | Hotel svc books, emits HotelBooked |
HotelBooked | Car svc fails, emits CarBookingFailed |
CarBookingFailed | Hotel listens, cancels; emits HotelCancelled |
HotelCancelled | Flight listens, cancels |
Risk: Implicit distributed state machine—harder to visualize than orchestrator's single log.
Parallel saga steps (advanced): When flight and hotel are independent, orchestrator may fan out steps 1 and 2 in parallel—reducing latency. Compensation order must still respect reverse completion order or explicit dependency graph: if hotel books before flight confirms, compensate hotel first. Parallelism without a documented DAG causes compensation races.
Saga state machine (explicit):
| State | Meaning | Allowed transitions |
|---|---|---|
STARTED | Saga initiated | → STEP_N_RUNNING |
STEP_N_RUNNING | Forward step in flight | → STEP_N_OK, COMPENSATING |
STEP_N_OK | Step committed | → next step or COMPLETED |
COMPENSATING | Running compensations | → COMPENSATED, FAILED |
COMPLETED | All forward steps done | terminal |
COMPENSATED | Rollback complete | terminal |
FAILED | Manual intervention required | terminal (ops) |
Persist state after every transition—crash recovery replays from last durable state, not from memory.
Customer-visible timeline example:
| Time | DB state (order svc) | DB state (payment) | Customer sees |
|---|---|---|---|
| T0 | — | — | Checkout clicked |
| T1 | PENDING order | — | Spinner |
| T2 | PENDING | AUTHORIZED | Still spinner |
| T3 | CONFIRMED | CAPTURED | Order confirmation email |
Between T1–T3, support tools must show in-flight saga—not "no order" or duplicate orders on retry.
Compensation ordering rules:
- Compensate in reverse order of successful forward steps (stack discipline).
- Skip compensation for steps that never committed.
- If step N compensation fails, do not compensate N-1 automatically unless business allows partial state—often halt and page ops.
- Idempotent compensation: calling
Refund(payment_id)twice must not double-refund—payment provider idempotency keys required.
8. Invariants and Guarantees
| Property | Type | Statement |
|---|---|---|
| Local ACID | Safety | Each step atomic within service DB |
| Compensation completeness | Safety | Application responsibility—not automatic |
| Global atomicity | Not provided | Intermediate states visible |
| Eventual business consistency | Liveness-oriented goal | After saga completes or compensates |
| No duplicate side effects | Safety (with idempotency) | Requires idempotent steps |
| Termination | Liveness | Requires timeouts, DLQ, human escalation |
Semantic vs physical undo: DELETE order vs status=CANCELLED—compensations are domain-specific.
9. Failure Scenarios
Scenario 0: Cyclic compensation dependency
Setup: Choreographed saga where Service A waits for B's compensation event while B waits for A's.
Effect: Deadlock at application level—no progress.
Mitigation: Orchestrator breaks cycle; define acyclic compensation graph; timeout to manual queue.
Scenario 1: Compensation fails
Setup: Payment refunded fails after inventory release failed.
Effect: Inconsistent state—customer charged, no inventory.
Mitigation: Retry with backoff; saga log marks COMPENSATION_FAILED; alert; manual playbook; reconciliation job.
Scenario 2: Duplicate message
Setup: ChargePayment delivered twice without idempotency.
Effect: Double charge.
Mitigation: Idempotency key on payment_id; dedup table.
Scenario 3: Lost event (choreography)
Setup: HotelBooked never delivered; car never booked; flight stuck.
Effect: Saga hung; customer billed for flight only.
Mitigation: Outbox, CDC, orchestrator timeout, state query API.
Scenario 4: Out-of-order events
Setup: PaymentRefunded arrives before PaymentCompleted processed.
Effect: Invalid state transition.
Mitigation: Versioned saga state; reject invalid transitions; partition by saga_id.
Scenario 5: Visible dirty intermediate state
Setup: Order created (visible) before payment; customer sees unpaid order.
Effect: UX confusion—not a bug if documented; support tickets.
Mitigation: PENDING status hidden from catalog; semantic locks.
10. Performance Characteristics
| Factor | Impact |
|---|---|
| Async steps | Higher latency than sync monolith—acceptable for many flows |
| Message overhead | Each step: publish + consume + DB |
| Orchestrator | Single coordination bottleneck—scale horizontally with sharded saga_id |
| Compensation | Extra round trips—failure path slower |
| Choreography fan-out | N services subscribe—broker throughput |
Compared to 2PC: Lower lock hold time, better availability; higher complexity and eventual consistency window.
11. Scalability Limits
- Orchestrator state store grows with active sagas—partition by saga_id.
- Choreography debugging doesn't scale with team count—needs tracing (OpenTelemetry).
- Compensation storms during outages amplify load.
- Human-in-the-loop steps don't automate—pivot to manual queue.
12. Operational Considerations
- Saga dashboard: active, stuck, compensating counts.
- Timeouts per step with escalation policy.
- DLQ for poison messages; replay tooling with idempotency.
- Correlation ID (
saga_id,trace_id) across all services. - Reconciliation batch jobs compare payment provider vs order DB nightly.
- Runbooks for
COMPENSATION_FAILEDand pivot transactions.
13. Security Considerations
- Orchestrator privilege: High-trust component—authenticate internal commands.
- Event tampering: Sign or encrypt sensitive saga commands on bus.
- Replay attacks: Idempotency keys bound to principal and time window.
- Compensation authorization: Refund endpoint must verify saga state server-side.
14. Cost Considerations
- Engineering: State machines, idempotency, monitoring—higher than monolith.
- Infrastructure: Workflow engine (Temporal), message broker, extra storage for saga log.
- Support cost: Intermediate states confuse users without clear UX.
- Saved cost: Avoids 2PC coupling, coordinator HA, cross-DB locks.
15. Production Implementations
Temporal / Cadence
Durable workflow execution—orchestration with automatic retries, timers, saga-style compensation via workflow code. Implementation choice for long-running sagas.
Camunda / Zeebe
BPMN-based process orchestration—visual flows; enterprise workflow.
AWS Step Functions
State machine as orchestrator—serverless; step limits and cost model apply.
Custom event-driven (Kafka + choreographed handlers)
Common in microservices—requires rigorous outbox and idempotency discipline.
NestJS / microservices patterns
Lightweight sagas via message patterns—team owns correctness.
eBay / large-scale choreographed sagas
Historical microservices literature describes event-driven sagas at scale with rigorous idempotency and compensating business processes—anecdotal operational experience emphasizing that choreography requires strong conventions (event schemas, versioning, ownership) to remain comprehensible as service count grows.
Banking and payment sagas
Payment networks often use state machines with explicit states (AUTHORIZED, CAPTURED, REFUNDED) rather than physical deletes. Compensation is a new forward transaction (refund) not an undo of capture—regulatory audit requires append-only payment history. Architects on financial paths should never assume DELETE FROM payments is a valid compensating action.
Saga timeout hierarchies
Production orchestrators define step timeout < saga timeout < business SLA. Example: payment step 30s, checkout saga 5m, customer-facing checkout 10m. Exceeding saga timeout transitions to REQUIRES_MANUAL_INTERVENTION with paging—not infinite retry loops that amplify outages.
Testing matrix:
| Test | Purpose |
|---|---|
| Fail each step once | Compensation order correct |
| Duplicate every message | Idempotency holds |
| Slow step past timeout | Escalation fires |
| Compensation fails | Alert + stuck state visible |
| Concurrent sagas same aggregate | Serialization or conflict policy |
16. Alternatives and Tradeoffs
| Pattern | Coupling | Consistency window | Complexity |
|---|---|---|---|
| Saga (orch) | Medium (orchestrator) | Visible intermediate | State machine |
| Saga (choreo) | Low between services | Same | Distributed debugging hard |
| 2PC/XA | High | None (atomic) | Blocking, ops |
| Outbox + process manager | Medium | Async | Good default combo |
| Monolith | High internal | Immediate | Doesn't scale teams |
| Event sourcing | Low | Replayable history | Steep learning curve |
17. Common Misconceptions
| Misconception | Reality |
|---|---|
| "Saga = 2PC with events" | No global atomicity; compensations are semantic. |
| "Compensation = DELETE" | Often status flip, refund API, release hold. |
| "Choreography is always better" | Loses visibility; orchestration aids ops. |
| "Exactly-once saga" | At-least-once + idempotency = effective once. |
| "Saga hides intermediate state" | Other services/users may observe partial completion. |
| "Failed step auto-rolls back DB" | Prior steps already committed—must compensate. |
18. Principal Architect Perspective
- Draw the state machine before choosing choreography vs orchestration.
- Define pivot points where compensation ends and humans intervene.
- Mandate idempotency keys on every saga step API.
- Pair with outbox for reliable event emission from local commits.
- Nightly reconciliation catches what sagas miss—belt and suspenders.
Governance: Establish an integration team or architecture guild that owns saga event schemas (OrderCreatedV1), compensation contracts, and breaking-change policy. Without this, choreographed systems become incomprehensible graphs where no one knows the full checkout flow. Version every event; consumers must tolerate unknown fields and dual-subscribe during migrations.
Business stakeholder communication: Sagas are eventually consistent—legal and product must accept that "order confirmed" may lag "payment captured" by seconds. Document SLAs for saga completion and what customer support should say during PENDING states. Escalation paths for FAILED sagas should be defined before launch, not during the first production incident.
19. Architecture Review Exercise
Scenario: Choreographed checkout: Order → Payment → Shipping via Kafka events; no central saga store; compensation = OrderCancelled event.
Review prompts:
- How detect stuck saga after 1 hour?
- Duplicate
PaymentCompletedhandling? - Shipping reads order before payment event—race?
- Payment compensated but shipping already dispatched?
- Redesign with orchestrator + semantic lock on ship?
Expected findings: Add saga_id state table or workflow engine; idempotency; cannot_ship_until_paid invariant; pivot manual for shipped goods.
20. Whiteboard Explanation
90-second version:
"A saga breaks a business transaction into local database transactions per service. Each step commits on its own—no distributed rollback. If step three fails, you run compensating transactions in reverse order—refund payment, cancel order—not database UNDO. Orchestration uses a central coordinator with a saga log; choreography uses events where each service knows its role. Compensations are semantic—cancel reservation, not delete row. Messages are at-least-once, so every step must be idempotent. Sagas trade strong consistency for availability compared to 2PC. You see intermediate states, so UX and monitoring matter. Pair with outbox for reliable events and reconciliation jobs for money paths."
21. Interview Questions
-
What is a saga?
- Signals: Local txs sequence; compensate on failure; no global lock.
-
Compensation vs rollback?
- Signals: Semantic undo after commit; not MVCC rollback.
-
Choreography vs orchestration?
- Signals: Event-driven decentralized vs central coordinator.
-
Why idempotency in sagas?
- Signals: At-least-once delivery; duplicate step safety.
-
Saga vs 2PC?
- Signals: Eventual vs atomic; availability vs blocking.
-
What is a pivot transaction?
- Signals: Point of no compensation—manual resolution.
-
Visible intermediate state—problem?
- Signals: UX, support; mitigate with PENDING status.
-
How handle compensation failure?
- Signals: Retry, alert, manual playbook, reconciliation.
-
Semantic lock example?
- Signals: Don't ship until payment confirmed.
-
Design flight+hotel booking saga.
- Signals: Order of steps, compensation order, failure points.
-
Temporal vs Kafka choreography?
- Signals: Durable workflow vs event graph; ops visibility.
-
Lost event in choreography?
- Signals: Timeout, outbox, orchestrator preferred for critical paths.
-
Saga and CQRS?
- Signals: Read models may lag; project from events.
-
Testing sagas?
- Signals: State machine tests, chaos on message delay, property-based order.
-
Can saga steps run in parallel?
- Signals: Yes with DAG; compensation order must respect dependencies.
-
What state should support tools show mid-saga?
- Signals: In-flight status, saga_id, last completed step—not silent failure.
Scoring rubric (principal loop):
| Dimension | Strong | Weak |
|---|---|---|
| Atomicity scope | Local ACID per step | "Distributed transaction" hand-wave |
| Failure path | Compensation order + idempotency | "Roll back" |
| Ops | Timeouts, stuck detection, reconciliation | No monitoring |
| Style choice | Choreo vs orch tradeoffs named | One-size-fits-all |
22. Interview Follow-Ups
-
Customer double-charged in saga—debug steps?
- Signals: Idempotency audit, payment provider id, saga log, duplicate delivery.
-
When refuse saga and use single DB?
- Signals: Strong invariants, small team, no scale-out need.
-
Saga across 12 services—orchestrator bottleneck?
- Signals: Shard orchestrator, async steps, simplify graph.
22A. Production Low-Level Design (Interview + Game Day)
Orchestrated checkout saga with compensations. Production LLD and Game Day Standard.
State machine (LLD)
| Step | Forward | Compensate |
|---|---|---|
| 1 | Reserve inventory | Release reservation |
| 2 | Authorize payment | Void / refund |
| 3 | Create shipment | Cancel shipment |
Step-by-step flow:
| Step | Notes |
|---|---|
| 1–3 | Each local TX + outbox/command; orchestrator persists saga state |
| 4 | Compensations are business undo, not DB rollback |
| * | Idempotent steps; timeouts → unknown → status before compensate |
API sketch
POST /sagas/checkout → {saga_id}; GET /sagas/{id} status; commands idempotent by saga_id+step.
22B. Game Day Playbooks
| # | Drill | Inject | Detect | Mitigate | Verify |
|---|---|---|---|---|---|
| GD-1 | Fail after pay before ship | Kill ship service | Saga stuck Paid | Compensate payment + inventory | No orphan charge |
| GD-2 | Duplicate step message | Redeliver reserve | Idempotent noop | — | Single reservation |
| GD-3 | Compensate fails | Refund 5xx | Compensating alarm | Retry / manual | Ops runbook clears |
GD-1 — Mid-saga failure
23. Strong Answer Example
Question: "Design checkout across order, payment, inventory services."
Answer this as a 45-minute design discussion using the PRACTICE framework. Below is how a strong student sounds aloud—saga + orchestration, not XA, and compensate ≠ DB rollback.

Figure A: PRACTICE map — Problem → Requirements → Assumptions → Capacity → Topology → Invariants → Constraints → Evolution.
Student interview script (PRACTICE)
P — Problem framing
"Checkout spans three services / three databases: Order, Inventory, Payment. We can't use XA/2PC across independently deployed services—blocking, coupling, and the card PSP isn't an XA participant anyway.
So I'd design a saga: each step is a local ACID transaction; on failure we run compensating transactions in reverse—semantic undo, not distributed rollback.
Coordination: For checkout I'd pick orchestration—a durable workflow / saga table keyed by
checkout_id—so ops can see one state machine. Choreography works but loses global visibility on a money path.Short answer: Orchestrated saga: create
PENDINGorder → reserve inventory (hold + TTL) → capture payment → markPAID/ confirm stock—with idempotent steps, timeouts, and reconciliation."
R — Requirements
| Requirement | Design implication |
|---|---|
| No oversell | Conditional reserve / hold before confirm |
| No orphan charge | Don't capture until reserve OK (or auth+capture split) |
| No double charge on retry | Idempotency key = checkout_id (or payment intent id) |
| Customer UX | Hide or clearly label PENDING; don't show false "paid" |
| Support / ops | Saga dashboard; stuck → page; manual pivot after ship |
| Ship only if paid | Semantic lock: dispatch consumes PAID only |
"I'd clarify: physical goods vs digital? Auth-then-capture vs immediate charge? Multi-warehouse inventory?"
A — Assumptions (state aloud)
| Assumption | Why it matters |
|---|---|
| Separate DBs per service | No shared transaction |
| At-least-once messaging | Every step must be idempotent |
| Payment via external PSP | Compensations = refund/void, not DELETE |
| Inventory is soft-hold then confirm | TTL releases abandoned carts |
| Shipping is later / out of saga or after pivot | Can't auto-unship easily |
"I'd say out loud: messages are at-least-once; effective exactly-once = idempotent handlers."
C — Capacity
Checkout QPS (say 1k peak) → orchestrator must shard / partition by checkout_id
Inventory hot SKU → row locks / conditional UPDATE; watch contention
Payment PSP p99 → step timeout < saga timeout < UX SLA
Hold TTL (e.g. 15m) → balance conversion vs abandoned-cart waste
Reconciliation nightly + lag alerts → mandatory for money
"I'd size holds and timeouts to conversion, not leave reservations forever."
T — Topology (draw this)

Figure B: Orchestrator drives forward steps; dashed path is backward recovery (release hold, cancel order; refund if charged).
Happy path (my default order):
| Step | Local TX | Outbox / result |
|---|---|---|
| 1 | Order: insert PENDING | OrderCreated |
| 2 | Inventory: reserve if available >= qty | Hold id + TTL |
| 3 | Payment: capture/charge with idempotency key | PaymentCaptured |
| 4 | Order → PAID; Inventory → confirm hold | CheckoutCompleted |
Why reserve before pay: Prefer failing on stock before charging; reduces refund volume. Alternate (pay then reserve) is OK if product wants payment-first—then inventory failure must refund/void.

Figure C: State machine, reverse compensations, and common pitfalls (idempotency, holds, TTL, pivot).
| Forward step | Compensation |
|---|---|
Create PENDING order | Cancel / expire order |
| Reserve stock | Release hold |
| Capture payment | Refund / void (idempotent) |
| Confirm reservation | Rare—usually after pivot; manual if shipped |
Choreography vs orchestration: I'd use orchestration for checkout (visibility, timeouts, single saga log). Choreography OK for simpler fan-out; still need outbox + idempotency + a way to detect stuck flows.
I — Invariants (safety)
"Non-negotiable:
- No XA across the three services.
- Each step local ACID + outbox for reliable handoff.
- Idempotent forward and compensate APIs.
- Compensations in reverse of successful steps; skip steps that never committed.
- Holds have TTL; abandoned checkout must release stock.
- Pivot: after ship (or irreversible fulfill), stop auto-compensate—human / support playbook.
- Reconciliation payment ledger ↔ orders ↔ inventory holds."
C — Constraints (second C)
| Constraint | Design response |
|---|---|
| PSP latency / outages | Auth hold + capture; retries with same key; degrade gracefully |
| Hot SKU races | Conditional UPDATE … WHERE available >= qty; or queue per SKU |
| Orchestrator crash mid-saga | Durable saga state; resume; never assume in-memory only |
| Compensation fails | Halt → COMPENSATION_FAILED alert; don't silently continue |
| Poison messages | DLQ + replay with same idempotency key |
| Customer retries "Buy" | Client/checkout_id dedupe returns same saga |
E — Evolution (prove it)
Phased delivery:
| Phase | Deliverable |
|---|---|
| M1 | Saga state machine + ADR (orch vs choreography; step order) |
| M2 | Order + outbox; inventory reserve/release with TTL |
| M3 | Payment idempotent charge/refund; wire orchestrator |
| M4 | Timeouts, dashboard, COMPENSATION_FAILED paging |
| M5 | Chaos: kill orch mid-pay; duplicate events; inventory fail |
| M6 | Nightly reconcile + support tools for stuck checkouts |
Failure modes:
| Failure | Mitigation |
|---|---|
| Double charge | Idempotency key on PSP + local payment row unique |
| Oversell | Conditional reserve; confirm only after pay |
| Orphan hold | TTL + sweeper + metrics on aged holds |
| Paid but order not updated | Orchestrator retry; reconcile job |
| "Roll back via Kafka" wishful thinking | Explicit compensations + state machine |
Observability: saga state counts, step latency, compensation rate, hold age, unpaid/paid mismatch, DLQ depth, PSP idempotency conflicts.
Closing line (interview)
"So: orchestrated saga—not XA. Create
PENDINGorder, reserve inventory with TTL, charge with idempotency keycheckout_id, then mark paid and confirm stock. Failures run compensations in reverse. Every step is local ACID + outbox; messages are at-least-once so handlers are idempotent. I'd add step/saga timeouts, a dashboard for stuck flows, shipping gated onPAID, and reconciliation. After an irreversible pivot like ship, humans take over—not blind auto-undo."
Scoring signals
| Strong | Weak / red flag |
|---|---|
| Saga + local TX + compensate | "Just use Kafka / 2PC / roll back" |
| Orchestration rationale for checkout | Pure choreography with no stuck story |
| Idempotency keys | Ignores duplicates |
| Hold TTL + reverse compensations | Physical DELETE as undo |
| Pivot + reconciliation | No money/ops follow-through |
| Rejects XA with reason | Proposes distributed transaction casually |
Compact outline (if time-boxed to 2 minutes)
"Orchestrated saga, not XA. PENDING order → reserve stock (TTL) → idempotent pay → confirm. Compensate in reverse. Outbox per step. Timeouts, dashboard, reconcile. Shipping only after PAID. Pivot to humans after irreversible fulfill."
24. Weak Answer Example
Question: "Design checkout across order, payment, inventory services."
"Use Kafka events between services. If something fails, roll back."
Why weak / red flags: No local-commit reality, no compensation semantics, no idempotency, no saga state/timeouts, no stuck-flow or reconciliation story—treats events as distributed rollback.
25. Hands-On Exercise
Lab 010: Saga Orchestration (runnable)
Full hands-on lab at labs/lab-010-saga-orchestration/ — Go HTTP API on port 8093, orchestrator + in-process participant stubs (payment → inventory → shipping).
cd labs/lab-010-saga-orchestration
go test ./... -v
docker compose -p lab010 -f docker/docker-compose.yml up --build -d
curl http://localhost:8093/health
chmod +x scripts/demo_saga.sh && ./scripts/demo_saga.sh
Demo flow (landing page at http://localhost:8093/):
| Step | Endpoint | What happens |
|---|---|---|
| 1 | POST /v1/sagas | Payment → inventory → shipping → completed |
| 2 | POST /v1/chaos/inventory-fail | Enable inventory failure injection |
| 3 | POST /v1/sagas | Inventory fails → compensate_payment → compensated |
| 4 | POST /v1/chaos/reset | Clear chaos flag |
| 5 | POST /v1/sagas/{id}/crash | Simulate orchestrator crash after payment |
| 6 | POST /v1/sagas/{id}/recover | Resume from saga log |
Complements Lab 009 outbox — outbox per step, orchestrator for global flow.
Engineer guide: how the local stack works
This section documents the runtime behavior of lab-010 as you would read it in a production runbook — component boundaries, saga state machine, and how to verify compensation.
Runtime topology
| Process | Port / entrypoint | Responsibility |
|---|---|---|
| Saga API | :8093 — go run ./src/main.go --serve | HTTP orchestrator; drives saga state machine |
| Payment stub | in-process | ReservePayment / CompensatePayment (idempotent by key) |
| Inventory stub | in-process | ReserveInventory — chaos flag fails next call |
| Shipping stub | in-process | CreateShipment — final forward step |
Browser entrypoints: http://localhost:8093/ (HTML landing), http://localhost:8093/docs (Swagger UI), http://localhost:8093/health.
API contract — POST /v1/sagas
Request body
| Field | Rule |
|---|---|
product_id | Required — logical product being ordered |
idempotency_key | Optional — duplicate key returns same saga (no double charge) |
State transitions (happy path)
started → payment_reserved → inventory_reserved → shipped → completed
Compensation path (inventory failure after payment)
payment_reserved → compensating → compensated
Each transition is appended to Saga.Log — inspect via GET /v1/sagas/{id}.
Chaos and recovery
| Endpoint | Purpose |
|---|---|
POST /v1/chaos/inventory-fail | Next saga fails at inventory — triggers compensation |
POST /v1/chaos/reset | Disable failure injection |
POST /v1/sagas/{id}/crash | Simulate orchestrator crash mid-saga |
POST /v1/sagas/{id}/recover | Resume from last committed log entry |
Verify compensation: GET /health → compensate_calls: 1, sagas_compensated: 1.
Contrast with Lab 009: outbox ensures reliable events; saga ensures multi-step business process consistency via compensations. Production systems often combine both (outbox per step, orchestrator for global flow).
Self-study checklist (optional)
- Model checkout as state machine (diagram in lab README).
- Run happy path; inspect saga log transitions.
- Inject inventory failure; verify compensation order.
- Retry same
idempotency_key— observe deduplication. - Simulate crash + recover — no double payment reservation.
- Document pivot: what if compensation impossible (e.g., shipped goods)?
26. Knowledge Check
- Saga compensations run in? (Reverse order of successful steps.)
- Choreography coordination via? (Events, no central controller.)
- Why not physical DELETE for compensate? (Already committed; business semantics.)
- At-least-once requires? (Idempotent handlers.)
- Saga provides global ACID? (No.)
27. Flashcards
| # | Front | Back |
|---|---|---|
| 1 | Saga | Local tx sequence + compensations. |
| 2 | Compensation | Semantic undo of committed step. |
| 3 | Choreography | Event-driven decentralized saga. |
| 4 | Orchestration | Central coordinator + saga log. |
| 5 | Backward recovery | Compensate prior steps on failure. |
| 6 | Forward recovery | Retry failed step. |
| 7 | Pivot transaction | No auto-compensation beyond here. |
| 8 | Semantic lock | Business guard during in-flight saga. |
| 9 | vs 2PC | Available, eventual, not atomic global. |
| 10 | Idempotency key | Dedup retried saga steps. |
| 11 | Garcia-Molina 1987 | Original saga paper. |
| 12 | Intermediate state | Visible—design UX accordingly. |
28. Cheat Sheet
SAGA
Forward: local ACID steps
Backward: compensate in reverse
No global rollback
STYLES
Choreography: events, loose coupling, hard debug
Orchestration: central state, easier ops
REQUIREMENTS
Idempotent steps
saga_id correlation
Timeouts + DLQ
Reconciliation for money
VS 2PC
Saga: eventual, available
2PC: atomic, blocking
OPS
Stuck saga dashboard
COMPENSATION_FAILED runbook
29. Related Concepts
- Two-Phase Commit — alternative with stronger atomicity
- Transactional Outbox — reliable events for saga steps
- Idempotency — required for saga steps
- Messaging and Streaming — transport for choreographed sagas
- Microservices — typical saga deployment context
30. References
Primary sources
- Garcia-Molina, H., & Salem, K. (1987). "Sagas." SIGMOD — original saga definition.
- Gray, J., & Reuter, A. (1993). Transaction Processing — contrast with 2PC.
Production and engineering
- Chris Richardson, Microservices.io — Saga pattern — choreography/orchestration catalog.
- Temporal Documentation — Workflow patterns — durable orchestration.
- Martin Kleppmann, DDIA — Chapter 9 distributed transactions and streams.
Distinction
| Claim type | Source |
|---|---|
| Saga definition | Garcia-Molina & Salem (1987) |
| Orchestration patterns | Richardson; Temporal docs |
| Comp vs 2PC tradeoffs | Kleppmann; engineering practice |