Lesson 11 of 16
42. Design E-commerce Cart and Checkout (Amazon / Flipkart)

42.10 How does the checkout saga work?

3 min read+10 XP

Checkout spans services (offers, inventory, payments, orders), so there's no single database transaction. It's a saga (Chapter 17): local steps, each with a compensation, run by the orchestrator:

StepCompensation
Apply coupon (mark it used)Un-use the coupon
Reserve stockRelease the hold
Charge paymentRefund
Create order (confirmed)Cancel the order

In the chapter's code, the coupon is applied and stock reserved, then the charge fails; the saga undoes stock first, then the coupon, leaving both as they were.

Rules that make sagas safe:

  • Order steps so the hardest-to-undo comes last (charging money, then confirming; emails only after confirmation).
  • Every step and compensation is idempotent (keyed by order ID), because the orchestrator may retry after a crash.
  • Persist saga progress in the order's state, so a recovery job can resume or compensate.
  • Use the outbox (Chapter 23) to publish order_confirmed exactly when the order commits.

Memory code: "Step by step, with an undo for each, and money last."