42. Design E-commerce Cart and Checkout (Amazon / Flipkart)
1
The interview prompt2
42.1 What do you ask first?3
42.2 How big is this, really?4
42.3 What are the doors?5
42.4 What do we store?6
42.5 What does the whole system look like?7
42.6 How is the cart stored?8
42.7 How do you reserve inventory?9
42.8 How do you make order creation idempotent?10
42.9 How does the payment hand-off work?11
42.10 How does the checkout saga work?12
42.11 What would you say in the last five minutes?13
Score yourself14
Variations to practise15
Sources16
Code lab42. Design E-commerce Cart and Checkout (Amazon / Flipkart)
42.5 What does the whole system look like?
3 min read+10 XP
Step 4: High-level design (10 minutes)
Figure: the final design. The cart is fast and forgiving; checkout is careful and exact.
Placing an order, step by step:
- Preview: checkout re-reads current prices and offers for every cart item and shows any changes.
- The shopper taps Place order; the app sends an idempotency key for this attempt (42.8).
- The checkout orchestrator creates the order in state created, then reserves stock with conditional updates (42.7), each with a 10-minute hold.
- It asks the payment service to start payment with the gateway (with the order ID as the gateway's idempotency key) and moves the order to payment_pending (42.9).
- The gateway reports the result by webhook. On success: confirm the holds, mark the order confirmed, and publish
order_confirmedthrough the outbox to warehouses, email and loyalty (Chapters 22 and 23). On failure: release the holds and cancel. - If nothing arrives in 10 minutes, the order expires and stock is released.
Interviewer pushback:
- "Cash on delivery?" → No payment step: reserve stock, confirm the order immediately, and collect payment on delivery; returns and refusals release stock later.
- "The orchestrator crashes mid-checkout." → Its progress is in the orders table (the state machine); a recovery job resumes or compensates orders stuck in intermediate states, and expired holds release themselves.
Memory code: "Re-price, reserve, request payment, confirm or release."
Offer: "The interesting parts are cart storage, reserving inventory, idempotent orders, the payment hand-off, and the saga. Which would you like?"