Quick revision
The short answer
Adding shards does not help because every checkout updates the same stock row, and all of them queue on that one row lock on one shard; throughput is capped by how fast a single row can be locked, changed and committed. We scale by splitting the stock into many sub-bucket rows decremented with conditional updates, or by keeping the hot counter in Redis with an atomic Lua script, and by putting admission control in front so only about as many buyers as units reach checkout. Reservations expire if payment does not complete, and order creation is idempotent so retries never reserve twice.
What to remember
In this answer, we saw that sharding spreads data but cannot spread contention on one row: every checkout queued on the same lock, capping throughput at a few hundred updates per second. We split the stock into sub-bucket rows with conditional updates, or moved the counter to Redis with an atomic Lua script, and put admission control in front so only about as many buyers as units reach the counter. Expiring reservations and idempotency keys keep the count correct when buyers abandon payment or retry.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Adding shards does not help because every checkout updates the same stock row, and all of them queue on that one row lock on one shard; throughput is capped by how fast a single row can be locked, changed and committed. We scale by splitting the stock into many sub-bucket rows decremented with conditional updates, or by keeping the hot counter in Redis with an atomic Lua script, and by putting admission control in front so only about as many buyers as units reach checkout. Reservations expire if payment does not complete, and order creation is idempotent so retries never reserve twice.
Why this question is asked
Flash sales are a classic way to discover that a well-sharded database still has a single point of contention. Interviewers ask this because it separates two ideas people often mix up: spreading data across machines, and spreading contention on one piece of data. In this answer, we will see why the row lock caps throughput, then look at three redesigns, sub-bucketed stock, an in-memory counter, and admission control, and finish with the reservation rules that keep the system from overselling or losing units.
1Why One Row Caps the Whole Checkout
Each checkout runs something like UPDATE stock SET qty = qty - 1 WHERE item = 77 AND qty > 0. To change the row, the database takes an exclusive row lock, and it holds that lock until the transaction commits. A second request for the same row must wait. So requests for one item form a single queue, no matter how many CPU cores or shards exist.
Let us put a number on it. If locking, changing and committing the row takes about 2 milliseconds, including the commit's disk flush, that row can absorb at most around 500 updates per second. The sale sends 12,000 requests per second. The rest wait on the lock, waits pile up, connection pools fill, CPU goes into lock management and context switches, and most requests hit their timeout. Adding shards spreads other items across machines, but item 77 still lives in one row on one shard.
2Redesign 1: Split the Stock Into Sub-Buckets
If one row is the bottleneck, we use many rows. We store the 5,000 units as 50 bucket rows of 100 units each. A request picks a random bucket and decrements it with a conditional update. If the bucket is empty, the update changes zero rows and the request tries another bucket.
The condition qty > 0 is what prevents overselling: the database checks and decrements in one atomic statement, so no bucket ever goes below zero. With 50 independent row locks, the theoretical ceiling rises from about 500 to about 25,000 updates per second. There is a catch near the end of the sale: when most buckets are empty, requests try several before finding units, so the client should try a few random buckets and then answer sold out, or keep a small hint of which buckets are known to be empty.
3Redesign 2: Keep the Hot Counter in Redis
Another option is to take the counter out of the database. Redis executes commands one at a time on a single thread, so a Lua script runs atomically, and it can do well over 100,000 simple operations per second. We keep the stock in a Redis key and decrement it with a script that refuses to go below zero.
A plain DECR would happily go to -1, -2 and so on under load, which is why the check and the decrement live in one script. The order itself is then written to the database asynchronously, for example through a queue, at a rate the database can sustain. The price is that Redis is now the source of truth for remaining stock during the sale, so we must think about losing it: we reconcile the counter against confirmed orders after the sale and on any Redis failover, because an asynchronously replicated Redis replica may have missed the last few decrements.
4Admission Control in Front of Checkout
Both redesigns make the counter faster, but we are still sending 12,000 requests per second at 5,000 units. Most buyers cannot succeed, and the cheapest answer to give them is a fast sold out. Admission control does this. A queue or token gate in front of checkout hands out about as many entry tokens as there are units, perhaps with a small margin for abandoned payments. Requests without a token get a waiting-room page or a sold-out answer from the edge, without touching the stock store at all.
This changes the shape of the problem: instead of 12,000 contested decrements per second, the stock store sees a few hundred well-behaved ones, and the user experience improves, because people learn quickly whether they got in.
- Edge and waiting roomqueue or token gate, about 5,000 tokens; others get sold out fast
- Checkout serviceidempotency key per attempt, reservation with expiry
- Stock counter50 bucket rows or a Redis key with a Lua check
- Order databaseorders written at a steady rate, reconciled with the counter
- Paymentconfirms or lets the reservation expire
5 layers, from the top down. Step through them one at a time.
Each layer removes load from the one below, so the contested counter sees only admitted buyers.
5Reservations That Expire and Requests That Can Be Retried
Decrementing stock is a reservation, not a sale. Many buyers abandon payment, so every reservation carries an expiry time, for example ten minutes, and a background job returns expired units to a bucket or the Redis counter. Without this, abandoned carts would silently sell out the item while 30 percent of units are never paid for.
Retries are the other danger. Under load, clients time out and retry, and a retry that reserves again takes a second unit for the same buyer. So each checkout attempt carries an idempotency key, and the reservation is stored under that key with a unique constraint. A retry finds the existing reservation and returns it instead of decrementing again. These two rules, expiry and idempotency, are what keep the count of units honest through all the failure cases.
6Follow-up questions
Why not use SELECT ... FOR UPDATE and check stock in the application?
It holds the same single row lock, and holds it longer, across a round trip to the application. Throughput gets worse, not better. The conditional UPDATE does the check and the change in one statement inside the database, which is the shortest possible lock.
Which redesign would you pick?
For a one-off sale on existing infrastructure, sub-bucket rows keep everything in the database, so orders and stock commit together and there is nothing to reconcile. For very large, frequent sales, a Redis counter plus asynchronous order writes scales further, at the cost of reconciliation. In both cases I would add admission control first, because it removes most of the load for little effort.
How do you avoid overselling if Redis fails over mid-sale?
Redis replication is asynchronous, so a promoted replica may show more stock than really remains. We can pause sales on failover and rebuild the counter from confirmed orders and live reservations, or keep a small safety margin and run a final reconciliation that cancels and apologises for any oversold orders. WAIT can make a write wait for replicas but does not make Redis strongly consistent.
7Conclusion
In this answer, we saw that sharding spreads data but cannot spread contention on one row: every checkout queued on the same lock, capping throughput at a few hundred updates per second. We split the stock into sub-bucket rows with conditional updates, or moved the counter to Redis with an atomic Lua script, and put admission control in front so only about as many buyers as units reach the counter. Expiring reservations and idempotency keys keep the count correct when buyers abandon payment or retry.
Open with
Adding shards would not help because the problem is one row, not one machine. Every checkout updates the same stock row, so every request waits for the same row lock, and if a lock, change and commit takes about two milliseconds, that row tops out around 500 updates per second while 12,000 arrive.
Then, if they want more
Sharding spreads other items, but this item still lives in one row. I would redesign it in three layers. First, admission control: a waiting room or token gate hands out roughly as many tokens as units and tells everyone else sold out quickly, so the stock store sees hundreds of requests per second instead of thousands.
Second, I split the counter: either 50 bucket rows of 100 units, decremented with UPDATE ...
WHERE bucket = random AND qty > 0, so the check and decrement are atomic and there are 50 independent locks, or a Redis key decremented by a Lua script that refuses to go below zero, with orders written to the database asynchronously and reconciled afterwards.
Third, correctness rules: a decrement is a reservation with an expiry, say ten minutes, and expired units go back, and each checkout attempt has an idempotency key with a unique constraint so a client retry returns the existing reservation instead of taking a second unit. That combination scales without overselling.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Why would adding shards not help, and how would you redesign stock reservation so it...": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/bc054bffe3/