How inventory-reservations works
inventory-reservations is a small inventory service with one job: when two customers reach for the last unit of a product at the same moment, exactly one of them gets it. Stock is kept as a history of every promise rather than a number that gets changed, so when the warehouse count disagrees there is something to compare it with. It is built for three people: the store that must never sell what it does not have, the operator who has to explain a stock level after the fact, and the reviewer with two minutes who wants to know whether the person who built it understands the problem. The demo runs the service's own code against a real Postgres database inside your browser, with the same tables, roles and permissions as a deployment. It takes a few seconds to start and says so. Nothing to install, and nothing you try in the demo leaves your machine. The site does count visits to its pages, how long each is read and how far down, and which links out are followed, with a service called Plausible that sets no cookie. It counts nothing when your browser sends Global Privacy Control, a privacy setting some browsers offer.
What happens when an order comes in
- The order names a product and a quantity. It can also carry a receipt, a code the sender picks, so the same order can be sent twice without being placed twice.
- It waits its turn. Orders for the same product line up and go one at a time. Each reads the shelf after the order ahead of it has finished, so nobody decides from a stale picture.
- The service adds up what is really available. Stock on hand is one number that changes only when goods ship. Everything promised to orders or set aside in checkouts is a line in the history, and what is available is worked out fresh on every read, never stored.
- It says yes or no. Yes adds one line to the history for the order. No comes back with how many were actually available. If the order carried a receipt, the receipt is saved in the same step, so a crash cannot leave one without the other.
- Later, the order ships or is canceled. Shipping lowers the count on hand and adds the line that closes the order. Canceling adds that line and leaves the count alone. No line is ever edited or removed.
What usually goes wrong, and does not here
Each of these is a mistake real stores make. Each has a test behind it in the repo. Each link opens the demo at the right place, and your browser's back button brings you here. In the demo, a refusal is the rule working, which is why it is shown as a success.
Two customers, one unit
Both read one available, both are told yes, and the store has sold something it does not have. The history records that faithfully and does nothing to stop it. Here orders for a product take turns, and the second one reads the first before it decides. The gray box under each step is the exact instruction sent to the database, and you can ignore it. Press "Next step" to the end with "No protection", then press "Row lock" and do it again.
A second try with the same receipt
A customer's connection drops after they press buy, the app retries, and one purchase becomes two orders. Here every order can carry a receipt, and a second try with the same receipt gets the first answer back and places nothing new. The receipt is kept long enough to outlast every retry. Place an order, then press "Retry the last order".
The cart that walked away
A shopper puts the last unit in a cart, wanders off, and nobody else can buy it. Here a checkout hold sets stock aside for a fixed time, fifteen minutes in the service and one minute in the demo so you can watch it. The moment it runs out the unit is back on sale, and a cleanup job writes down afterward how the hold ended. Press "Hold for checkout" and wait a minute.
The number somebody edited
A quantity column can be changed by a bug or a well-meaning fix, and afterward nobody can say why. Here the service connects as a role that cannot change or delete anything, and the database refuses an edit from its owner too. The only way to correct the history is to add a line that says so. In the history a negative line takes a unit away and a positive one gives it back. Under "Try to break it", find "Edit a ledger entry as the service" and press "Try it", then do the same for "Edit it as the database owner".
One store seeing another's stock
Many stores share one database, and a query that forgets its filter shows everyone's rows. In the demo, Acme and Globex are two stores sharing one database. Here the API key decides which store is asking, and the database checks again on every row, so a query that forgets gets nothing, not everything. Under "Try to break it", find "Read stock without saying whose" and press "Try it".
The mistake nobody noticed
A bad entry in the history makes a product look more available than it is, until a customer is told yes for a unit that does not exist. Here a reconciliation job checks every history against the rules a correct one keeps and reports what is wrong. It fixes nothing, because an automatic fix would guess, so a person records the correction instead. Press "Write a bad entry", then "Run reconciliation", then "Fix it with a compensating entry".
The guided part of the demo is the race at the top. Press a strategy, then press "Next step" to watch each buyer's turn.
Under the hood, briefly
The rest is for engineers.
The service is TypeScript on Node with Postgres behind it, and it ships as a distroless container with health and readiness probes and scheduled jobs that run from the same image. The image is built from base images pinned by digest. CI scans it and the lockfiles for known vulnerabilities, and fails on any high or critical one that has a fix. The published image carries a bill of materials and a record of how it was built. The ledger is append-only, enforced by a database trigger, available stock is derived on every read, and orders for one product queue on a row lock. That choice was measured rather than assumed: with 32 buyers on one product, row locking held about 700 orders a second with the slowest 1% at 56 ms and none refused, where serializable transactions fell to about 320 a second with the slowest 1% at 584 ms and some refused once retries ran out. The demo charts those numbers. Tenancy is enforced twice, by the API key and by row level security, and the service runs as a role that cannot delete anything. Every choice with a real alternative is a decision record.
The README is the case study, and the decision records are beside it.
Who made it
Jason Poteet, as a portfolio project. If this is the kind of work you are hiring for, get in touch.