Optimistic locking
Optimistic locking updates only if a version has not changed.
What it is
Optimistic locking updates only if a version has not changed.
When to use it
Use when conflicts are uncommon and retries are acceptable.
Syntax
UPDATE accounts SET balance_cents = ?, version = version + 1
WHERE id = ? AND version = ?;Example
$changed = DB::table("accounts")->where("id", $account->id)
->where("version", $account->version)
->update(["balance_cents" => $newBalance, "version" => $account->version + 1]);
if ($changed !== 1) throw new RuntimeException("Concurrent modification");Common mistakes
All relevant writers must update/check the version; retries need fresh state and revalidation.