How to design a flash sale
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Estimates
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- Never selling unit 10,001
- The surge and fairness
- Releasing unpaid units
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
- Disney
The problem
A flash sale checkout sells a small fixed stock to a crowd many times its size, in seconds: Nike puts 10,000 pairs on sale at noon and tens of millions of people tap buy. The answer turns on two ideas. First, keep nearly all of that crowd away from the stock: an edge waiting room lets buyers through at a pace the backend handles, after a bot check, and the edge itself answers sold out when the stock is gone. Second, take each unit in one atomic step that produces a timed hold rather than an order, so a unit is taken at most once and goes back on sale if the buyer walks away from the payment page.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
How many units are there, and how many people show up for them? | The gap between the two decides the whole shape: if the crowd dwarfs the stock, most of the design is about turning people away cheaply rather than serving them. | 10,000 pairs, and tens of millions of people trying to buy within the first few seconds after noon. |
Is it first come, first served, or may we choose who gets a unit another way? | Arrival order rewards whoever is fastest, which in practice is scripts. If the interviewer cares about fairness, random admission or a draw becomes a real option rather than a tangent. | Start with arrival order; fairness is a stated goal, so be ready to argue for picking buyers at random among those present at noon. |
One pair per person? And do sizes count as separate stock? | A per-account limit is a cheap, strong bot defense and changes the reservation check. Sizes multiply the stock counters without changing the mechanism. | One pair per account. Treat sizes as separate counters of the same shape; the design is the same for each. |
Does payment go through a third party, and how long may a buyer hold a unit before paying? | A card payment takes seconds and can fail, so a unit has to be held while it runs. The hold length trades stock sitting idle against buyers losing a pair mid-payment. | Yes, an outside payment provider. Pick a hold of a few minutes and defend it. |
How much of the noon traffic do we expect to be bots? | If most of it is automated, bot checks move from a nice-to-have to the first thing the entry point does, and arrival order stops being a fair way to pick buyers. | A lot of it, possibly most. Plan for the entry point to challenge traffic ahead of handing out places in line. |
If a held unit is not paid for, does it go back on sale while the sale is still running? | It decides whether you need a release path at all, and how fast it must run: a unit freed after the crowd has gone home is a lost sale. | Yes. A held but unpaid pair returns to stock and is offered to someone still waiting. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
Never sell more than the 10,000 units in stock | Taking a unit has to be one atomic step on one counter, with the check and the decrement in the same operation, and a successful take becomes a hold the order step verifies before it confirms. Reading the count and then writing it back is ruled out. |
Survive tens of millions of users arriving within seconds | Almost none of those people can buy, so almost none of them may reach the reservation service. The sale page and the sold-out answer are served at the edge, and a waiting room admits buyers at a pace the backend handles. Capacity planning is about the edge, not the database. |
Fair access: bots and fast clicks should not take every unit | A bot check ahead of any place in line, one unit per account, and a way of choosing who gets through that does not reward raw speed, such as random admission from the room. |
A secured unit that is not paid for goes back on sale | Each hold carries an expiry, something fires when it lapses rather than minutes later, and the release runs once even if it is retried, so the counter and the hold records keep agreeing. |
Estimates
| Quantity | Working | Result | What it sizes |
|---|---|---|---|
Share of arrivals who can buy | "Tens of millions" means at least 10 million; take 10 million as the floor. 10,000 units ÷ 10,000,000 people = 0.1% | About 0.1% of arrivals get a pair; about 99.9% get a sold-out answer | How much traffic the edge must turn away without touching the backend: nearly all of it. |
Arrival rate at the edge | Assume the 10 million floor arrives within 10 seconds of noon: 10,000,000 ÷ 10 = 1,000,000 a second | About 1,000,000 requests a second at the entry point, more in the first second | The load the edge and waiting room must take; nothing behind them should see a rate anywhere near it. |
Admission pace against sell-out time | 10,000 units ÷ 1,000 admissions a second = 10 seconds; 10,000 ÷ 100 a second = 100 seconds | At 1,000 admissions a second the stock is held in about 10 seconds; at 100 a second, in under two minutes | The one knob on the waiting room: how many buyers a second the reservation service lets in, and how long the sale takes as a result. |
Write load on the stock counter | Admitted buyers are the ones who touch the counter: about 1,000 a second at the pace above, against about 1,000,000 a second arriving at the edge | The counter sees roughly one write for each thousand arrivals | Why the waiting room, not a faster counter, is what absorbs the surge: the room cuts the counter's load by about a thousand times. |
Units that return from unpaid holds | Assume a hold of 10 minutes and that 10% of holds lapse unpaid: 10% × 10,000 = 1,000 units come back about 10 minutes after they were taken; at 1,000 admissions a second that is about one second of admissions | About 1,000 units in a second wave, 10 minutes after the first | Why the sold-out flag at the edge must be able to flip back, and why the waiting room should keep people in line rather than send them away when the first wave ends. |
The first oversold unit | 10,000 units in stock + 1 = unit 10,001 | Unit 10,001 is the first pair that does not exist | The correctness bar for the whole design: successful takes stop at 10,000, whatever the crowd does. |
Rows the sale produces | 10,000 orders plus lapsed holds, assuming ten holds for each unit in the worst case: about 110,000 rows × 500 bytes ≈ 55 MB | About 55 MB for the whole sale | Why one database with a standby is enough and sharding the orders is overkill. |
Core entities
| Entity | What it holds |
|---|---|
Sale | One drop: which item, when it opens, how many units it started with. |
Inventory | Units left for a sale: a counter in Redis during the sale, with the database as the record it is rebuilt from. |
Reservation | A timed hold on one unit for one buyer, between taking the unit and paying for it. |
Order | A paid reservation: the state in which a unit has truly left the stock. |
Admission token | Proof that a buyer came through the waiting room; the reservation service refuses requests without one. |
The API
| Endpoint | Notes |
|---|---|
| Served from the CDN with a short lifetime; the backend is not in the path. "Whether units remain" is a flag the edge holds, flipped when the counter hits zero and flipped back if released units return, not a live count. |
| Requires a valid admission token and a signed-in user. Runs the atomic take; on success writes the hold and returns its expiry so the client can show a countdown. Out of stock answers |
| Checks the hold is still |
The high-level design
Step 1 of 4
Reserve, then pay, against one stock row
Start with the two calls a buyer makes. A reservation service takes POST /sales/{id}/reservations and lowers the stock count in a row of the main database with a single guarded update (units_left > 0 in the WHERE clause), writing a hold with an expiry when the update touches a row. An order service takes POST /orders, charges the card through the payment provider, and confirms the hold as paid. The payment runs outside any database transaction, because a third-party call that takes seconds must not hold a row lock. This already meets the correctness requirement for a quiet sale: the row lock serializes the takes, and the guard stops the count at zero.
What breaks first
The deep dives
Never selling unit 10,001. Thousands of requests for the last few pairs land in the same millisecond, and each one saw units remaining a moment ago. How does one request take a unit while the rest are refused, where does the count live, and what happens when that store dies in the middle of the sale?
A concept that helps here: Atomic read-modify-write
A concept that helps here: Hot keys and hot partitionsA concept that helps here: Optimistic and pessimistic lockingThe surge and fairness. Tens of millions of people, many of them scripts, tap buy at noon for 10,000 pairs. Which component gives out first if they all get through, and how do you decide who gets in and in what order?
A concept that helps here: Virtual waiting rooms
A concept that helps here: Backpressure and load sheddingA concept that helps here: Rate limiting algorithmsReleasing unpaid units. A buyer secures a pair and then closes the payment page while people still in line are being told it is sold out. What puts that pair back on sale, when, and what happens if the payment arrives after the hold has lapsed?
Concepts this guide uses
Sources
- Redis documentation: Scripting with Lua
- Redis documentation: EXPIRE
- Cloudflare Waiting Room docs: About
- Cloudflare Waiting Room docs: Queue ordering methods (FIFO and random)
- Cloudflare Turnstile docs: Overview
- Shopify Engineering: We replaced Redis with MySQL for inventory reservations, and it scaled
- Nike Help: SNKRS Draw rules
- Stripe API reference: Idempotent requests