Flash Sale System
High-Concurrency Limited Inventory
Design a sale where a few thousand discounted units go on sale at one moment to millions of buyers: sell exactly the stock, one per buyer, fairly, without the rest of the store falling over.
Overview
In a flash sale a store puts a small number of units (10,000 game consoles at a discount, say) on sale at an announced minute. Millions of people are waiting, and almost all of them will leave with nothing. The system has to sell exactly the stock, never more and never less, give each buyer at most one, and survive a spike that is hundreds of times its normal traffic.
The useful way to see it: this is mostly a rejection problem. Out of 2 million people, 10,000 succeed, half a percent. The design is judged by how early and how cheaply it says no to the rest, and whether the few decisions that matter (who gets a unit) are correct and fair.
Questions to ask first
- How much stock, and how many people? — 10,000 units; about 2 million people waiting
- How fast do people arrive? — Nearly all click within the first 10 seconds
- How many per buyer? — One
- How long to pay? — 5 minutes of hold; unpaid holds return to stock
- Must it be first come, first served? — Fair: people who were waiting at the start get a random place; later arrivals queue in order
- Does the rest of the store keep running? — Yes; the sale must not take it down
What you will be able to do
- Size the spike and show where each layer cuts it down.
- Design a waiting room that admits buyers at a rate the back end can take.
- Decrement stock atomically, one per buyer, with a Redis script or a database unit pool.
- Hold units while buyers pay and put unpaid ones back.
- Explain what stays correct when a cache node or the queue fails.
Key concepts
- Waiting room
- Admission rate
- Atomic reserve
- Redis Lua script
- Unit pool with SKIP LOCKED
- Hold and expiry
- One per buyer
- Idempotency key
- Bot defense
Functional requirements
- Schedule a sale with item, price, quantity, per-buyer limit, start and end
- Let buyers wait before the start and get a fair place in line
- Reserve one unit per buyer and hold it for 5 minutes
- Turn a paid hold into an order; return unpaid holds to stock
- Show each buyer their state: waiting, admitted, reserved, sold out
Non-functional requirements
- Never sell more units than the stock, and never leave stock unsold because of a bug
- Each buyer gets at most one unit, even with several tabs, retries or accounts on one card
- A buyer gets a clear answer (reserved or sold out) within a second of being admitted (a target we chose)
- The sale traffic never takes down the regular store
- Keep automated buyers from taking most of the stock
Interview questions
- How do you make sure you never oversell?
- Why not just UPDATE a stock row in the database?
- How does the waiting room work?
- How do you stop bots?
- What happens to units that are reserved but never paid?
- How do you keep the rest of the store up during the sale?
- Is strict first come, first served fair?
- How would you run ten sales at the same time?
Sign up free to read the full topic
More System Design topics
Start free on Cariara · Coding problems · System design studio