The job of inventory
The inventory system answers one question for every product: how many units are available to sell right now. Get it wrong and you either oversell stock you do not have or hide stock you could be selling.
On hand versus available
- On hand is the physical count in warehouses.
- Reserved is units held for carts and unconfirmed orders.
- Available to sell equals on hand minus reserved.
Exposing only available to sell prevents two shoppers from buying the last unit.
Consistency matters
Inventory writes must be atomic. Decrementing a count needs a transaction or a conditional update so two concurrent buyers cannot both succeed on the final unit. This is the classic race condition that causes overselling.
Scaling reads
Reads of stock counts are far more frequent than writes, so cache availability for display, but always re check the authoritative count at the moment of reservation or order placement.
Key idea
Track on hand and reserved separately, expose available to sell, and make the final decrement atomic to avoid overselling.