Prevent Overselling Limited Inventory Under Concurrent Checkouts
Last updated: November 29, 2026
Quick Overview
A store sells a limited-quantity item through DoorDash, and many customers check out at the same moment. Explain the race that causes overselling and implement an atomic, all-or-nothing reservation of every cart line against an inventory table.
DoorDash
November 29, 20260
0
0 solved
A store sells a limited-quantity item through DoorDash, and many customers check out at the same moment. Explain the race that causes overselling and implement an atomic, all-or-nothing reservation of every cart line against an inventory table.
DashMart and convenience partners carry real stock counts, so overselling turns into cancelled orders and refunds. This databases and transactions question checks that you can explain lost updates, choose between conditional updates, row locks and isolation levels, and keep multi-item reservations atomic and deadlock-free.
What the Interviewer Expects
- Describe the lost update when two transactions read qty = 1 then both write 0
- Use a single conditional UPDATE and check the affected row count
- Wrap multi-line carts in one transaction and roll back on any failure
- Lock rows in a consistent order to avoid deadlocks
- Make the reservation idempotent per order
- Compare with SELECT ... FOR UPDATE and SERIALIZABLE isolation
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 reservations expire if the customer abandons checkout?
- How would you handle a hot item with thousands of concurrent buyers?
- What changes if inventory lives in a different service than orders?
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
- Tables:
inventory(item_id, qty >= 0)andreservations(order_id, item_id, qty). reserve(order_id, lines)reserves all lines or none.- Concurrent checkouts must never drive qty below zero.
- ...
High-Level Design
The bug is read-then-write: two requests read qty 1, both decide there is stock, and both write 0, so two units are sold. The fix makes check and decrement one statement, `UPDATE inventory SET qty = q...