Inventory Management
Lots, reservations, picking order and an audit trail
Design a system that tracks stock per product and warehouse, promises units to orders without overselling, picks the right lot first, moves stock between sites and keeps a record of every change.
Overview
An inventory system answers one question all day: how many units of this product can I promise right now, and where are they? Goods arrive from suppliers in lots, orders reserve units, pickers take them from shelves, trucks move them between warehouses, and someone has to be told when a shelf runs low. Every one of those steps changes a count, and the counts must never lie.
The interview version strips the warehouse down to a few objects: a product identified by its SKU, a lot that arrived together (with an expiry date if the product has one), a warehouse that holds lots, and a movement record for each change. The hard parts are the same as in a real system. Two orders must not both take the last unit. Milk that expires first must leave first. A transfer must not lose or double units. And an auditor must be able to add up the history and get the shelf count.
A strong answer says 'available, not on hand' early, puts one lock around each warehouse's check-and-change, makes the picking rule a pluggable strategy, and writes the audit row in the same step as the change. The code below does all four.
Scoping the problem
Before drawing anything, ask these questions, because each answer changes a class. Each comes with why it matters and the answer used below.
- One warehouse or many? Many means transfers and a lock per site. The design below has many.
- Do products expire or need lot tracking? This decides whether stock is a number or a list of lots. Use lots, with an optional expiry date.
- Do orders reserve stock before shipping? Reservation is what stops overselling between order and pick. Yes: reserve, then ship.
- Which lot leaves first? FIFO (first in, first out) suits most goods, and FEFO (first expired, first out) suits perishables. Make it a strategy object, with FEFO in the demo.
- Who needs to know about low stock? The choice is polling or notification. Call listeners at the reorder point.
- What must the audit trail prove? This decides whether a running total is enough or every movement is kept. Keep every movement, signed, in order.
- Units: whole items, or weights? Decimals need care. Use whole units, as integers.
Functional requirements
- receive(sku, warehouse, qty, lot_id, actor, expiry) adds a lot and writes a receive movement
- reserve(sku, warehouse, qty) promises units to an order and fails with InsufficientStock if fewer are available
- ship(reservation, actor) takes the promised units from lots in strategy order and returns the picks
- transfer(sku, from, to, qty, actor) moves units between warehouses, keeping lot identity and expiry
- The picking order (FIFO, FEFO) is a strategy chosen at construction
- A listener is called when a shipment brings a SKU to or below its reorder point
- Every change appends an immutable movement row with a signed quantity and the actor
Non-functional requirements
- Stock is never oversold: reservations plus shipments never exceed what is on hand, under concurrent callers
- Every operation on one warehouse is atomic under that warehouse's lock
- Transfers take two locks in a fixed order and cannot deadlock
- The movement log for a SKU sums to the units on hand across all warehouses
- Adding a picking rule or an alert channel adds a class and changes no existing one
Interview questions
- Two orders arrive at the same moment for the last 3 units. How do you make sure only one gets them?
- How does the picking strategy work, and how would you add a new rule?
- A transfer touches two warehouses. How do you avoid a deadlock?
- Why keep every movement instead of just updating a count?
- How would this change with millions of SKUs and many services?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio