Design an inventory management system
medium 80 minCount goods in several places, promise them to orders without ever promising too much, ship or let go, and explain every count.
Watch lesson4:28
Check yourself
One question at a time. Each answer explains why.
Question 1 of 13
A line has on hand 6 and held 2. An order of the 2 ships. What is available before and after?
Why are holds not written as movements?
An order asks for 2 fig and 9 plum. There are 4 fig and 7 plum. What is held after the refusal?
Which class should know that a place is out of reach for an order?
`available()` returned 1 and then `reserve` for 1 was refused. What happened?
Why are low stock listeners called after the lock?
Thread A takes locks plum then fig. Thread B takes fig then plum. What is the fix?
The sweeper is two minutes late. A customer ships a promise that ended one minute ago. What happens?
What makes `reserve` safe to repeat?
A count finds 5, the book says 7 and 6 are promised. What should the code do?
Batch A: received 1 May, good until December. Batch B: received 9 May, good until August. Which goes first with FEFO?
Why does `reserve` take a map of products and not a list?
Five jars leave the cellar for the shed by van. Where are they in the books meanwhile?
Pick an answer to see why.
Not quite. Before: 6 - 2 = 4. After: on hand 4, held 0, so 4. Available fell when the pieces were held.
Not quite. Nothing moved. A ledger that adds up to on hand can be compared with a count of the shelf.
Not quite. Planning only reads. The refusal comes before the first hold.
Not quite. It is a business rule on choosing places. The line only guards two counts.
Not quite. The number was true when it was read. That is why reserve checks again under the lock.
Not quite. The listener is not our code. It may be slow, or it may call back into the inventory.
Not quite. With one fixed order nobody can hold a later lock while waiting for an earlier one.
Not quite. `ship` asks the clock itself. Correctness does not depend on the sweeper.
Not quite. The repeat carries the same id and finds the first promise. A new id for each call cannot be recognised.
Not quite. Facts win, history stays, and somebody must learn which promise cannot be kept.
Not quite. First to expire, first out. With FIFO, batch B might go bad on the shelf.
Not quite. Two entries for plum would be planned separately, and each could count the same jars.
Not quite. Then they cannot be sold from either place, and a lost jar shows up as a number.
Flashcards
Say your answer, then reveal it.
Card 1 of 14
Which counts are stored?
On hand and held, for each product in each place.
Say your answer, then reveal it.
What is available?
On hand minus held. Worked out, never stored.
Say your answer, then reveal it.
The first invariant
0 ≤ held ≤ on hand, after every call.
Say your answer, then reveal it.
What is the sum of movements?
On hand.
Say your answer, then reveal it.
What is the sum of open promises?
Held.
Say your answer, then reveal it.
What does shipping change?
On hand and held fall together. Available stays.
Say your answer, then reveal it.
How is reserve all or nothing?
Plan every product first, then hold. The refusal comes before the first change.
Say your answer, then reveal it.
Why does a reservation keep picks?
To give back exactly what was held, where it was held.
Say your answer, then reveal it.
What does a pick policy change?
Nothing. It returns a plan or None.
Say your answer, then reveal it.
Who decides that a promise is over?
The clock, when somebody touches it. The sweeper only cleans up.
Say your answer, then reveal it.
Where are listeners called?
After the lock is released.
Say your answer, then reveal it.
How do you avoid deadlock with several locks?
Take them in one fixed order, for example sorted by product.
Say your answer, then reveal it.
The database form of hold
UPDATE with on_hand - held >= qty in the WHERE, and look at the row count.
Say your answer, then reveal it.
FIFO or FEFO for jam?
FEFO: a later delivery may end sooner.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.