Updated 24 October 2026
Inventory, reservations and flash sales
Never overselling: atomic decrements, reservations with expiry, holds versus commits, sharded inventory counters, queues and virtual waiting rooms for flash sales, bots, and keeping cached stock counts honest.
Try it yourself, in a playground:Reservation holds
Limited stock appears everywhere: seats at a concert, rooms on a night, sneakers in a drop, the last item in a warehouse. The requirement is always the same (never sell more than you have) and the hard case is always the same: a crowd arriving at the same instant for the same few items. This guide covers the patterns for both the everyday case and the flash sale.
The race
Two buyers see "1 left", both click buy, both reads return 1, both write 0. Any design that reads stock, decides in application code, then writes will oversell under concurrency. The check and the decrement must be one atomic operation.
Atomic decrements
For countable stock (100 identical items), let the database do the check and the decrement together:
UPDATE inventory SET available = available − 1
WHERE sku = 'A1' AND available >= 1If zero rows changed, it is sold out. In Redis, a Lua script or DECR with a check does the same in memory at very high rates. For distinct items (seat 14C, room for one night), use a row per item and a conditional update or a unique constraint. See transactions, isolation levels and locking.
Reserve, then commit
Buying takes time: the user enters details, payment is authorised. Do not decrement only at the end (someone else may take the item while they type), and do not decrement permanently at the start (abandoned carts would lock stock forever). Use a reservation with an expiry:
- On "add to cart" or "select seat": move one unit from available to held, with an expiry (5 to 15 minutes) and a reservation id.
- On successful payment: convert the hold to sold.
- On expiry or abandonment: return the unit to available automatically.
The expiry is enforced by data (an expires_at checked when stock is counted) and by a sweeper that releases expired holds, so a crash never leaves stock stuck. Payment is authorised while the hold exists and captured on commit. See money in system design and Design Ticketmaster.
Hot items: when one row is the bottleneck
A popular SKU means every buyer updates the same row; a database row handles a few hundred to a few thousand contended updates a second. Options:
- Shard the counter: split 10,000 units into 20 sub-counters of 500; each buyer tries a random sub-counter and moves on if it is empty. Sold-out is when all are empty.
- Hold stock in Redis for the sale, with atomic scripts, and write sold units to the database asynchronously (with reconciliation, because Redis can lose acknowledged writes on failover).
- Pre-allocate: hand out tokens (one per unit) from a queue; whoever gets a token gets the item.
Flash sales: control the crowd first
When a million people arrive for 10,000 items, the inventory logic is not the main problem; the crowd is.
- Virtual waiting room: put arrivals in a queue at the edge, admit them at a rate the backend can handle, and show their position. Randomising order among people who arrived before the start is fairer than rewarding the fastest bot.
- Pre-scale for the known start time; autoscaling reacts too slowly. See autoscaling.
- Serve static content from the CDN, so only purchase attempts reach the origin.
- Rate limit per user, device and payment method.
- Stop early: once stock is gone, answer "sold out" at the edge instead of letting requests reach the database.
Bots and fairness
Drops attract bots that buy faster than humans and resell. Defences: queue-based admission with randomisation, purchase limits per account and per payment card, device and behaviour signals, verified-fan pre-registration, and making the reward for speed small (a lottery for the right to buy). See rate limiting and resilience.
Showing stock without lying
Product pages show "in stock" or "3 left" millions of times; that must not read the hot inventory row. Cache availability with a short TTL and treat it as approximate; the purchase step is the authority. It is fine to show "in stock" and then fail at checkout occasionally; it is not fine to oversell. Flip pages to "sold out" quickly through cache invalidation when stock reaches zero. See caching.
Overselling on purpose
Airlines and hotels oversell deliberately because some customers do not show up. If the business wants it, make it an explicit, configured allowance with a policy for the overflow, never an accident of concurrency.
Reconciliation
Count sold, held and available units regularly and compare with orders and payments. Holds that outlived their expiry, sold units without paid orders, and paid orders without stock all indicate bugs. See Design Airbnb for the per-night version of the same checks.
Checklist
- Atomic check-and-decrement or a unique constraint; never read-then-write.
- Holds with expiry, converted on payment, released automatically.
- Sharded counters or in-memory stock for hot items.
- Waiting room, pre-scaling and edge rate limits for flash sales.
- Bot and fairness controls.
- Cached, approximate stock for display; the purchase step decides.
- Reconciliation of holds, sales and payments.
Questions people ask
What is overselling, and how do you prevent it?
Overselling is selling more units than you have, and it happens whenever a design reads stock, decides in application code, then writes: two buyers both see "1 left" and both write 0. The fix is to make the check and the decrement one atomic operation, such as a conditional update that only decrements available when it is at least 1, where zero rows changed means sold out. Distinct items like a seat or a room night use a row per item with a conditional update or a unique constraint.
How do inventory reservations with expiry work?
When a user adds an item to the cart or selects a seat, one unit moves from available to held, with a reservation id and an expiry of 5 to 15 minutes. Successful payment converts the hold to sold, and expiry or abandonment returns the unit to available automatically. This stops someone else taking the item while the user types without letting abandoned carts lock stock forever, and an expires_at check plus a sweeper means a crash never leaves stock stuck.
How do you design a flash sale system?
Control the crowd first: when a million people arrive for 10,000 items, the inventory logic is not the main problem. Put arrivals in a virtual waiting room at the edge and admit them at a rate the backend can handle, pre-scale for the known start time because autoscaling reacts too slowly, serve static content from the CDN, and rate limit per user, device and payment method. Once stock is gone, answer "sold out" at the edge instead of letting requests reach the database.
How do you scale inventory for a hot product?
A database row handles only a few hundred to a few thousand contended updates a second, so a hot SKU needs a different pattern. Shard the counter, for example 10,000 units as 20 sub-counters of 500 with each buyer trying a random one; hold stock in Redis with atomic scripts and write sold units to the database asynchronously, reconciling because Redis can lose acknowledged writes on failover; or pre-allocate one token per unit from a queue.
How do you stop bots in a flash sale?
Bots buy faster than humans and resell, so the defence is to make speed matter less. Use queue-based admission with randomisation, purchase limits per account and per payment card, device and behaviour signals, and verified-fan pre-registration. A lottery for the right to buy keeps the reward for speed small, and randomising order among people who arrived before the start is fairer than rewarding the fastest bot.
How do you show stock levels without hitting the inventory database?
Cache availability with a short TTL and treat it as approximate, because product pages show "in stock" or "3 left" millions of times and must not read the hot inventory row. The purchase step is the authority: it is fine to show "in stock" and occasionally fail at checkout, but it is not fine to oversell. When stock reaches zero, flip pages to "sold out" quickly through cache invalidation.