Prevent Overselling Inventory Under Concurrent Checkout Requests
Last updated: November 29, 2026
Quick Overview
Many checkout requests for a limited item hit the inventory service at the same time. Show why a read-then-write decrement oversells, and implement both an optimistic compare-and-set approach and a pessimistic locking approach that never sell more than the stock.
Walmart Labs
November 29, 20260
0
0 solved
Many checkout requests for a limited item hit the inventory service at the same time. Show why a read-then-write decrement oversells, and implement both an optimistic compare-and-set approach and a pessimistic locking approach that never sell more than the stock.
Console and holiday doorbuster drops on Walmart.com create intense contention on a handful of SKUs. The Walmart Labs onsite interviewer checks whether you can name the check-then-act race, fix it two ways, and reason about contention, retries and where the atomicity actually lives in a distributed system.
What the Interviewer Expects
- Identify the check-then-act race and show a failing interleaving
- Implement optimistic concurrency with a version and retries
- Implement pessimistic locking with a narrow critical section
- Compare throughput and failure behavior under high contention
- Map the in-process solution to a database or cache primitive
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 do this across 50 app servers sharing a database?
- How do you keep a hot SKU from overwhelming a single database row?
- What does a client see when retries are exhausted, and how should the app retry?
- How would you combine this with cart reservations that expire?
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
try_purchase(sku, qty) returns True and decrements stock only if enough units remain, or returns False. Under any interleaving of concurrent calls, the sum of successful quantities must never exceed...
High-Level Design
Option 1, optimistic: store (qty, version). Read both, check stock, then write only if the version is unchanged, using compare-and-set. On conflict, reread and retry. This is SQL `UPDATE ... SET qty...