AlgoViz

Design an inventory management system

medium 80 min

Count goods in several places, promise them to orders without ever promising too much, ship or let go, and explain every count.

Watch lesson4:28

Ledger

Why the count is what it is

The point: the counts are a cache of the ledger. If they can be rebuilt from it, they can be checked, explained and repaired.

Two books

Movements and promises

MovementPromise
AnswersWhat happened to the jars?Who may have them next?
ChangesNeverStatus only
SumOn handHeld, if open
LivesYears, for the booksDays
Writing holds into the movement ledger is a common mistake. Then the sum is available, and a physical count cannot be compared with anything.

Count

The shelf says 5, the book says 7

Somebody counts the shelf. The difference is written as a movement of its own with a reason. The old lines are not corrected, because they were true when they were written: that many jars did arrive.

Python
class Line:    def __init__(self):        self.ledger = []                 # (change, reason)        self.promises = {}               # id -> qty, oldest first    @property    def on_hand(self):        return sum(change for change, _ in self.ledger)    @property    def held(self):        return sum(self.promises.values())    def move(self, change, reason):        if self.on_hand + change < 0:            raise ValueError("below zero")        self.ledger.append((change, reason))    def count(self, seen, reason):        """Set the book to what was seen. Returns the promises that can no longer be kept."""        if seen != self.on_hand:            self.move(seen - self.on_hand, f"count: {reason}")        broken = []        for pid in reversed(list(self.promises)):   # the newest promise breaks first            if self.held <= self.on_hand:                break            del self.promises[pid]            broken.append(pid)        return brokenline = Line()line.move(+6, "delivery 12"); line.move(-2, "order 3"); line.move(+3, "delivery 13")line.promises = {"order-5": 4, "order-6": 2}assert (line.on_hand, line.held) == (7, 6)assert line.count(5, "two jars broken") == ["order-6"]assert (line.on_hand, line.held) == (5, 4)assert line.ledger[-1] == (-2, "count: two jars broken")print("ok")
The sums are worked out for clarity. The real line stores them and compares at night.
Edit and run it in your browser

A count finds fewer jars than are promised. Now what?

BreaksRefuse the count
How
The program says that held may not be above on hand.
Limit
The jars are broken whether the program likes it or not. Staff will stop entering counts.
Choose when
Never. Facts win.
WorksAllow available below zero
How
Accept, and show available as minus 1 until somebody acts.
Limit
The first invariant now has an exception, and every check must know it.
Choose when
When people decide case by case.
StrongBreak promises by a rule, and tell
How
Newest first, or not yet paid first. Each broken promise is reported to the shop.
Limit
The rule is a business decision and belongs in an interface.
Choose when
When it has to run without people.

Restart

Rebuild, and do not replay for ever

  1. 1

    Snapshot

    Once a day, store each line with the number of the last movement in it.

  2. 2

    Load

    At start read the snapshot, then the movements with higher numbers.

  3. 3

    Promises

    Read the open ones, add up held, close those that are overdue.

  4. 4

    Check

    Run the four rules before the first order is accepted.

The same shape as a log and a snapshot in a database. See change data capture for logs between systems.