Menu

Stopping Black Friday Oversells: A Stock Reservation Data Model
📰

Stopping Black Friday Oversells: A Stock Reservation Data Model

DEV Community: database·Daniel Cruz·6 days ago
#HSDhjiwI
#devtodatabase#devto#dev#stock#available
Reading 0:00
15s threshold

The bug that only shows up during the sale

Most checkout flows work fine on a normal Tuesday. You read the stock count, check that it is above zero, take payment, and decrement it. The flaw stays hidden until many buyers go after the same discounted item in the same few seconds. Then two requests both read stock = 1 , both pass the check, and both charge a card for an item that only exists once.

This post walks through a data model that closes that gap. It uses short-lived reservations, atomic updates, and idempotent payment handling. The examples use PostgreSQL syntax, but the ideas carry over to any store that supports transactions or atomic conditional writes.

Why check-then-write fails

The naive version looks like this:

-- Request A and Request B run this at the same time SELECT stock FROM products WHERE id = 42 ; -- both see 1 -- application code: if stock > 0 then...…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.