KatInterview

Design a Flash Sale System with Virtual Waiting Room

System DesignModel answer
MediumModel answerSystem Designsystem-designdistributed-systemsredis

Last updated

Architect a high-concurrency item drop system using edge-cached waiting queues, zero-oversell inventory reservation, and idempotent payment processing.

Scenario

A high-demand ticketing and merchandise platform frequently releases limited-edition inventory—such as opening-day theme park passes, exclusive concert packages, and drop-style physical collectibles. Millions of shoppers hit the purchase page within seconds of an event going live.

You are tasked with designing an end-to-end flash-sale system capable of absorbing massive initial traffic spikes, metering shopper access through a virtual waiting queue, maintaining strict zero-oversell inventory holds, and providing idempotent checkout processing.


Requirements

Functional Requirements

  1. Virtual Waiting Room: When incoming traffic exceeds backend capacity, divert shoppers into a waiting queue displaying their current position and estimated wait time.
  2. Controlled Admission: Admit queued shoppers into the checkout flow at a rate matched to backend database processing limits using cryptographically signed access tokens.
  3. Exact Inventory Reservation: Grant admitted users a temporary hold on an item (e.g., 10 minutes). Overselling must be impossible.
  4. Hold Expiration & Rollback: Automatically return reserved items to the available inventory pool if a user abandons their session or fails to complete payment before the hold window expires.
  5. Idempotent Checkout: Ensure network retries, browser refreshes, or repeated payment webhooks never result in duplicate charges or duplicate order fulfillment.

Non-Functional Requirements

  1. Strict Inventory Consistency: Zero tolerance for overselling under any node failure, network partition, or concurrent execution state.
  2. High Ingress Availability: The edge queue and position check endpoints must remain fully available (99.99%) during extreme traffic spikes.
  3. Fault Tolerance: Worker crashes or payment provider timeouts must never leak item holds or leave orders stuck in an indeterminate state.
  4. Abuse Mitigation: Protect against queue manipulation, token forgery, replay attacks, and script-based bot hoarding.

System Scale & Constraints

  • Peak Ingress Load: 2,000,000 concurrent users waiting at drop time.
  • Total Catalog Inventory: 25,000 units per flash-sale event.
  • Admission Throughput: 500 to 1,000 users per second admitted to the reservation stage.
  • Hold Duration (TTL): 10 minutes.
  • Latency Targets: Under 200 ms for status checks and reservation attempts.

Key Discussion Areas

  1. High-Level System Architecture: Responsibilities of the CDN/Edge layer, queue service, gatekeeper API, inventory management service, payment flow, and background workers.
  2. Virtual Queue & Access Control: Monotonic counter sequence vs. sorted sets for queue management, cryptographically signed access tokens (JWTs), and API gateway validation.
  3. Inventory Management & Locking: Concurrency control strategies (pre-materialized inventory rows, database locks using SKIP LOCKED, Redis fast-path pre-filters), and deterministic hold expiration.
  4. Idempotent Payment & Order Completion: End-to-end checkout protocol with idempotency keys, handling asynchronous PSP webhooks, and ensuring transactional durability via the Transactional Outbox pattern.

Hints

Hint 1

Isolate high-volume read traffic (queue polling) from transactional write traffic by using edge caching for global queue progress counters.

Hint 2

Pre-create individual inventory rows in a relational database and select them with SELECT ... FOR UPDATE SKIP LOCKED to prevent row-lock contention across concurrent connections.

Hint 3

Treat Redis as an optimistic pre-filter to reject requests fast when stock is zero, but use relational database primary keys and unique indexes as the single source of truth for exact accounting.