Quick revision
The short answer
Static content comes from the CDN and the server only reveals a sale token when the sale starts, so the origin sees purchase attempts and little else. Attempts go to POST /sales/{id}/attempts with an idempotency key, an in-memory Redis stock counter that cannot go below zero rejects almost everyone in microseconds, winners are put on a queue, and order workers create orders at a rate the database can sustain while clients poll GET /attempts/{id} for the outcome.
What to remember
In this answer, we designed a flash sale by shaping traffic layer by layer. The CDN serves everything static and a sale token appears only at the start, the attempt API answers queued or rejected with an idempotency key, a Redis stock gate rejects the 99 percent who cannot win in microseconds, a queue feeds winners to order workers at a rate the database sustains, and a unique key on sale and user keeps one unit per person in the record of truth.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Static content comes from the CDN and the server only reveals a sale token when the sale starts, so the origin sees purchase attempts and little else. Attempts go to POST /sales/{id}/attempts with an idempotency key, an in-memory Redis stock counter that cannot go below zero rejects almost everyone in microseconds, winners are put on a queue, and order workers create orders at a rate the database can sustain while clients poll GET /attempts/{id} for the outcome.
Why this question is asked
A flash sale turns normal traffic upside down: almost nothing for hours, then hundreds of times the usual load in a few seconds, with 200 visitors for every unit on offer. A design that sends each of those visitors to the order database will fall over in the first second. Interviewers ask this because it tests whether we can shape traffic so that each layer only does work it can afford. In this answer, we will estimate the load, move everything static to the CDN, design the API, build the stock gate in Redis, add a queue in front of order creation, and finish with the data model.
1Requirements and Rough Numbers
We sell 10,000 units to about 2 million visitors. The sale must stay up, must never sell more than 10,000 units, and must tell each person quickly whether they won. Let us estimate the peak. If half the visitors press Buy within the first 10 seconds, that is about 100,000 attempts per second. Product pages, images and countdown refreshes before the start can be several times more requests than that.
Now compare with what an order database can do. A well-tuned PostgreSQL primary might create a few thousand orders per second with all their rows and indexes. So the design must answer 99.5 percent of attempts, the losers, without touching the database at all, and feed the 10,000 winners to it at a pace it can handle.
2Static First: Keep the Origin Quiet
The sale page, images and countdown are the same for everyone, so they are served from the CDN, a network of caches close to users, and the origin servers never see those requests. The countdown runs in the browser against a clock time fetched from the CDN, not by polling our servers.
But there is a catch: if the Buy button simply calls an API, scripts will call it before the sale starts. So the server reveals a sale token only once the sale begins, through a small, heavily cached endpoint that changes at the start time. An attempt without a valid token is rejected at the edge. The origin therefore sees purchase attempts, and very little else.
3Designing the API
The purchase is asynchronous, because the decision and the order happen at different speeds. The attempt returns quickly with queued or rejected, and the client asks for the final result separately or receives a push.
| Call | Body or result | Purpose |
|---|---|---|
| GET /sales/{id} | item, start time, sale token once started | cached page data |
| POST /sales/{id}/attempts | user, quantity, Idempotency-Key -> queued or rejected | try to win a unit |
| GET /attempts/{id} | QUEUED, WON, LOST | poll for the outcome |
| POST /orders/{id}/checkout | payment -> confirmed order | winner pays within 10 minutes |
The idempotency key means that a double click or a retry after a timeout returns the same attempt instead of creating a second one. Winners then finish a normal checkout within a time limit, and units from winners who do not pay go back to stock.
4The Stock Gate in Redis
The heart of the design is a counter of remaining stock held in Redis, an in-memory data store that executes each command one at a time. A single atomic operation either takes a unit or reports that none are left. Because Redis runs a Lua script without interleaving any other command, the check and the decrement cannot be split by another request.
A single Redis node handles well over 100,000 such scripts per second, and each answers in microseconds. So of the 1 million attempts, the first 10,000 get 1, and the other 990,000 get 0 and an immediate 'sold out' without any database work. The y10 version of this question extends the script to enforce one unit per user and to return unpaid units.
5A Queue Between Winners and the Database
Winners are not written to the order database directly. Each winning attempt is appended to a queue, for example a Kafka topic or a Redis stream, and a pool of order workers consumes it at a fixed rate, say 2,000 orders per second. All 10,000 orders are created within about 5 seconds, and the database never sees more than it can sustain.
8 messages in order. Play them, or step through them one at a time.
Losers stop at the Redis gate; only winners reach the queue and the database.
6The Data Model
Three tables are enough. The sale describes what is on offer, sale_attempts records each person's outcome, and orders are created asynchronously by the workers.
Notice the unique key on sale and user. Redis decides quickly, but the database is the record, so even if some bug let a person win twice, the second insert would fail and the order worker would return the unit. This is the general pattern of the whole design: fast, approximate decisions at the front, and strict guarantees at the back.
7Follow-up questions
Why not decrement stock with a database UPDATE instead of Redis?
An UPDATE ... SET stock = stock - 1 WHERE stock > 0 is correct, but every attempt would wait for the lock on the same single row. A row like that handles perhaps a few thousand updates per second, far below 100,000 attempts per second, and the waiting connections would exhaust the pool. Redis answers in microseconds without row locks.
What if Redis fails during the sale?
Redis replication is asynchronous, so a failover can lose recent decrements and oversell. The safer pattern is to give Redis stock in batches from a durable source and recompute remaining stock from durable winners before reopening after a failover, with the database constraint as the last guard.
How do clients learn the outcome without hammering GET /attempts?
The response includes a suggested retry interval, for example two seconds, and clients that support it receive a push through a WebSocket or server-sent events instead. Since losers are answered immediately, only the 10,000 winners ever poll.
8Conclusion
In this answer, we designed a flash sale by shaping traffic layer by layer. The CDN serves everything static and a sale token appears only at the start, the attempt API answers queued or rejected with an idempotency key, a Redis stock gate rejects the 99 percent who cannot win in microseconds, a queue feeds winners to order workers at a rate the database sustains, and a unique key on sale and user keeps one unit per person in the record of truth.
Open with
With 10,000 units and 2 million visitors, I expect around 100,000 purchase attempts per second at the start, while the order database can create maybe a couple of thousand orders per second, so the design has to reject almost everyone without touching the database.
Then, if they want more
First, everything static comes from the CDN: the page, images and the countdown, and the server only reveals a sale token when the sale starts, so scripts cannot call the API early and the origin mostly sees purchase attempts.
The API is POST /sales/{id}/attempts with the user, quantity and an idempotency key, which returns queued or rejected immediately, and GET /attempts/{id} to get the result, or a push. The stock gate is a counter in Redis decremented by a small Lua script that refuses to go below zero, so 990,000 losers get sold out in microseconds.
Winning attempts go onto a queue such as Kafka or a Redis stream, and order workers create orders at a fixed rate the database can sustain. Winners then check out normally within a time limit, and unpaid units go back.
For the data model I have sale, sale_attempts with a unique key on sale and user so one person gets one unit, and orders created asynchronously. Redis is fast, but the database is still the record of truth.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design the request path, API and data model of a flash sale for 10,000 units with 2...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/ff67b393e4/