AlgoViz

Design an Inventory System

medium 45 min

Count 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

0:00 / 4:25

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

  1. 1913

    Ford W. Harris describes the economic order quantity: how much to order so that ordering and storing together cost least.

  2. 26 June 1974

    The first product barcode is scanned, in a Marsh supermarket in Ohio, says GS1. Counting by scanning begins.

  3. 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.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 4

Look. A product page reads a cached number that may be a few seconds old

Understand it

The plant nursery
8

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

SystemKind
SAP, Oracle NetSuiteBusiness software with stock as one part
Shopify inventoryStock of one shop over several channels
Fulfilment networksMany warehouses with promises by date
Automatic replenishmentOrdering again when stock is low
Store systemsTills 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

The shop checkout and the warehouse app call the gateway, which calls the stock service. The stock service reads display counts from Redis and writes to eight PostgreSQL shards. An expiry worker also changes the shards. The shards send movements through an outbox to Kafka, which feeds the history store and the reorder service. The reorder service sends orders to suppliers.
Blue is reading, pink is changing counts, orange is the history. Dashed arrows do not delay a shopper.

Where to read what

QuestionPlace
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

IdeaPage
Conditional updates and locksConcurrency control
Safe repeatsIdempotency
Reading from a cacheCache-aside
Events from the databaseChange data capture
Work over several databasesSaga
Separate read modelsCQRS
Splitting dataSharding

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.

Sources