Atomic State Transitions and Idempotent Retries
Interleave withdrawals, reconcile wallet and ledger state, and scope retry records to actor, operation, and request fingerprint.
You’re reading a preview. Premium includes the full lesson and specialist practice. View the full curriculum.
Judge committed state, not two success responses
A race condition occurs when overlapping operations produce a result that depends on an unsafe interleaving. In a wallet API, each request can validate a positive amount and check the balance correctly against the value it read, yet both requests can spend the same available funds.
Our WalletDesk fixture starts with 100 units. Two withdrawals of 80 read the same balance before either writes. Each creates a ledger entry and saves a new balance of 20. The final wallet says 20 while the ledger records 160 withdrawn. The flaw is a stale read and lost update across related state.
A reads balance 100
B reads balance 100
A checks 100 >= 80 → true
B checks 100 >= 80 → true
A creates withdrawal 80 and saves balance 20
B creates withdrawal 80 and saves balance 20
Final: balance 20; two withdrawal records totaling 160
The example proves inconsistent wallet and ledger state. If no external payout code is shown, do not claim two bank transfers definitely settled. Trace queued or external effects separately.