(Note · · 6 min read)
The stock number is wrong and nobody can say why
The system says forty-two units. The shelf has thirty-eight. Somebody asks what happened, and the honest answer is that nobody can tell, because the only record is a number that has been overwritten a few hundred times.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
A mutable quantity has no history
Every change — a receipt, a sale, a return, a correction, a transfer between warehouses — writes over the previous value. When the number is wrong, there is nothing to inspect: no sequence, no causes, no way to find the step that introduced the error.
It is also the shape that invites the race described in any check-then-act bug. Two sales read the same quantity, both decide there is enough, both write a new value, and one movement vanishes.
Record movements, derive the balance
Store one append-only row per movement: what changed, by how much, when, why, caused by which document, entered by whom. Nothing is ever updated or deleted. The quantity on hand is the sum of movements.
This is the shape an inventory ERP needs, and the one I built for a trading business uses: purchase orders and goods receipts in, sales and returns out, adjustments and warehouse transfers between — each one a ledger entry rather than an edit to a number.
The gain is that every question becomes answerable. What was the stock on the last day of the month, why did this product drop by six, who entered the adjustment: all queries over rows that exist, rather than reconstructions from an audit log bolted on afterwards.
Corrections are entries too
When a count finds a discrepancy, the fix is not to edit history. Write a correcting movement with a reason, so the discrepancy itself is recorded. The fact that stock was wrong on a Tuesday is information somebody will want.
The same holds for reversals. A cancelled sale is a compensating entry, not the removal of the original — which keeps the pair visible and keeps every report that already included the original consistent.
Keep a cached balance, carefully
Summing the whole ledger on every page load stops being viable as it grows. Keep a running balance per product and location, updated in the same transaction as the movement, and treat it strictly as a cache of a derivable figure.
BEGIN;
INSERT INTO stock_movements (product, location, delta, reason, ref, actor)
VALUES ($1, $2, -3, 'sale', $3, $4);
UPDATE stock_balances SET qty = qty + (-3)
WHERE product = $1 AND location = $2 AND qty >= 3;
-- 0 rows affected -> not enough stock, roll back
COMMIT;The condition in the update is what prevents overselling, and it works because the check and the change happen together. Reconcile periodically — sum the ledger, compare to the cached balance, and alert on a difference. A cache that silently diverges is worse than no cache, because people trust it.
Availability is not the same as stock on hand
Units physically present are not units you can sell: some are reserved for confirmed orders, some are in transit between locations. Model reservations as their own entries so availability is a derived figure rather than a second mutable number to keep in step.
Reservations need an expiry, or an abandoned checkout holds stock forever. That expiry is itself a movement, which keeps the whole history consistent.
The pattern generalises
Account balances, loyalty points, leave days, credit limits, wallet balances: all the same problem. Wherever a number goes up and down and somebody will eventually ask why, the movements are the data and the number is a convenience.
Common questions
Why is my stock quantity wrong?
Usually because it is a single mutable column with no history, so every change overwrites the last and the error cannot be traced. It is also the shape that lets two concurrent sales both read the same quantity and lose one movement.
What does a stock ledger look like?
One append-only row per movement — what changed, by how much, when, why, which document caused it and who entered it — with nothing ever updated or deleted. The quantity on hand is the sum of those movements.
Is summing a ledger too slow?
At scale, yes, so keep a running balance per product and location updated in the same transaction as the movement, with the stock check as a condition on that update. Treat it as a cache: reconcile against the ledger periodically and alert on any difference.
How should stock corrections be recorded?
As a new correcting movement with a reason, never by editing history. The fact that stock was wrong on a particular day is information someone will want, and a cancelled sale should be a compensating entry so reports that included the original stay consistent.