Skip to content
Ameen Abozaid
MID-031

Distributed workflows

  • 8 Lessons
  • 4 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Sagas
  • choreography vs orchestration
  • compensation

Learn first

4 h of practice (estimate)

Learning outcomes

  1. Model a business process as a saga with local transactions and compensations.
  2. Compare choreography and orchestration and choose for a given process.
  3. Implement an orchestrated saga with persisted state and timeouts.
  4. Design compensations that are idempotent and semantically correct.

Lessons

  1. 01Why distributed transactions don't scale
  2. 02Sagas
  3. 03Choreography
  4. 04Orchestration
  5. 05Compensation design
  6. 06Saga state, timeouts, and stuck sagas
  7. 07Demo: the order saga, happy path
  8. 08Demo: payment declined, compensations run

Lab and exercises

Lab

MID-031-LAB1 · Order saga

Outcomes: 1–4 · Time: ~4 h Implement an orchestrated saga in orders: place order → reserve stock (inventory) → authorize payment (payments) → confirm. Persist saga state with a deadline. On payment failure, release stock and cancel the order. Handle timeouts with a sweeper. Use the outbox and inbox from MID-029 for all messages.

Acceptance: every failure path is tested end to end: stock failure, payment declined, payment timeout, duplicate and late replies, orchestrator restart mid-saga.

Exercises (assessment: saga design for a new process, returns)

IDOutcomeTask
MID-031-EX011Explain in five sentences why 2PC across services is avoided and what replaces it.
MID-031-EX021Model "customer returns an item" as a saga: steps, local transactions, compensations, and non-undoable steps.
MID-031-EX032Compare choreography and orchestration for the returns saga and choose one with reasons.
MID-031-EX043Define the saga state enum and table for returns, including deadline and version.
MID-031-EX054Write an idempotent refund compensation that is safe if the payment was never captured.
MID-031-EX064A Reserve message arrives after Release for the same order. Describe the bug and the fix.
MID-031-EX073Define timeouts per step and what the sweeper does when each expires.
Show solutions
  • EX01 2PC holds locks across services, blocks on coordinator failure, multiplies unavailability, and often is unsupported. Use local transactions with compensations.
  • EX02 Steps: create return REQUESTED; receive item (inventory); refund (payments); restock; close. Compensations: cancel return; none for received item (flag for inspection); void refund request. A "refund email" is last and non-undoable.
  • EX03 Orchestration: five steps with branching (item rejected after inspection) and deadlines, so one owner of the flow is clearer. Choreography fits only if the flow stays linear.
  • EX04 RETURN_REQUESTED, ITEM_RECEIVED, REFUNDED, COMPLETED, COMPENSATING, FAILED; table return_saga(id, state, deadline, attempts, version).
  • EX05 Look up capture by return id; if none or already refunded, return; otherwise refund with the return id as the idempotency key and store the refund id.
  • EX06 Without a record, the late reserve re-reserves stock that nobody releases. Store a tombstone (cancelled reservation by order id) and reject the reserve.
  • EX07 Example: reserve 30 s (retry twice, then compensate); payment 2 min (void and cancel); overall 10 min (mark FAILED and alert).

Quiz Q01 to Q03 also cover outcome 1; outcome 3 is mostly verified through the lab.

Quiz

Outcomes: O1 saga model · O2 choreography vs orchestration · O3 orchestrated saga state · O4 compensation design.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1A saga consists of:O1
  2. 2A weakness of 2PC across services:O1
  3. 3Sagas lack:O1
  4. 4In choreography:O2
  5. 5A flow of 6 steps with branching and deadlines usually suits:O2
  6. 6A risk of orchestration:O2
  7. 7Why persist saga state?O3
  8. 8A reply arrives for a saga already in COMPENSATING. The handler should:O3
  9. 9A compensation must be:O4
  10. 10Which is a good compensation for a captured payment?O4

Practical part

Seven exercises MID-031-EX01 to EX07, the returns saga design, and MID-031-LAB1. Pass: at least six exercises, a returns design with compensations and timeouts, and a lab where all failure paths pass.

Common mistakes

Compensations that assume the forward step happened; no timeout handling.

Assessment

Saga design for a new process (returns).

Career relevance

Required in the MID capstone; orchestration (SRB-001).

Type to search levels, phases, modules and lessons.