The Desi Architect
Notes / Docker Sandboxes / Race Conditions & Locks: Your First Concurrency Bug
Docker Sandboxes · Tier 0: Start Here · Lab 08

Race Conditions & Locks: Your First Concurrency Bug

Make a race condition happen on purpose with a seat counter, fix it with a lock, then see why a lock inside one program does not protect two programs. This is the idea behind every database lock, Redis lock and fencing token that follows.

Time30 minutes
StackPython, runs in a containerPorts None (runs inside a container)
MemoryAbout 30 MB
Before thisLab 00Docker Basics
Verified runWindows, Mac, LinuxBash and PowerShell
  1. 1PrepareWhat you will learn
  2. 2PredictGuess before you run
  3. 3RunStep by step
  4. 4ObserveWhat you should see
  5. 5Break itChange one thing
  6. 6Clean upLeave nothing running
On this page
  1. 00 / What You Will Learn
  2. 01 / Why It Matters
  3. 02 / What We Are Building
  4. 03 / Words You Need
  5. 04 / Predict First
  6. 05 / Step by Step
  7. 06 / What You Should See
  8. 07 / Look Inside
  9. 08 / Break It, Fix It
  10. 09 / In Real Projects
  11. 10 / Interview Corner
  12. 11 / Clean Up

00 /What You Will Learn

By the end you will be able to
  • Explain a race condition in one sentence, with a bus-seat example.
  • Reproduce a lost update and a check-then-act bug on purpose.
  • Fix both with a lock, and say what the fix costs.
  • Explain why a lock inside one program does not protect other programs.
  • Say why servers on different machines need a lock that lives outside the programs.

Before you start

You need Docker Desktop running. You need to be able to read a few lines of simple Python. The code runs inside a container, so you install nothing on your laptop.