Oversell Safe Stock Reservation For A Flash Sale
Last updated: November 29, 2026
Quick Overview
Implement a thread-safe stock holder for a single flash-sale SKU that lets each user reserve at most one unit, holds the unit for a payment window, returns expired holds to stock, and never sells more units than exist.
Pinduoduo
November 29, 20260
0
0 solved
Implement a thread-safe stock holder for a single flash-sale SKU that lets each user reserve at most one unit, holds the unit for a payment window, returns expired holds to stock, and never sells more units than exist.
Pinduoduo runs limited-quantity flash sales where tens of thousands of requests hit the same SKU in the first second, so overselling or double-buying is a real business risk. The interviewer checks that you understand races in check-then-act code, keep critical sections small, and can translate the single-process design into Redis or database primitives.
What the Interviewer Expects
- Identifies the check-then-decrement race and protects it with one lock
- Enforces one unit per user inside the same critical section
- Reclaims expired holds so abandoned carts return stock
- Makes the clock injectable so timeouts are testable
- Explains the distributed version with an atomic Redis script or a conditional UPDATE
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you implement reserve as a single atomic operation in Redis?
- The SQL version uses UPDATE stock SET n = n - 1 WHERE sku = ? AND n > 0. What are its limits under heavy contention?
- How do you make confirm_payment idempotent when the payment callback is retried?
- How would you shard one hot SKU across several counters?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
reserve(user) returns RESERVED, SOLD_OUT or DUPLICATE. A reservation holds one unit for hold_seconds. confirm_payment(user) turns a live hold into a sale and fails if the hold expired. Invariant...
High-Level Design
The bug to avoid is if available > 0: available -= 1 executed by two threads at once. Put the reclaim, the duplicate check, the availability check and the decrement under one lock so they are atomic...