Design the checkout for an online shop selling 200,000 products. A shopper reviews their cart, enters an address and pays by card through an external payment provider. On normal days the shop takes 30 orders per second; during the Black Friday sale it peaks at 600 checkout requests per second, and popular items sell out in minutes. Shoppers double-click, mobile networks drop and retry, and the payment provider is sometimes slow: none of this may produce two charges, two orders, or an order for stock that does not exist.
Functional requirements
Persist carts and show up-to-date prices and stock at checkout
Reserve stock while the shopper pays, and release it if payment fails or times out
Charge the card through an external payment provider
Create exactly one order per checkout attempt and send a confirmation email
Let shoppers see the status of their order
Non-functional requirements
Never charge a shopper twice or create duplicate orders for one checkout
Never sell more units than are in stock
Checkout responds within 1.5 seconds at p95, including payment authorisation
Constraints and scale
Peak of 600 checkout requests per second on Black Friday
Payment provider allows 1,000 calls per second and takes about 250 ms per call
Stock reservations expire after 10 minutes
You will practise
Use idempotency keys end to end, from the browser to the payment provider
Keep order and stock changes consistent with database transactions
Move non-essential work (emails, fulfilment) off the checkout path with a queue
Show 3 hints
The browser retries a request whose response was lost. How does the server recognise it has already done the work?
Which steps must succeed before you answer the shopper, and which can happen a second later?
What happens to reserved stock if the shopper closes the tab?