Glossary

Race Condition

A race condition is a bug that occurs when the result depends on the order of execution of concurrent operations. Classic example: two requests simultaneously read a balance ($100), both withdraw $80, both write $20 — but the correct result should be -$60.

In databases

Solved with transactions at the appropriate isolation level, or SELECT ... FOR UPDATE — locking the row until the end of the transaction.

In caches

Atomic Redis operations (INCR, SETNX, Lua scripts) prevent race conditions at the cache level.

Optimistic vs pessimistic locking