Full answer
Requirements
Functional:
- At the start time, users press "Buy"; the first 100,000 valid buyers get a phone, one per user.
- Winners complete payment within a time limit; unpaid units go back to the pool.
- Everyone else sees a clear sold-out or waiting message.
Non-functional:
- No overselling, no duplicate purchases per user.
- The rest of the site keeps working during the sale.
- Fair: bots should not take most units.
Capacity estimates
Assumptions: 10M users arrive within 10 seconds, 100,000 units, the order database sustains 5,000 order writes per second.
- Incoming: 10M / 10 = 1M requests per second at the peak.
- Successful buyers: 100,000, only 1% of requests.
- Order creation: 100,000 / 5,000 = 20 seconds to write every order if they are queued and processed steadily.
The design must turn a 1M requests per second spike into a 5,000 writes per second stream.
API
GET /sale/phone-x (static page from CDN with countdown)
POST /v1/sales/phone-x/enter -> {"queue_token": "...", "position": 120345} (waiting room)
POST /v1/sales/phone-x/claim (with admission token) -> 200 {"purchase_token", "expires_in": 600} or 410 {"status": "SOLD_OUT"}
POST /v1/orders {"purchase_token", "address_id", "payment_method_id"}
Data model
sale:{id}:stock:{bucket}in Redis, 100,000 units split into, for example, 20 buckets of 5,000.sale:{id}:buyersset of user IDs that already claimed (one per user).purchase_tokens: token, user_id, sale_id, expires_at, status.- Orders in the normal orders database, written by queue consumers.
High-level design
flowchart LR
U[Users] --> CDN[CDN: sale page]
U --> WR[Waiting room]
WR -- "admitted at fixed rate" --> CL[Claim service]
CL --> R[(Redis stock buckets and buyer set)]
CL --> Q[(Order queue)]
Q --> OW[Order workers]
OW --> DB[(Orders DB)]
OW --> PAY[Payment]
EXP[Expiry worker] --> RThe claim step
A Lua script in Redis runs atomically:
- If the user is in the buyer set, return "already claimed".
- Pick a random stock bucket; if its count is above 0, decrement it, add the user to the buyer set and return success; otherwise try another bucket, and if all are empty return sold out.
Success returns a signed purchase token and puts an order request on the queue. Because each script is atomic, stock never goes below zero. Splitting stock into buckets spreads the load over several Redis shards so one key is not a bottleneck.
When stock reaches zero, a "sold out" flag is published to the CDN and waiting room, so new users get the answer without touching the claim service.
Bottlenecks and trade-offs
- Protecting the rest of the site. Run the sale on separate capacity and use rate limits so the flood cannot exhaust shared services.
- Fairness and bots. Require sign-in before the sale, one claim per verified account, CAPTCHA or proof of work in the waiting room, and randomised admission among users who arrived before the start.
- Unpaid claims. Purchase tokens expire after 10 minutes; the expiry worker returns units to stock and can admit more users.
- Redis failure during the sale: use a replicated setup and accept that a failover could briefly reject claims; reconcile Redis stock with orders after the sale.
- Strict first-come-first-served is impossible at network scale; a waiting room with random order among early arrivals is fairer than a race of milliseconds.
By level
Fresher: Explain that the sale page is cached, users wait in a queue, and stock is reduced in one safe step. A fresher should say why the database cannot take every click directly.
Mid: Do the spike math, design the waiting room, the atomic claim and the order queue, and handle unpaid claims. Explain stock buckets.
Senior: Cover fairness, bots, isolation from the main site, Redis failover and reconciliation, and the limits of first-come-first-served. Expect a clear plan for failing fast.
Key points
- Turn a 1M requests per second spike into a steady stream the database can handle
- Static page on CDN plus a waiting room
- Atomic stock decrement in Redis split into buckets
- One claim per user enforced in the same atomic step
- Async order creation from a queue; expiring purchase tokens
Common mistakes
- Sending every buy request to the relational database
- One Redis key for all stock, creating a hot key
- Checking "already bought" and decrementing in separate steps
- Letting the sale traffic take down the rest of the site
Follow-ups
- How would you stop bots from winning most units?
- How do you return unpaid units to other users?
- How would you run several flash sales at once?
Tips
- Lead with the numbers: 1M per second versus 5,000 writes per second
- Explain why most requests should fail fast
- Show the atomic script steps
Evaluation rubric
- Correct spike math and overload protection
- Atomic, oversell-free stock handling
- Async order pipeline with expiry
- Fairness and isolation trade-offs