Count page hits without lost increments
An article-view counter hit by 50 parallel requests at once. If you do «read value → increment in code → write», parallel requests see the same starting number and a chunk of increments is lost. The fix is to perform the increment in a single update statement that derives the new value from the old.
Increase n by one for the row with id 1 in counters in a single UPDATE. Run the command five times — the final value should grow by exactly five. Postgres serializes per-row updates automatically.
Sign in to see submission history
Sign inSign in to use AI Mentor
Sign in