SkillSprout
Chapter 25 · Lesson 1
Intermediate
3 min read

Concurrency, Race Conditions and Locks

Overview

Concurrency means handling many tasks at overlapping times. When those tasks touch the same data, unlucky timing can corrupt it. A race condition is that bug, and a lock is the basic tool that prevents it.

Detailed Notes

What is a race condition

Concurrency is structuring a program so many tasks can be in progress at overlapping times, even on one CPU core that switches between them. Parallelism is literally running tasks at the same instant on several cores. Programs spend most of their time waiting (for disk, network, other services), so concurrency makes much better use of the hardware.

A race condition is a bug where the result depends on the relative timing of concurrent tasks on shared data.

The problem it solves

A concert has one ticket left. Two people click "Buy" at almost the same instant. The code does three steps: read tickets left (1), if greater than 0 sell one, write tickets left = 0.

Both requests read "1" before either writes, both see "available", and the same ticket is sold twice. Nothing crashed and each request alone is correct; only the timing exposed the bug. The classic pattern is read-modify-write: balance is 100, two +50 deposits both read 100, both write 150, and one update is lost (it should be 200).

Another form is check-then-act: check a seat is free, then book it, while someone books it in between.

How it works

A critical section is code that touches shared data and must not run in two places at once. A lock (also called a mutex, for mutual exclusion) lets one task in at a time while the others wait. The second buyer waits, then sees 0 tickets and is correctly refused. Locks have costs:

  • Contention and latency: many tasks waiting on one lock reduce throughput.
  • Deadlock: tasks waiting on each other forever (a later lesson).
  • Forgotten release: a crash while holding a lock can block everyone, so always release safely.

Often better than locks:

  • Atomic operations: a single indivisible step that happens completely or not at all, such as UPDATE tickets SET left = left - 1 WHERE id = 7 AND left > 0. Pushing the check and the change into one database statement is often the simplest and fastest fix.
  • Immutability: data that never changes cannot race (append-only records).
  • Single writer / partitioning: route all operations for one key to the same worker (as in data partitioning), so nothing is shared.

A simple example

A single-stall bathroom with one key: only the key holder goes in and everyone else queues.

System design example

A ticketing site double-sold seats during a flash sale because "is the seat free?" and "book the seat" were two separate steps. The fix was one atomic conditional update: "mark seat 12A as sold only if it is still available". The database lets exactly one request win and the other gets "seat taken".

Summary

  • Concurrency is overlapping progress; race conditions come from unlucky timing on shared data.
  • Fix with atomic operations, locks, single-writer partitioning or immutability.
  • Locks cost throughput, so keep protected sections tiny.

Architecture Diagram

Two buyers, one seat
Architecture

Buyers

Buyer A
Buyer B
App serverlock / atomic update
Databasetickets left: 1

Without protection both buyers succeed; an atomic check lets only one win.

  1. 1Both buyers send a request at nearly the same instant.
  2. 2The lock or atomic update lets one request through first and sells the ticket.
  3. 3The second request sees 0 and is refused.

Common Interview Questions

Finished this lesson?

0 of 91 lessons done · 0% of the course