Skip to content
RED SAGSkills
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])
Choose one answer