Design an Inventory Management System
🏭 What is an inventory management system? It is the software that knows how many units of every product sit in every warehouse. When an order comes in, it holds the units the order needs, and when the order ships or is cancelled, it updates the counts. When a product runs low somewhere, it tells someone to restock.
This prompt comes in a few shapes: "design a warehouse stock system", "track stock levels across warehouses", "handle stock reservations", "send low-stock alerts". They are one design with four parts, and we cover all of them: warehouses and their stock levels, reservations that hold stock for orders, and low-stock alerts. If you have read /low-level-design/order-management, this is the Inventory class from that post grown up: several warehouses instead of one shelf, and a team that needs to hear when stock is low.
Requirements
"Design an inventory system for a retailer with several warehouses. Orders reserve stock, and the team wants an alert when something is running low."
Before any class, pin down what "stock" means and when it changes. These questions settle the design.
Clarifying Questions
You: What do we track: individual items, or counts?
Interviewer: Counts. For each product (we call it a SKU) and each warehouse, how many units there are. No serial numbers, no shelf locations.
You: What happens between an order being placed and the goods leaving the building?
Interviewer: The order reserves the units. Later the order system either commits the reservation, which means the units shipped, or releases it, because the order was cancelled.
You: If several warehouses have the product, which one fills a reservation?
Interviewer: Shipping from one warehouse is cheaper, so if any single warehouse can fill the whole thing, use it. Warehouses have a priority order; take the first one that can. If none can do it alone but together they can, split it, again in priority order.
You: Can stock be removed outside of orders, for damaged or lost units?
Interviewer: Yes, but never units that are reserved. Those are promised to a customer.
You: When exactly should a low-stock alert fire?
Interviewer: Each SKU has a threshold. When a warehouse's free stock of that SKU drops below it, alert once. Don't send another alert for every unit after that; only alert again after the stock has come back up.
You: Who receives the alerts?
Interviewer: For now, we collect them and someone reads the list. Later, email and the purchasing system will want them.
You: Do reservations expire on their own?
Interviewer: Not in this system. The order system decides when to release. We may talk about expiry later.
The alert answer is the subtle one. "Alert when stock is below the threshold" and "alert when stock drops below the threshold" sound the same, but the first sends an alert on every sale once stock is low, and the second sends one. Ask which one they mean before you code it.
💡 Interview tip: In any problem with counts, ask what the states of a unit are. Here a unit is free, reserved or gone. Naming those three states early gives you the data model (two counts per SKU per warehouse) and the three operations (reserve, commit, release) in one step.
Final Requirements
- The system has warehouses in a fixed priority order. Each warehouse tracks, per SKU, the units it holds and the units reserved.
- Stock can be added to a warehouse and removed from it. Removing never touches reserved units.
- A reservation holds a quantity of one SKU. It comes from the first warehouse (in priority order) that can fill it alone; otherwise it is split across warehouses in priority order; if all warehouses together cannot fill it, it fails and nothing is held.
- A reservation is closed exactly once, by a commit (the units leave the warehouse) or a release (the units become free again).
- Anyone can ask how many units of a SKU are available (held and not reserved) in a warehouse.
- Each SKU has a low-stock threshold. When a change takes a warehouse's available count from at or above the threshold to below it, one alert is raised. Another alert for that SKU and warehouse needs the count to come back up first.
- Alerts are collected for reading now, and other receivers (email, purchasing) must be able to plug in later without changing the stock code.
Out of scope
- Purchase orders and suppliers (an alert is where that process would start).
- Shelf locations, serial numbers, batches and expiry dates of goods.
- Reservations that expire, and moving stock between warehouses (both in Extensibility).
- Several threads at once (Extensibility).