Two checkout requests arrive 4 ms apart for the last unit of a SKU. Both read on_hand = 1 , both pass the check in application code, and both write. Stock ends at -1 and one customer gets an apology email. This race appears wherever a web store, a marketplace sync, and a warehouse scanner write to the same row, and it grows worse with every added channel or worker process. It is the first bug we design against in inventory management software development for concurrent, multi-writer systems . The fix is small: let PostgreSQL decide the outcome, not your Node.js process.
Context and Setup
The scenario is a Node.js 20 API with pg talking to PostgreSQL 15, serving several channels that reserve stock before payment clears. PostgreSQL is a safe base for this: the 2025 Stack Overflow Developer Survey reports it as the most-used database among professional developers (55.6%).
You need two tables. stock_levels holds sku , location , on_hand , and reserved .…