Database Concurrency & Scaling interview question · Most asked
Two customers buy the last item at the same moment and both orders are confirmed. What happened, and how do you prevent it?
Summary
A lost update: both requests read stock = 1, then each wrote back a value computed in application code. Fix it with an atomic conditional update (UPDATE … SET stock = stock - 1 WHERE id = ? AND stock >= 1 and check affected rows), a row lock, or a version check.
Longer Answer
Read-modify-write in the application (product.stock -= 1; save()) splits the check and the write. Under load, two transactions read the same value before either writes. Neither Read Committed nor MySQL's Repeatable Read prevents this, because the plain SELECT takes no lock.
Options, in order of preference:
- Atomic conditional update: the database checks and decrements in one statement; zero affected rows means sold out.
SELECT … FOR UPDATEwhen the decision needs several reads.- Optimistic version check (
WHERE version = ?) when conflicts are rare. - Add
CHECK (stock >= 0)as a last line of defence, and load-test with concurrent requests on the same item.
Key Points
- Read-modify-write loses updates
- Atomic conditional UPDATE + affected rows
- Locks or versions for multi-step logic
- CHECK constraint as a safety net
Deep dive: Lost Updates
More most-asked Database Concurrency & Scaling questions
- How do you prevent overselling in a flash sale with 50,000 buyers in the first minute?
- After a deploy, logs show hundreds of 'deadlock detected' errors per hour on checkout. Walk through how you'd investigate.
- Your service throws 'connection pool exhausted' errors but database CPU is only 20%. What's going on?
- Why shouldn't you call a payment API inside a database transaction?
- What's the difference between Read Committed and Repeatable Read in practice?
- Database CPU is at 85%. How do you decide what to optimise before buying a bigger server?
- A user saves their profile and the next page shows the old data. Reads go to replicas. How do you fix it?
- How do you choose a shard key for a multi-tenant SaaS?