Race-Safe Checkout for One-of-a-Kind Products in Next.jsRace-Safe Checkout for One-of-a-Kind Products in Next.js
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started
Two people start checkout for the last moon jar at the same moment. Only one of them can get it.
Kilnworth is a small demo shop I built for a made-up ceramics workshop, in Next.js 16 on Cloudflare Workers. Three of its thirteen pieces are one-offs with a stock of 1, so much of the code is about that moment.
The stock check sits inside the database write. Checkout takes the piece with an UPDATE that only succeeds while one is left, so there is no gap between reading the stock and taking it. The second buyer's UPDATE changes no row, and the page tells them which piece ran out.
A test fires ten concurrent holds at a product with stock 1 and checks that exactly one succeeds.
Starting checkout holds the jar for ten minutes, and a hold that runs out goes back on the shelf before anyone reads the stock. The order itself is confirmed only by Stripe's signed webhook, never by the browser coming back from payment.
Post image
Back to feed
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started