Skip to main content
Module 11 · Lesson 2 of 442/57 in the course~14 min
Module lessons (2/4)

Race Conditions

Lesson preview5 hands-on exercises readySQL Course · Module 11 — Transactions and Concurrency

Understand read-modify-write anomalies and lost updates under concurrent transactions.

What you'll learn in this lesson

  • Recognize the Lost Update concurrency bug caused by the "read → compute to RAM → overwrite" application pattern.
  • Eliminate lost updates for counters and balances with SQL atomic updates (col = col - 1) + guards WHERE col > 0.
  • Distinguish the three classic anomalies: Dirty Read, Non-Repeatable Read and Phantom Read.

Overview

When a single terminal writes to the DB everything seems simple; but in production dozens of Node.js, Go, or Python workers invoke queries in the same millisecond. If you move calculations out of the database (read balance → subtract in memory → write new number), you generate race conditions and silent data losses (Lost Update).

Advance the Unsafe scenario step by step between T1 and T2: observe how the separate reading from the calculation (SELECT stock followed by UPDATE SET stock = 0) causes the same item to be sold twice and brings the stock to -1!

Sections covered in this lesson

  1. 11. Mental Model: Reread in SQL vs Calculate in App
  2. 22. Progressive & Annotated Examples
  3. 33. Interactive Lab: View the Concurrent Lost Update
  4. 44. Diagnostic Verification (Quiz)
  5. 55. Progressive Practical Laboratory
Pro Lesson · Module 11 — Transactions and Concurrency

Unlock "Race Conditions" and the full SQL Course curriculum

Understand read-modify-write anomalies and lost updates under concurrent transactions.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in SQL Course plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.