(Note · · 5 min read)
It works until two people use it at once
Read the current value, decide, write the new one. It is the most natural way to write a feature and it is wrong whenever two requests do it at the same moment. Testing never catches it, because testing is one person clicking one thing, and production is not.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
The gap between reading and writing
Between the read and the write, another request can do the same read. Both see one seat remaining, both conclude there is room, both write a booking. The code looks correct when read line by line, which is why these bugs survive review.
The symptoms are familiar: a last item sold twice, a balance that drifts, two accounts created with the same address, a promotional code redeemed more often than it exists. All the same shape.
Let the database decide
The reliable fix is to stop deciding in application code and let a constraint arbitrate. A unique constraint on an email column settles two simultaneous signups, with one receiving an error you can translate. No amount of checking first achieves the same, because the check and the insert are separate moments.
For counters, make the write itself conditional rather than computing the new value in your process: decrement where the remaining count is greater than zero, and treat zero rows affected as sold out. The condition and the change happen together, which is exactly what was missing.
-- racy: another request can book between these
SELECT units FROM inventory WHERE id = $1; -- 1 left
UPDATE inventory SET units = units - 1 WHERE id = $1;
-- safe: the condition is part of the write
UPDATE inventory SET units = units - 1
WHERE id = $1 AND units > 0;
-- 0 rows affected = sold outLock the row when a decision needs several steps
Where the work genuinely involves reading, computing and writing across several statements, take a row lock as part of the read inside a transaction, so other requests wait rather than interleave.
Keep what happens inside that transaction short, and never call an external service from within it. Holding a row lock while waiting on a payment provider turns one slow network call into every other request queueing behind it.
Beware the pattern that spans a user's thinking time
A form loaded at ten o'clock and submitted at ten past has a ten-minute gap in which someone else changed the record. The submission then overwrites their change with values computed from stale data, and nobody sees an error.
Send a version with the form and reject the write if the stored version has moved on, telling the user the record changed rather than silently discarding someone's work.
Where to look in an existing system
- Anything that checks availability or a limit before acting: stock, seats, quotas, promotional codes.
- Anything that reads a balance and writes a new one, rather than applying a delta.
- Anything creating a record only if one does not already exist.
- Any handler that can receive the same event twice, including webhooks and job retries.
- Any edit form whose submission overwrites a whole record.
Common questions
Why does my feature break only under load?
Because it reads a value, decides, then writes — and between the read and the write another request can read the same value. Both proceed as if they were alone. The code reads as correct line by line, which is why review rarely catches it.
How do I stop overselling the last item?
Make the condition part of the write: decrement where the remaining count is greater than zero and treat zero rows affected as sold out. Checking availability first cannot work, because the check and the update are separate moments.
When should I lock a row?
When the decision genuinely needs several statements. Take the lock as part of the read inside a transaction so others wait instead of interleaving, keep the transaction short, and never call an external service while holding it.
How do I stop one user's edit overwriting another's?
Send a version with the form and reject the write if the stored version has changed, telling the user the record moved. Otherwise a submission computed from data loaded minutes ago silently discards whatever happened in between.