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.
Video lesson · Chapter 1 of 6
Make a prediction
Theory
Read or listen first. About 2 min.
What it is
An inventory in a low-level design round is the part of a shop that knows how many pieces of each product are in each place, and who they are promised to. It accepts deliveries, reserves pieces for orders, and records what leaves.
Its core is small: a stock line with two counts for each pair of product and place, a reservation that can end in one of three ways, and a ledger of movements that explains the counts.
The difficulty is one method. Reserving an order has to look at several products in several places and then take all of them or none, while other orders do the same.
Why it exists
Promising a piece twice is the mistake everybody understands. A customer has paid and the shelf is empty. The design must make it impossible and not unlikely.
The same shape appears in many systems: seats, rooms, connections, licences, money on an account. Something limited is held for a while, then used or given back.
It also shows whether a candidate separates facts from decisions. What is on the shelf is a fact. Which place sends it, when to warn and who loses when a jar breaks are decisions of the business.
A short history
Middle Ages
In Europe debts are cut into split tally sticks. The longer half, kept by the one who advanced the money or goods, was called the stock.
1494
Luca Pacioli's Summa de arithmetica is published in Venice, with a description of double-entry bookkeeping: every change has two sides.
1913
Ford W. Harris develops the economic order quantity model: how much to order so that ordering and storing cost least together.
26 June 1974
The first product with a Universal Product Code is scanned, a pack of chewing gum in Troy, Ohio. Counting at the till becomes a machine's work.
Words to know
- SKU
- Stock keeping unit: the name under which one kind of thing is counted.
- On hand
- Pieces that are physically there.
- Held or reserved
- On hand, and promised to somebody.
- Available
- On hand minus held. Only this may be sold.
- Reservation
- A promise of certain pieces, with an end.
- Pick
- So many of one product from one place.
- Movement
- One line of the ledger: a signed change with a reason.
- Oversell
- To promise more than there is.
- FIFO and FEFO
- First in, first out. First to expire, first out.
- Invariant
- A sentence that is true after every operation.
The solution, twice
Reach the regular design first. Then go where a FAANG interviewer digs.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3API and dataThe contract first, then the data that serves it.
- 4DesignStart with the smallest system that works. Change it only when something breaks.
- 5Deep divesCompare the options before you open them, then say which you would pick.
- 6Explain your choicesAnswer first. Then compare your reasoning.
- 7By levelThe same question is judged differently for mid, senior and staff.
- 8MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Receive. Goods arrive with a reason.
Understand it
The problem
Three numbers that look like one
A shop asks a simple question: how many jars of plum jam do we have? There are three true answers. On hand is how many stand on the shelf. Held is how many of them are promised to somebody. Available is the rest, and only these may be sold.
The tag is the trick: a jar can be on the shelf and not for sale.
Words
Four verbs, and what each one changes
| Verb | On hand | Held | Available | In the ledger? |
|---|---|---|---|---|
| Receive 3 | +3 | same | +3 | Yes |
| Reserve 2 | same | +2 | -2 | No: nothing moved |
| Ship 2 | -2 | -2 | same | Yes |
| Release or expire 2 | same | -2 | +2 | No |
| Count says 1 fewer | -1 | same | -1 | Yes, with a reason |
First design
What goes wrong with one number
The design to avoid: a number anyone may change
1/5A manager for everything
Counting, mails and cards in one class. It has four reasons to change, and a test needs all of them.
useshas (lives on)extends (is a)
The last jar is sold twice
# before: ask, then act, on a public numberif product.quantity >= qty: product.quantity -= qty # anyone else may be between these lines send_mail_if_low(product) # a slow call in the middle# after: one call that checks, changes and remembersreservation = inventory.reserve("order-7", {"plum": qty}, seconds=900)Second design
One coordinator, one guarded line
The design to reach: one coordinator, one guarded line, plain records
1/7Start from the verbs
Goods arrive, are promised, leave, or the promise ends. Four verbs on one coordinator, Inventory. Callers see nothing else.
implementsusesowns (dies together)
Life
A promise and its four states
One order from promise to parcel
Where
One product in several places
The pair of product and place is the unit that is counted. So the class is the line and not the product. A product is a name with a description. A place is a name. Neither of them has a count.
How the plan is found
FewestParcels. Another rule changes the middle boxes and nothing else.When do you take the jar out of the count?
BreaksWhen it ships
- How
- Only a shipped parcel lowers the number.
- Limit
- Between order and parcel the same jar is sold again. The seller finds out while packing.
- Choose when
- Never, if orders wait before they ship.
WorksWhen the order is placed, as sold
- How
- The number falls at once and rises again if the order is cancelled.
- Limit
- The shelf count and the number differ, so a physical count cannot be compared. Forgotten cancels lose jars.
- Choose when
- Goods without a shelf, such as licence keys.
StrongHold first, take at shipping
- How
heldrises with the promise and has an end.on_handfalls when the parcel leaves.- Limit
- Two counts and a sweeper.
- Choose when
- The default, whenever there is time between promise and parcel.
Risks
What can go wrong, and who stops it
Sold twice
Two orders see the same jar.
holdchecks and changes in one step, under the lock.Half an order
Fig held, plum missing. Plan everything before the first change.
Held for ever
The customer left. Each promise has an end, and the sweeper closes it.
Shipped twice
The parcel is scanned twice. The status is not HELD any more.
Nobody knows why
The count is wrong. The ledger has a reason for each change.
Forty mails
Each sale below the mark sends one. The line remembers that it has warned.
Keep
- Two stored counts, one worked out
- A promise as an object with picks
- A signed, append-only ledger
- Plan first, change after
- Alerts told outside the lock
Leave out
- A subclass for each kind of goods
- A public quantity field
isInStockas a public step before taking- A global instance
- Mail and cards inside the inventory
Limits
What this page is, and what it is not
This is the design of the classes in one process. How a large shop does it over many machines, with a database and queues, is Inventory System and Flash Sale System. Baskets and payment belong to online shopping. Claiming one of several equal things is also the heart of the parking lot; here the things are counted and not listed.
Summary
The whole topic in one minute.
Count a product in a place. Store on hand and held, work out available, and let one small class guard the rule between them.
A promise is an object with picks and an end. Reserve plans everything first and holds everything after, under one lock. Ship, release and expiry close it exactly once.
The ledger holds movements and not promises, so its sum is the shelf. Policies choose the place, listeners hear about low stock, and neither can change a count.
- On hand, held, available. Store two, work out the third.
- Held is at most on hand, and neither is ever below zero.
- Plan everything, then hold everything, under one lock.
- A promise keeps its picks and has an end.
- Shipping does not change what is available.
- The ledger adds up to the shelf.
Sources
- Shopify: inventory management apps
- Adobe Commerce: source algorithms and reservations
- Odoo: removal strategies
- Saleor: stock allocation
- Saleor: stock reservation
- Medusa: inventory concepts
- Square: Inventory API
- WooCommerce: product settings
- PostgreSQL: explicit locking
- Python: heapq
- Wikipedia: Universal Product Code