Design E-commerce Inventory
System Design Cases: lesson 13 of 20
One unit left, two carts, and only one of them may hear yes.
Lesson 13 of 20 · 7 min
Design E-commerce Inventory
Step 1 of 11
One unit left on the hottest item, and two carts reaching for it in the same second.
The Idea
Sell the last unit once. Assume a launch where thousands of carts a second reach for the same item, and the hard part is not storing the count but deciding who is allowed to hear yes.
Real-World Example
A theatre seat held while you pay. For ten minutes it is neither sold nor available, and it goes back on the map the moment you walk away from the counter.
reserve(sku, n):
UPDATE stock SET available = available - n
WHERE sku = ? AND available >= n -- 0 rows updated = sold out
INSERT hold(sku, n, expires_at = now() + 10m)
confirm(hold): delete the hold, write the order
expire(hold): available = available + n
The Tradeoff
A conditional decrement makes overselling impossible and turns the hottest item into a single row everyone queues behind. Holding stock with an expiry keeps carts honest, and guarantees some inventory sits unsellable while those timers run down.
Your turn
Put the steps in the right order.
- Confirm the hold on payment and write the order
- Decrement stock only while the count is still above zero
- Release anything still held when its timer expires
- Record a hold with a ten-minute expiry
Mini quiz
1 / 3