Concurrency
Concurrent financial operations can both act on an old balance.
What it is
Concurrent financial operations can both act on an old balance.
When to use it
Use atomic conditions or engine-appropriate locks for shared state.
Syntax
UPDATE accounts SET balance_cents = balance_cents - ?
WHERE id = ? AND balance_cents >= ?;Example
$changed = DB::table("accounts")->where("id", $sourceId)
->where("balance_cents", ">=", $amountCents)
->decrement("balance_cents", $amountCents);
if ($changed !== 1) throw new InsufficientFunds();
// Within a transaction covering related writes.Common mistakes
Never use an unprotected read-check-write; verify behavior using the target database engine.