Two buyers, one unit, and a lock
transcript
Eight units on hand. Six already reserved. That leaves two. Two buyers each want two units, at the same moment. Both check. Both see two available. Both are told yes.
Buyer A reserves two. Reserved goes from six to eight. Buyer B reserves two. Reserved goes from eight to ten. Ten promised. Eight on the shelf. Two units do not exist.
The code reads available, then writes reserved. Two separate steps. Between them, another buyer reads the very same number.
The check and the write must be one single step. In memory, that means one lock around both of them. In a database, it means a conditional update. Add only if enough remain.
This check lives inside the inventory class. Callers never do it themselves. Every reserve, confirm, ship and adjust takes the same lock. The lock is held for a moment. Plans are tiny.
Never read, then write. Check and reserve together, or not at all.
The full design is in the lecture. Plans, expiry, and an order state machine.