The Hoarder
On a limited drop, bots pile in and oversell the inventory, leaving real customers with cancelled orders. A single atomic write in Cloudflare D1 guards the count, so it can never go below zero.
The pain
Limited drops invite a buying frenzy, and a read-modify-write race can oversell stock you do not have.
The fix
Guard the count with one atomic D1 UPDATE (qty > 0), so concurrent buyers can never push it below zero.
So what
No oversells, no refunds or angry customers, and a fair drop that holds up under bot-driven load.
Try it
Limited drop: 10 in stock.
Buy one at a time, or send a bot stampede of 15 simultaneous buys. The count never goes below zero, and the extra buys come back sold out. No overselling.
Real Cloudflare D1: UPDATE stock SET qty = qty - 1 WHERE qty > 0 is one atomic statement, so concurrent buyers can never race it below zero.
How it works
A single atomic D1 write guards the stock count, so concurrent buyers can never race it below zero.
Add it to your site
1 SQL statement · no raceconst { meta } = await env.DB.prepare(
'UPDATE stock SET qty = qty - 1 WHERE id = ?1 AND qty > 0'
).bind(productId).run();
const bought = meta.changes === 1; // 0 changes = sold out, no oversellOfficial Cloudflare docs