
Two checkout requests read stock = 1 and both return “reserved.” The interviewer wants the invariant and the exact write that protects it, not a vague “add a lock.” This scenario connects race conditions, database locking, and large-scale inventory design.
The Interview Question
“Two customers buy the last item concurrently. How do you guarantee that at most one reservation succeeds while keeping checkout responsive?”
Clarifying Questions
Ask whether stock is global or per warehouse, whether reservations expire, whether overselling is acceptable, and how many writes a hot SKU receives. Assume one authoritative inventory row, a five-minute reservation, and 5,000 requests per second for a flash sale.
The Naive Design and Why It Fails
SELECT stock, check stock > 0, then UPDATE stock leaves a race between statements. A Redis counter can diverge from the database during failover. A long transaction around payment holds locks and turns a hot SKU into a bottleneck.
Better Design
Use an atomic conditional update and create a reservation record in a short transaction. The update succeeds only when available stock remains; its affected-row count is the decision.
flowchart TD
A[Checkout request] --> B[Idempotency key]
B --> C[Atomic stock decrement]
C -->|one row changed| D[Create expiring reservation]
C -->|zero rows changed| E[Out of stock]
D --> F[Async payment and order flow]
F -. expiry .-> G[Compensating restock]
Data Model and Race Safety
Use inventory(sku, available, version) and reservations(id, sku, order_id, expires_at, status) with unique (sku, order_id). UPDATE ... WHERE sku = ? AND available > 0 serializes the invariant. A sweeper releases expired reservations with a compare-and-set on status so it cannot restock a completed order.
The critical write should make the predicate and the decrement one database operation:
UPDATE inventory
SET available = available - 1,
version = version + 1
WHERE sku = :sku
AND available >= 1;
If the affected-row count is zero, the caller must not create a reservation. If it is one, insert the hold in the same transaction and publish an outbox event after commit. A unique key on (order_id, sku) makes a retry of the same checkout a no-op rather than a second decrement. The database can use a row lock, optimistic version check, or another serializable mechanism; the important property is that the availability predicate cannot be separated from the write.
Reservation Lifecycle
Treat a hold as a state machine: HELD -> COMMITTED, HELD -> EXPIRED, or HELD -> RELEASED. Payment and expiry race through compare-and-set transitions. A payment worker may commit only while the row is HELD and unexpired; the reaper may release only while the same conditions hold. Whichever transition wins owns the stock, and a later retry observes the resulting state instead of applying a second action.
The five-minute expiry is a business policy, not a license to trust application clocks. Store the deadline from one authoritative clock, make the sweeper idempotent, and monitor expiry backlog. If the sweeper is down, the system should stop promising new reservations once the uncertainty exceeds the safety window rather than continuing to oversell.
Failure Handling
If the client loses the response, the idempotency key returns the original reservation. If payment fails, a saga releases the reservation. If the database is unavailable, fail closed for scarce stock rather than trusting a stale cache. Emit reservation and release events through an outbox.
Scaling and Trade-offs
A single hot row cannot process 5,000 serialized writes indefinitely. Pre-allocate stock tokens, shard inventory by warehouse or token bucket, and queue excess demand. A queue smooths bursts but makes reservation asynchronous; state that trade-off explicitly.
For a flash sale, an admission layer can issue a bounded number of reservation tokens before requests reach the authoritative row. Tokens must themselves be durable or reconciled; a process-local counter loses capacity during a crash. If inventory is split across warehouses, route to one warehouse authority per reservation and keep the customer-facing total as a projection. Cross-warehouse allocation improves availability but makes the invariant and compensation path more complex.
Do not use a cache hit as proof that stock exists. The cache may advertise availability to improve browsing, but the conditional write remains the final decision. Track request rate, conditional-update conflicts, lock wait time, reservation conversion, expiration backlog, negative-stock attempts, and reconciliation differences per SKU. A hot-key dashboard is more useful than a fleet-average CPU graph.
Interview Follow-Ups
1. Why is an atomic update enough?
The database takes a row lock or equivalent conflict control while evaluating the predicate and applying the decrement. Only one transaction can observe the last unit as available. The affected-row count gives a definitive success or failure.
2. When would you use pessimistic locking?
Use it when contention is high and the critical section is short, such as decrementing one inventory row. Avoid holding it across payment or shipping calls. For lower contention, optimistic version checks reduce waiting but require retries.
3. How do reservations expire safely?
A worker claims expired rows with UPDATE ... WHERE status='HELD' AND expires_at < now(), then increments stock in the same transaction. It must be idempotent and race with payment using a state transition, so a late payment cannot resurrect an expired hold.
4. Can the cache be authoritative?
Only if the entire inventory invariant and durability model live in that store. A cache in front of the database is an optimization, not proof of availability. Rebuild or reconcile it after failures.
5. What metrics show oversell risk?
Track negative-stock attempts, reservation conflicts, lock wait time, expiry backlog, queue age, and reconciliation differences by SKU. Alert on any negative authoritative stock, even if the customer-facing order was later canceled.
Related Practice
Review quorum writes, backpressure, and duplicate payments.
References
YouTube Videos
- “Designing an Inventory System” - Gaurav Sen [https://www.youtube.com/watch?v=Ibf7wKf8MqU]