Video in progress
A product is sold at the same time on your own site, a mobile app and two marketplaces, from three warehouses. How do you track stock without overselling and without losing sales for nothing?
I don't keep stock as one number: per warehouse I track "physically on hand", "reserved" and "available to sell". The inventory service is the single source of truth, and the available quantity, minus a safety reserve, is pushed to channels through events; reservation on an order is atomic and time-limited.
📖 Answer
The classic problem: the warehouse has 3 units. The site shows 3, the marketplace also shows 3, and they sync once every 5 minutes. Two channels each sell 2: 4 orders, 3 items. The marketplace fines you for the cancelled order and lowers your rating.
The model. For each (sku, warehouse):
on_hand — physically on hand (from the warehouse system)
reserved — held for orders that are unpaid or not yet picked
available = on_hand - reserved - safety_stock
The quantity shown to a channel is the sum of available across warehouses, sometimes with a per-channel share (a more cautious number for the marketplace where cancellation is costly).
Reservation is atomic:
UPDATE stock SET reserved = reserved + :qty
WHERE sku = :sku AND warehouse = :wh
AND on_hand - reserved - safety_stock >= :qty;
-- 0 rows = not enough, try another warehouse
The reservation expires (15 minutes to pay) and is released when it runs out. A marketplace order arrives through a webhook and goes down the same path, with an idempotency key.
Distribution. When available changes, an event goes out, and channel adapters push it to the marketplace API. Their APIs are slow and rate-limited, so changes are merged (debounce), but when stock is low they are sent faster. Safety stock exists precisely to cover this sync delay.
Which warehouse. At reservation time a warehouse is picked: the closest to the customer, and if possible everything in one shipment. These routing rules are a separate component, because the business changes them often.
Checking against reality. A stocktake, or damaged or returned goods, change on_hand from the outside. Daily reconciliation with the warehouse system, and differences go to an alert.
I explain the trade-off to the business with numbers: a large safety stock means less overselling but more goods left unsold. Tuning it by each SKU's sales velocity is far more useful than one global number.
Sign up to mark studied questions and save your progress.