Databases · Senior · Question 13 of 75
Many requests update the same counter row (UPDATE stats SET views = views + 1 WHERE id = 1). What's the problem and a fix?
- 1PostgreSQL loses some increments
- ✓Correct answer: All updates serialize on one row lock (a hot row); spread counts over several rows or buffer increments and flush them in batches
- 3The update deadlocks every time
Explanation
Each update waits for the previous one to commit. Sharded counters (N rows summed on read) or counting in Redis with periodic flushes remove the contention.