Software engineering: make the second request safe

Lesson 1 of 3

Races, injection, and what “fixed” means

8 min

A race condition is a bug that only shows up because two operations overlap. Both read the old balance, both write their own result, and one write silently wins.

Deadlock is different: everyone is stuck waiting. A memory leak is a resource that is never released. Schema drift is a column that no longer matches the code. Naming the failure is the first debugging move, because each one has a different fix.

SQL injection is a structure problem, not a spelling problem. If you build a query by gluing the user’s text into the SQL, the user can change the query. Stripping quotes is a guessing game. The fix is to send the SQL shape and the user’s value separately: a parameterized query.

Example

Checkout reads stock = 1 for two shoppers. Both see 1, both sell the last item. The fix is not “click slower.” It is a conditional update: `update items set stock = stock - 1 where id = $1 and stock > 0`, and treat zero updated rows as “sold out.” The parameter `$1` is also how the id stays data instead of SQL.

Practice

In one sentence, what is the difference between a race and a deadlock?