Interview practice question
Stock count goes wrong under load
Backend Engineering · Relational Databases · Intermediate
Your code reads a product's stock, subtracts the ordered quantity in application code and writes it back. Under load, stock counts become wrong. Why, and how do you fix it?
What a strong answer covers
Your AI report card counts how many of these ideas you cover.
- Lost update race condition.
- Atomic update: SET stock = stock - n WHERE stock >= n.
- Or SELECT FOR UPDATE.
- Or optimistic locking with version.
- Check affected rows.
Practise this topic
Start without signing up. Sign in free to get an AI report card on your answer.