Use case · E-commerce
Flash sale checkout
A limited drop opens and every buyer hits checkout in the same second. Watch one sale go through, lock up under a crowd, then hold with a queue and per-unit holds.
Used by apps like Shopify stores, Nike SNKRS, Supreme, Amazon Lightning Deals and Zalando
Happy path · break it · fix it
Flash sale checkout: sequence diagram
Flash sale checkout, shown as messages between Buyer (The buyer's browser or app), API (The load balancers at the edge and the checkout application behind them), Inventory (The inventory service and the database rows that hold stock), Payments (The payment provider that authorises the card), Orders (The order service that records the finished purchase).
Happy path: 8 steps.
- Buyer → API: POST /buy. One buyer presses Buy, and the app sends a single checkout request.
- API → Inventory: reserve 1. Before any money moves, checkout sets one unit aside for this buyer for ten minutes.
- Inventory → API: unit held (reply). The unit is held, so nobody else can buy it while this buyer pays.
- API → Payments: charge card. Only now is the card charged, with the unit safely on hold.
- Payments → API: approved (reply). The payment provider approves the charge.
- API → Orders: create order. The order is written once, after the payment has succeeded.
- API → Inventory: claim unit. The hold becomes a sale: the unit leaves stock for good and the timer stops.
- API → Buyer: 200 paid (reply). The buyer sees a confirmation, about a second after pressing Buy.
Result: One buyer, one unit, one charge, one order.
Break it: 9 steps.
- Buyer → API: POST /buy (many at once). The drop opens and a crowd presses Buy in the same second.
- API → Inventory: reserve 1 (many at once). Every checkout goes straight through and asks for a unit of the same product.
- Inventory: 1 stock row, 3 writers. Stock is one row with a count, so every buyer must change that row and they line up on its lock.
- Inventory → API: unit held (reply). One buyer gets the lock and a unit; the others are still waiting behind it.
- Inventory → API: timeout (never arrives) (reply) (many at once). The waiting checkouts run out of time before the lock is free, so no answer comes back.
- API → Buyer: 504 (reply) (many at once). Those buyers see an error page, with stock still on the shelf.
- Buyer → API: retry (many at once). They refresh at once, and newcomers keep arriving, so the request count goes up.
- API → Inventory: reserve 1 (many at once). Each retry is another writer on the same locked row, so the line gets longer.
- API → Orders: create order (never arrives). Orders share the stalled database, so even the buyer who holds a unit cannot finish.
Result: Checkout is stuck behind one row and nobody can finish buying.
Fix it: 10 steps.
- Buyer → API: POST /buy (many at once). The same drop, the same crowd, the same second.
- API: fair queue: 2 go in. The edge lets buyers in at a steady rate, first come first served; the third buyer waits in line.
- API → Inventory: reserve 1 (many at once). Stock is one row per unit: each buyer locks a different free row and skips rows already taken.
- Inventory → API: units 7, 8 (reply) (many at once). Both get a unit at once, since neither had to wait for the other's lock.
- API → Payments: charge card. The first buyer pays for unit 7.
- Payments → API: approved (reply). The charge is approved while the hold is still running.
- API → Inventory: claim 7. Unit 7 is sold, and its hold ends as intended.
- Inventory: unit 8 back in stock. The second buyer walked away, so that hold runs out and unit 8 returns to stock by itself.
- API → Inventory: reserve 1. The third buyer's turn comes, and the freed unit 8 is there to reserve.
- API → Buyer: 200 paid (reply) (many at once). Two units, two buyers, and the database never had more work than it could do.
Result: Nobody waits on a lock, nothing is oversold, abandoned units come back.
step 1 of 8
One buyer presses Buy, and the app sends a single checkout request.
- request
- success
- held / reserved
At scale
One buyer never shows this failure. Turn up the crowd and compare: with no queue the database tips over; with a line at the edge it keeps selling. All numbers are illustrative.
Numbers are illustrative: a small simulation, not a measurement.
The happy path, step by step
- The buyer presses Buy and the app sends one checkout request.
- Checkout asks inventory to reserve one unit before any money moves.
- Inventory sets the unit aside with a hold that expires, for example after ten minutes.
- Checkout asks the payment provider to charge the card.
- The payment provider approves the charge.
- Checkout writes the order once, after the payment has succeeded.
- Checkout claims the held unit, which removes it from stock for good and ends the hold.
- The buyer sees a confirmation.
The failure
What breaks and why
When a drop opens, every buyer wants the same few units in the same second. If stock is one row with a count, every checkout has to read and change that one row, so writers queue on its lock, and a check made a moment ago is already out of date: two buyers can both see “1 left”. Lock waits turn into timeouts, buyers refresh and send the same writes again, and the database that also holds carts and orders slows down for everything that shares it. To shoppers the site has crashed, and what does get through may be oversold.
Before / after
The fix
Three changes, each aimed at one part of the jam. At the edge, admit buyers to checkout at a rate the database can sustain and keep the rest in a first-come line with a waiting page. In inventory, keep one row per sellable unit and claim a free one with SELECT … FOR UPDATE SKIP LOCKED: buyers lock different rows, never wait on each other, and no unit can be claimed twice. Give every claim an expiry, so an abandoned checkout hands its unit back after a few minutes.
-- one row per product, shared by every buyer
SELECT qty FROM stock WHERE sku = 'TEE-01';
-- app: if qty > 0, carry on (already stale)
UPDATE stock
SET qty = qty - 1
WHERE sku = 'TEE-01';
-- every checkout queues on this one row lock-- a hold is a unit row with an expiry
START TRANSACTION;
SELECT id FROM stock_units
WHERE sku = 'TEE-01'
AND (held_until IS NULL OR held_until < NOW())
LIMIT 1
FOR UPDATE SKIP LOCKED; -- taken rows are skipped
UPDATE stock_units
SET held_by = :checkout,
held_until = NOW() + INTERVAL '10' MINUTE
WHERE id = :id;
COMMIT;Boss fight
Diagnose the real outage
The real outage
A celebrity make-up sale sold out in minutes. It also took down every other shop on the same database. The next sale was a week away. What would you ship first?
Fight the boss90 seconds · the company is revealed after you answer
From their own write-ups
How real companies approach it
Shopify describes a throttle in its Nginx and Lua load balancers: a leaky bucket lets a set number of requests into checkout per 5-second window and sends everyone else to a cached queue page that keeps polling, since a plain 429 would look like a crash to shoppers.
Shopify Engineering: Surviving flashes of high-write traffic using scriptable load balancers, part I ↗Shopify says random polling made that queue a lottery, with waits of up to 40 minutes. Its fix keeps no shared state: each buyer's first attempt is timestamped in a signed cookie, and every load balancer moves a “now serving” cut-off with a proportional feedback controller.
Shopify Engineering: Surviving flashes of high-write traffic using scriptable load balancers, part II ↗Shopify describes moving inventory reservations from Redis to MySQL: one row per sellable unit, claimed with SKIP LOCKED from a pool capped at 1,000 rows per item and location, in the same database as the inventory ledger so that reserving and claiming are one transaction.
Shopify Engineering: We replaced Redis with MySQL for inventory reservations, and it scaled (2026) ↗
How this comes up in interviews
"Design checkout for a limited drop where every buyer arrives in the same second and stock must never be oversold."
Then they ask
- Stock is one row with a count. What happens to the database when the crowd arrives?
- A buyer abandons checkout after a unit was held for them. How does that unit get back on sale?
- Why show buyers over the limit a queue page at the edge, and not an error?
A strong answer covers
- Reserve a unit with a hold that expires before any money moves, and claim it after the payment has succeeded.
- One row with a count makes every checkout wait on one lock, and a check made a moment ago is already out of date.
- Keep one row per sellable unit and claim a free one with SKIP LOCKED: buyers lock different rows and no unit is claimed twice.
- At the edge, admit buyers at a rate the database can sustain and keep the rest in a first-come line.
- Every claim has an expiry, so an abandoned checkout hands its unit back.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
Your turn
What would you do?
A sneaker drop opens at 10:00. Within a minute checkout takes 30 seconds, the stock database shows hundreds of transactions waiting on one row, and other shops on that database slow down. Which change removes the cause?
Related
Flash sale checkout questions
- What is a reservation hold in checkout?
- A short claim on one unit of stock, taken before payment starts. It stops anyone else buying that unit while the buyer pays. A successful payment turns the hold into a sale; if the buyer leaves, the hold expires and the unit goes back on sale.
- Why use SELECT … FOR UPDATE SKIP LOCKED for inventory?
- It locks rows that are free and passes over rows another transaction has locked, instead of waiting for them. With one row per unit, many buyers can each claim a different unit at the same moment, and the database guarantees no row is handed out twice.
- Why show a queue page instead of returning 429 during a flash sale?
- Both limit how many buyers reach checkout. A 429 looks like a broken site to a shopper, who then refreshes and adds load. A queue page says the sale is working, keeps the buyer's place in arrival order, and lets them in when there is room.
Sources: Shopify Engineering: Surviving flashes of high-write traffic using scriptable load balancers, part I ↗ · Shopify Engineering: Surviving flashes of high-write traffic using scriptable load balancers, part II ↗ · Shopify Engineering: We replaced Redis with MySQL for inventory reservations, and it scaled (2026) ↗ · PostgreSQL documentation: SELECT, the locking clause (SKIP LOCKED) ↗ · Report a problem
A typical design, informed by public write-ups, not any one company's exact system. Diagrams are our own. Numbers in the simulation are illustrative. All wording is original and paraphrases the linked sources.