Inventory, overselling and flash sales
Sell the last item exactly once, even when thousands of people tap "Buy" in the same second, and tell them the truth while they wait.
Updated since you finished it. We changed this lesson after you marked it done. Your progress is kept.
What broke
A shop in Bengaluru runs a midnight sale: 500 pairs of a limited sneaker at ₹4,999. Marketing has been posting about it for two weeks.
At 00:00, about 40,000 people tap "Buy" within the first minute. The checkout code reads the stock, checks that it is above zero, and then writes the new number.
The sale page says "sold out" after 90 seconds. Everyone celebrates. The next morning, the warehouse counts 523 paid orders for 500 pairs. Twenty-three customers get a refund and an apology email. Several post screenshots online.
There was a second problem. During the first minute, checkout pages took 20 seconds to load. The stock row was the bottleneck. Every purchase had to wait its turn for that one row.
The idea in simple words
Overselling happens when two requests check the stock at the same time. Both see "1 left". Both decide to sell. Both write. This is a race between a read and a write. It is the same bug as the lost update in transactions and isolation levels.
The fix is simple: do not check in your app code. Ask the database to check and change the number in one step. If the number is already zero, the database changes nothing, and you show "sold out".
A flash sale adds a second problem: everyone wants the same row. The row lock that keeps you correct also makes buyers wait one at a time. So you also need ways to spread the work, and to let fewer people in at once.
The key idea. Correct stock is a single number that only the database may change, in one atomic step. Speed comes from splitting that number, and from controlling who reaches checkout. It never comes from skipping the check.
How it works
Step through two buyers racing for the last item, and the one-line fix. Use Next and Back, or the ← and → keys.
- One unit is left. Buyer A's request reads the stock row. It sees 1, so the code decides it can sell.
- At the same moment, Buyer B's request reads the same row. It also sees 1. Neither request knows about the other.
- Request A works out 1 − 1 = 0 in the app, writes 0 and creates order A. So far, so good.
- Request B also writes 0 and creates order B. Stock says 0, but two people paid for one item. One of them gets an apology email.
- The fix: let the database check and change the row in one statement. Request A's UPDATE matches the row, since stock is 1. It changes 1 row, so A gets the item.
- Request B's UPDATE waits until A's transaction commits. Then Postgres checks WHERE stock > 0 again on the new value, 0. It changes 0 rows, so the app shows sold out.
Fix 1: an atomic conditional decrement
Write the check into the UPDATE itself:
UPDATE items SET stock = stock - 1
WHERE id = 7 AND stock > 0
RETURNING stock;In Postgres, under the default Read Committed level, a second UPDATE on the same row waits for the first one to commit. Then Postgres checks the WHERE clause again on the new version of the row. 1 So when stock reaches 0, the next UPDATE matches no row.
Postgres reports this as UPDATE 0. That is not an error. 2 Your code must read the count, or the RETURNING result, and treat 0 rows as "sold out". To sell three items, use stock >= 3.
Fix 2: lock the row first
Sometimes you must read before you decide, for example to check a per-customer limit. Then use SELECT ... FOR UPDATE. It locks the row until your transaction ends. Other transactions that try to update or lock it wait. 3
Keep these transactions short. Never call a payment API while you hold the lock. If you lock more than one row, always lock them in the same order, such as by ID. Postgres advises this to avoid deadlocks. 3
Fix 3: reservations with expiry
You can hold stock when the buyer starts checkout, not only at payment time. Shopify's inventory model has states for held stock. It splits the units on hand into states such as available, committed and reserved. 5
A reservation is a row like "1 unit for cart 88, until 00:10". Your conditional UPDATE moves one unit from available to reserved. Payment turns the reservation into a sale. A small job, or a check at read time, releases reservations that expire. Without expiry, people who leave without paying keep stock held forever.
Pick the expiry from real data, like how long most people take to pay. A short expiry frees stock fast. Too short, and buyers lose the item while typing a card number.
The hot row
In a flash sale, every buyer updates the same row. A transaction holds the row lock until it commits. So, as Alibaba's PolarDB docs explain, updates to one hot row run one at a time. The docs say sharding the database or tables gives limited gains here. 6 Alibaba calls this the "inventory hotspot" problem. A researcher from its database team says it solved the problem in time for Double Eleven in 2013. 7
You cannot remove this waiting, but you can make each turn shorter. Do only the stock UPDATE inside the transaction. Do the rest of the order, like the address and the email, outside it.
Splitting stock into buckets
This section is our advice, not a method from the sources above. Split one stock number into several rows. For example, store 500 sneakers as 10 bucket rows of 50. Each request picks a bucket at random and runs the conditional UPDATE on it. Now 10 purchases can run at the same time, not one.
When a bucket is empty, try another. Near the end, a buyer may see "sold out" while one other bucket still has a pair. You can handle this with a final pass over all buckets, or a small job that moves leftover units. In Postgres, FOR UPDATE SKIP LOCKED skips rows that others hold. The docs say it gives an inconsistent view, but it suits queue-like tables. 4 That makes it useful to pick any free bucket.
Queues and virtual waiting rooms
Even a fast database has a limit. So control how many people reach checkout. Shopify learned this in its flash sales. Checkout writes were taking down parts of the platform. 8 It added a throttle, a limit on how many buyers get through, in its load balancers. Buyers over the limit saw a cached queue page that checked again from time to time. 8
The first version picked people at random. Some customers waited up to 40 minutes for a sale that lasted 40 minutes. 8 The fix stored each buyer's first attempt time in a signed cookie, so earlier buyers would not lose out to later ones. 9
You can also buy this as a product. Cloudflare's Waiting Room admits visitors based on two limits: total active users and new users per minute. Others wait in a first-in-first-out queue and see an estimated wait time. 10 Rate-limit checkout, not browsing. See rate limiting.
Caching availability vs truth
The product page might get 100 times more views than purchases. Do not hit the stock row for each view. Cache "in stock" or "few left" for a few seconds, as in caching and cache stampedes.
But the cache is only a hint. The conditional UPDATE at checkout is the truth. A stale cache may show "in stock" for a few seconds after the last sale. That is fine, as long as checkout says "sold out" clearly.
Being honest with users
Tell people where they stand. Show their place in the queue or an estimated wait. When stock runs out, tell everyone in the queue at once, so they stop waiting. When a buyer reaches checkout, say how long their item is held. If you ever do oversell, refund fast and say sorry plainly.
Trade-offs
| Option | Prevents overselling? | Speed on one hot item | Use when |
|---|---|---|---|
| Read, check in app, write | No | Fast, but wrong. | Never for stock. |
| Atomic conditional UPDATE | Yes | Limited by one row lock. | The default for most shops. |
| SELECT FOR UPDATE, then write | Yes | Slower. The lock is held longer. | You need other checks before the write. |
| Reservations with expiry | Yes , if made with a conditional UPDATE. | Same as the UPDATE. | Checkout takes minutes, such as card payments. |
| Stock split into buckets | Yes | Much better. | Flash sales of one product. |
| Waiting room in front | Indirectly. It limits load. | Protects everything behind it. | Planned big sales and ticket drops. |
| Cached availability | No It is only a hint. | Very fast reads. | Product and list pages. |
Simple rule: make the conditional UPDATE the only way stock goes down. Add buckets and a waiting room when one product gets a planned rush.
At work: how a senior uses this
- Search the code for "read stock, then write stock". Replace each case with a conditional UPDATE, and check the rows-changed count.
- Ask product: "How long do we hold an item in checkout? What do we tell a buyer when it expires?"
- Before a big sale, load-test checkout for the one hot product, not a spread of products. See load shedding.
- Add a check that runs every few minutes: units sold plus units available must equal the starting stock. Alert if they differ.
- Plan for the user, not just the database: a queue page, a clear "sold out" for everyone, and a refund plan.
- Keep the stock transaction tiny. No payment calls, emails or HTTP calls while a row lock is held.
With AI
What to ask the agent
"Find every place in this repo where we read stock and then update it in separate statements. Rewrite each one as a single conditional UPDATE, and handle 0 rows as sold out."
"Write a test that starts 50 parallel purchases against a product with stock 10, using a real Postgres database. Assert that exactly 10 succeed and stock ends at 0."
"Design a stock-bucket scheme for one product with 500 units in 10 rows. Show how a buyer picks a bucket, and what happens when the chosen bucket is empty."
What to check yourself
- The test really runs purchases in parallel, against a real database. A loop that buys one at a time proves nothing.
- The code checks the rows-changed count. An UPDATE that changes 0 rows does not raise an error.
- No network call happens inside the stock transaction.
- Expired reservations go back to available stock, and this has its own test.
- The agent did not "fix" the race with an app-level lock or a mutex. That breaks as soon as you run two servers.
Check yourself
Your progress is saved only in this browser. Export it to keep it safe.
You have finished 5 lessons. Your progress lives only in this browser. Export a copy so you don’t lose it.
Go deeper
Show 10 sources and related lessons
- PostgreSQL documentation, “Transaction Isolation” (Read Committed) (opens in a new tab)
What an UPDATE does when another transaction changed the same row first. It waits, then checks the WHERE clause again. - PostgreSQL documentation, “UPDATE” (opens in a new tab)
The RETURNING clause, and the "UPDATE count" result, where a count of 0 is not an error. - PostgreSQL documentation, “Explicit Locking” (opens in a new tab)
Row-level locks with SELECT FOR UPDATE, who they block, and how to avoid deadlocks. - PostgreSQL documentation, “SELECT”: the locking clause (opens in a new tab)
NOWAIT and SKIP LOCKED, and when skipping locked rows is safe to use. - Shopify developer docs, “Manage inventory quantities and states” (opens in a new tab)
A real inventory model with on_hand, available, committed, reserved and safety_stock. - Alibaba Cloud documentation, “Hot row optimization” (PolarDB for MySQL) (opens in a new tab)
Why updates to one hot row run one after another, and how the database groups them. - Alibaba Cloud blog, “A Decade of Evolution of Alibaba's Databases - Part 1” (opens in a new tab)
Alibaba's work on the "inventory hotspot" problem for its Double Eleven sales. - Emil Stolarsky, “Surviving Flashes of High-Write Traffic Using Scriptable Load Balancers (Part I)”, Shopify Engineering (2017) (opens in a new tab)
How checkout writes overloaded the database during flash sales, and the first checkout queue. - Emil Stolarsky, “Surviving Flashes of High-Write Traffic Using Scriptable Load Balancers (Part II)”, Shopify Engineering (2017) (opens in a new tab)
Making the queue fair, with a first-attempt timestamp in a signed cookie. - Cloudflare documentation, “About Waiting Room” (opens in a new tab)
Total active users, new users per minute, first-in-first-out order and estimated wait times.
Case studies: Shopify: Surviving flash sales with a checkout queue at the load balancer
Related: Transactions and isolation levels · Locking and MVCC in practice · Caching and cache stampedes · Rate limiting · Load shedding and graceful degradation · Shopping cart design (coming soon) · Checkout and the order state machine · Sagas, outbox and CDC
Before this: transactions and isolation levels and locking and MVCC in practice. Next: checkout and the order state machine, and sagas, outbox and CDC for releasing stock when payment fails.