Skip to content
BytePatterns

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.

  1. Confirm the hold on payment and write the order
  2. Decrement stock only while the count is still above zero
  3. Release anything still held when its timer expires
  4. Record a hold with a ten-minute expiry

Mini quiz

1 / 3

Overselling is prevented by:

New lessons land every few weeks

Leave an address and we will tell you when the next one is up. That is the only reason we will use it.

One address, stored so we can email you. Nothing else, ever.