Skip to content

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
Asked at
  • Disney
Practice this question

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

AskWhy it mattersA 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.

The requirements menu

RequirementScopeWhy

View the sale item

Core

The page tens of millions load at once. It has to be served from a cache or the edge, and it should show whether units remain without asking the backend.

Secure one unit for a limited time while the sale runs

Core

The heart of the design: an atomic take on the stock that becomes a hold with an expiry, not yet an order.

Pay for a secured unit to complete the purchase

Core

Payment runs through a third party and takes seconds, which is why the hold exists. Confirming the order must check that the hold is still live.

Limit each account to one unit

Optional

Worth taking: it costs one check in the reservation step and removes the easiest way for a script to take many pairs. Leave it if the interviewer says resellers are not a concern.

Show buyers their place in line and an estimated wait

Optional

Cheap once a waiting room exists, since the room already orders people. It keeps buyers from refreshing, which is itself load. Skip it if time is short; it changes nothing underneath.

Let a buyer give up a hold before it expires

Optional

A small addition to the release path that returns units sooner. Take it if you have built the release path anyway; otherwise the expiry covers it.

Choose buyers by random draw instead of arrival order

Optional

Tempting as the fairest answer to bots. Leave it unless asked: it changes the product (no instant purchase) more than the system.

Several sizes of the same shoe, each with its own stock

Optional

If the interviewer wants sizes, it turns one hot counter into several smaller ones, which helps rather than hurts, so mention it and keep the single-counter story for the board.

Restocking units in the middle of a sale

Out of scope

An admin raising the count mid-sale is a write to the same counter and an edge flag to flip back. Name it and park it.

Browsing or searching the wider catalog

Out of scope

Ordinary storefront traffic with none of the contention. Keep the sale page separate so it can be cached on its own.

Shipping, returns, refunds and order history

Out of scope

Downstream of a confirmed order. The one exception worth a sentence: a refund when a payment lands after its hold expired, which the release deep dive covers.

Admins creating and configuring sales

Out of scope

A form that writes a sale row and seeds a stock counter before noon. Nothing about it is hard; say so and move on.

Non-functional requirements

RequirementWhat 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

QuantityWorkingResultWhat 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

EntityWhat 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

EndpointNotes

GET /sales/{id} → item, price, whether units remain

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.

POST /sales/{id}/reservations → { reservation_id, expires_at }

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 409; no token answers 403. In this design a retry by the same user for the same sale finds the existing hold through the unique (sale_id, user_id) pair and returns it instead of taking a second unit.

POST /orders { reservation_id, payment } → { order_id }

Checks the hold is still held and belongs to the caller, charges through the payment provider with reservation_id as the idempotency key, then marks the hold paid and writes the order in one transaction. A hold that has lapsed answers 410 Gone before any charge is attempted.

The high-level design

payholdchargereserveconfirmBuyerReservationserviceOrder servicePostgresPayments

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.

tapchallengepassedtokenpayLuaholdscheduledueunpaidrestorechargewebhookconfirmzeroBuyerCDN and edgeBot checkWaiting roomReservationserviceOrder serviceRedis stockPostgresDelay queueRelease workerPaymentprovider
The whole design

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: Hot keys and hot partitionsA concept that helps here: Optimistic and pessimistic locking

The 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: Backpressure and load sheddingA concept that helps here: Rate limiting algorithms

Releasing 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?

A concept that helps here: Job scheduling and delayed workA concept that helps here: Idempotency keysA concept that helps here: Distributed locks and leases

Concepts this guide uses

Sources

  1. Redis documentation: Scripting with Lua
  2. Redis documentation: EXPIRE
  3. Cloudflare Waiting Room docs: About
  4. Cloudflare Waiting Room docs: Queue ordering methods (FIFO and random)
  5. Cloudflare Turnstile docs: Overview
  6. Shopify Engineering: We replaced Redis with MySQL for inventory reservations, and it scaled
  7. Nike Help: SNKRS Draw rules
  8. Stripe API reference: Idempotent requests
Sign in to report a mistake