Design an Inventory System
medium 45 minCount 10 million products in 100 warehouses and never promise a unit twice.
Video lesson · Chapter 1 of 5
Two mugs on the screen and one real shelf
An inventory system in 90 seconds
1:28Reel coming soon
Theory
Read or listen first. About 1 min.
What it is
An inventory system knows how many units of each product are in each place, and how many of them are already promised to somebody. Shops, warehouses and marketplaces all ask it and all change it.
Two things make it different from a counter. A unit can be held while a shopper pays, and the hold must end by itself. And the truth is a physical shelf, so the system must be able to find out that it is wrong and say why.
Why it exists
Without it a shop sells things it does not have. The order is cancelled, the money goes back and the shopper does not.
Being too careful costs as well: units stuck in abandoned baskets or forgotten in a lorry cannot be sold. Good design promises every unit once, and only once.
A short history
1913
Ford W. Harris describes the economic order quantity: how much to order so that ordering and storing together cost least.
26 June 1974
The first product barcode is scanned, in a Marsh supermarket in Ohio, says GS1. Counting by scanning begins.
January 2016
PostgreSQL 9.5 adds SKIP LOCKED, so several workers can take different rows of one table without waiting for each other.
Words to know
- SKU
- Stock keeping unit: one product in one variant, for example a red mug.
- On hand
- Units physically in a place.
- Held
- Promised for a short time to a shopper who is paying.
- Allocated
- Promised to a paid order, not yet shipped.
- Available
- On hand, minus held, minus allocated, minus a safety amount.
- Movement
- One line in the history: what changed, where, by how much, who and why.
- Cycle count
- Counting a few shelves every day, so that all are counted over time.
- ATP
- Available to promise: what we can promise for a given date, including goods on the way.
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.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Look. A product page reads a cached number that may be a few seconds old
Understand it
Shelf, cart and lorry are three places. A pot is always in exactly one of them.
Scope
What are we building?
The stock service of a large shop: 10 million SKUs, 100 warehouses, 500,000 orders on a normal day and ten times that on a sale day. One extreme sale of a single product is a flash sale, and a numbered seat is Ticketmaster. Here many products are sold in many places at once.
Promise once
Two shoppers never get the same unit.
Let go
Abandoned baskets return their units.
Read a lot
Product pages outnumber orders 200 to 1.
Explain
Every change has a reason and an author.
Similar systems
| System | Kind |
|---|---|
| SAP, Oracle NetSuite | Business software with stock as one part |
| Shopify inventory | Stock of one shop over several channels |
| Fulfilment networks | Many warehouses with promises by date |
| Automatic replenishment | Ordering again when stock is low |
| Store systems | Tills that change shelf counts |
The finished system
Where the design ends up
The last version. The Design tab starts with one service and one database.
The system
Where to read what
| Question | Place |
|---|---|
| How do I open this interview? | Approach |
| How big is it? | Numbers |
| How does it grow? | Design |
| Holds, baskets, transfers? | Deep dives |
| Can I practise it? | Mock |
The ideas and their own pages
| Idea | Page |
|---|---|
| Conditional updates and locks | Concurrency control |
| Safe repeats | Idempotency |
| Reading from a cache | Cache-aside |
| Events from the database | Change data capture |
| Work over several databases | Saga |
| Separate read models | CQRS |
| Splitting data | Sharding |
Summary
The whole topic in one minute.
One row for each product and place holds three numbers: on hand, held and allocated. A hold is one conditional update, and it carries its own end time. Payment turns it into an allocation; shipping lowers on hand.
Everything else serves that row. A cache answers the product pages, an outbox writes the history, shards carry a sale, and daily counting compares the numbers with the shelves.
- Available = on hand - held - allocated - safety.
- Check and change in one statement, and look at the number of rows changed.
- A hold has an end time, and only the state change on its own row frees the units.
- Show from the cache, promise from the database.
- The shelf is the truth. Corrections are new lines, never edits.