Check Then Charge

Two withdrawal requests for $80 hit the same $100 wallet at the same moment. Each handler reads the balance, sees $100, decides $80 is affordable, and writes the debit. The wallet lands at -$60. What actually fixes this?

A

Keep a cached copy of the balance in Redis and check that copy before writing, since the cached copy reflects the newest debit before the row does

B

Put the predicate in the write: UPDATE wallets SET balance = balance - 80 WHERE id = ? AND balance >= 80, and only proceed when a row is returned

C

Wrap the read and the write in a single database transaction so the pair of statements commits atomically and the second handler cannot see a stale balance

D

Retry the whole handler with exponential backoff so the losing request re-reads a corrected balance on its second pass through the handler