KatInterview

Stock Reservation Service for a Flash-Sale Storefront

System DesignModel answer
MediumModel answerSystem Designconcurrency-controlidempotencyinventory

Last updated

Design a stock service that reports sellable quantity and reserves items for orders without overselling under concurrency, with idempotent retries, expiry, and contention handling.

Stock Reservation Service for a Flash-Sale Storefront

You are designing the stock-keeping part of an online store. The service must support two main capabilities:

  1. Availability lookup: report how many units of a product can currently be sold.
  2. Order placement: accept an order for one or more units and hold stock for it, so that the store never sells more than it owns, even when many buyers hit the same product at once.

Concentrate on contention on hot rows, where transaction boundaries belong, and how optimistic and pessimistic concurrency control compare.

Assumptions

  • Stock is tracked per SKU at a single logical warehouse.
  • Each placement request carries an idempotency key. It must reserve every requested unit or none of them.
  • Many concurrent orders may target the same popular SKU.
  • Cancelled or expired orders return their held units to stock.
  • Payment is out of scope, but the reservation must have a state that supports a later confirm or cancel step.

Questions worth raising with the interviewer

  • Can availability reads be a little stale, or must they reflect every committed reservation?
  • Must an order spanning several SKUs succeed or fail as a whole?
  • How long should a hold last before it expires?
  • How hot is the hottest SKU (requests per second)?
  • On failure, should the response identify which SKU was short and by how much?

What to deliver

Describe the data model and invariants, the API contracts (query, place, confirm, cancel, expire), the logic that prevents overselling, how retries are handled, a comparison of optimistic and pessimistic control under low and high contention, and how you deal with multi-SKU deadlocks, expiry, monitoring, and failure recovery.

Hints

Hint 1

Track three numbers per SKU: physically on hand, held by pending orders, and the derived sellable amount (on hand minus held). Decide which ones you store and which you compute.

Hint 2

The check 'is there enough?' and the write 'hold it' must happen as a single atomic step, for example a conditional UPDATE, so that two requests cannot both pass the check.

Hint 3

Compare what happens to a hot SKU when conflicting writers retry (optimistic) versus when they queue behind a row lock (pessimistic), and think about what changes for multi-SKU orders.