EngineeringDon't Store Inventory as a Number
- #inventory
- #data-model
- #tradeoff
- #scale
It was a practice project: business screens for a company that sells shoes through its stores, its own online store, and external marketplaces. Staff at headquarters, the stores, the online store, and the marketplaces pick up the same products, orders, and stock from one another. This post records how those screens store inventory.
The short version: I did not store the stock number. Stock was computed as the sum of a movement log. Units already promised were held separately.
Four places share the same stock
One of the company's stores was writing a single sale into five places. Headquarters receiving, store sales, online orders, and marketplace shipments all add to and take from the same stock.
With that structure, inventory needs atomicity. In the retrospective I asked whether the same solution would make sense for a tiny corner shop, and this was my call:
In the end, what matters is engineering that fits the scale.
Since then I size a solution by how many people and how many places handle the same information. This company was a case of four places sharing one stock.
A movement log instead of a number
When the storage layer was rebuilt, the stock-number field went away. Every receipt, sale, return pickup, transfer, and adjustment appends one line to stock_movement. Lines are never edited, only appended. Stock at a location is the sum of that location's lines.
Store the number
One stock field, edited on every operation. Nothing records why the number is what it is.
Store the movements
One line appended per operation. Stock is the sum. Every number has the lines behind it.
Starting quantities that no record could explain were filled in with lines whose reason is "opening stock". Recomputing from the log changed two headquarters stock values from what the old screen showed (6 to 8, 5 to 6). The new values match the receiving and shipping records.
A check script ran about 80 operations and all of them passed. Total stock matched opening plus receipts, sales, return pickups, exchange shipments, and adjustments. Transfers between locations and inspection results summed to zero.
On-hand stock and available stock
A single number could not tell two things apart. For example, if a store has three pairs on the shelf but one is promised to a prepaid customer, only two can be sold.
So holds live separately in stock_hold: units reserved by prepayments, online orders, and exchanges. Available stock is on-hand stock minus the holds at that location.
const free = (loc: number, skuId: number, except?: StockHold) => stock(loc, skuId) - held(loc, skuId, except);
export const qty = (loc: Loc, id: string) => stock(locId(loc), Number(id));
export const total = (id: string) => LOCS.reduce((a, l) => a + qty(l, id), 0);
export const avail = (loc: Loc, id: string) => free(locId(loc), Number(id));A prepayment is refused when there are no units to hold. It looks at headquarters, then other stores, then incoming stock, and if none has units it is not accepted. The point of holds is to never promise the same units to two customers.
The screens show the distinction too. The default number is available stock. When there are holds, available and on-hand are shown together. Available stock is the value that decides whether an operation can go through. The quantities for choosing a shipping location, marketplace shipments, and headquarters shipments are also judged against available stock.
Screens read only through functions
I first asked whether moving the back-office data to the real structure would be expensive. The agent answered that a full replacement would be expensive and a step-by-step one would be cheap. A full replacement meant rewriting 25 operation functions and four screens, and the presentation depended on the current build. Its first recommended step was the movement log and holds.
The reason a step-by-step change looked cheap was on the screen side. Screens never read stored values directly. They read and wrote data only through functions like qty(), total(), and actions. Keep those names and shapes, and the inside can change with almost no screen edits.
I still asked for the full replacement, and the storage layer became 28 ERD tables. Midway, the agent started editing four screens at once. I stopped it and narrowed the scope to storage. Another session was redesigning the screens at the time. The storage agent kept the names and shapes the screens used, and not a single screen file changed.
28
Tables in the new storage layer
25
Operation functions rewritten
0
Screen files changed in the swap
~80
Operations run by the check script
All passed
One more thing was kept for the same reason. Returns and exchanges stayed at the screens' three steps, because splitting them into four would break the screens' step check. Things the screens newly had to show, such as available stock, were added on the screen side after the storage swap. I did not get to check how those screens behaved that day.
The remaining limit
These screens have no server and store data only in the browser's localStorage. So several people cannot share the data. The practice app does not yet handle the very scale that called for atomicity.
Two things were confirmed. With stock as the sum of a log and promises as holds, on-hand and available stock separate. With screens reading only through functions, the whole storage layer can be replaced while the screens stay as they are.