An e-commerce site will sell 5,000 units of a phone in a flash sale with 2 million users waiting. How do you prevent overselling and keep the site up?

Short answer

Protect the system with a virtual waiting room and edge caching, keep stock correct with atomic conditional decrements (or pre-split Redis counters reconciled to the database), give reservations a short TTL, and process orders asynchronously behind a queue with idempotency.

Detailed answer

  1. Admission control: a virtual waiting room at the edge (CDN-based queueing) admits users at a rate the backend can handle; bot protection and per-account purchase limits.
  2. Static content at the edge: product page cached; stock badge updated every few seconds.
  3. Atomic stock reservation: UPDATE stock SET reserved = reserved + 1 WHERE sku = ? AND on_hand - reserved >= 1 or DynamoDB conditional writes. For extreme contention, split 5,000 units into e.g. 50 Redis counters of 100, decrement atomically (DECR with check via Lua), and persist reservations to the database asynchronously; the database remains source of truth for orders.
  4. Reservation TTL (e.g. 10 minutes) released if payment does not complete.
  5. Order intake queue: accept "buy" requests into a queue, process at a controlled rate, return status via polling/push.
  6. Idempotency keys to avoid double orders on retries.
  7. Pre-scaling and load tests at 2–3× expected peak.

Architecture diagram

flowchart LR
  U[2M users] --> WR[Waiting room + bot protection]
  WR -->|admitted rate| API[Checkout API]
  API --> RC[(Redis stock buckets<br/>atomic decrement)]
  API --> Q[[Order queue]]
  Q --> W[Order workers] --> DB[(Orders DB)]
  W --> PAY[Payment]
  W -- timeout/decline --> RC

Real-world example

Ticketing platforms and sneaker drops commonly use queue-based waiting rooms (for example Queue-it, Cloudflare Waiting Room) combined with atomic inventory counters.

Follow-up questions

  • What if Redis fails mid-sale? (Fall back to DB conditional updates at lower rate; Redis with replication; reconcile.)
  • How do you handle payment failures? (Release reservation; next user in queue gets a chance.)

What the interviewer expects

Both protection of the platform and correctness of stock, with clear atomicity mechanisms and failure handling.

Common mistakes to avoid

Read-then-write stock checks (race conditions), distributed locks per request, synchronous calls to slow payment providers inside the inventory transaction.