hard20 pts · first answer countstransactionsconcurrencysql
Lost update on a wallet balance
Two requests run this code at the same time for the same wallet with balance 100, each adding 50. Sometimes the final balance is 150 instead of 200. Which fix is correct?
const { balance } = await db.one("SELECT balance FROM wallets WHERE id = $1", [id])
await db.query("UPDATE wallets SET balance = $1 WHERE id = $2", [balance + 50, id])Discussion 0
Join free to post in the discussion.
Related questions
- Stopping SQL injectionBackend Developer
- Purpose of a database indexBackend Developer
- Rows returned by a LEFT JOINBackend Developer
- What a foreign key enforcesBackend Developer
- Atomicity in a money transferBackend Developer
- COUNT and AVG with NULLsBackend Developer
- Filtering grouped resultsBackend Developer
- Duplicate emails despite a checkBackend Developer