System Design Academy

Saving…

Lesson 2 of 10 in Module 40 of 45 lessons complete

Race Conditions & Critical Sections

Module 4, lesson 2Concept12 min

Definition

Race condition

In plain English
A race condition is a bug where the result depends on the unpredictable timing of concurrent tasks.
Think of it like
It is like two people booking the last hotel room on different laptops at the same second.

Data race

In plain English
A data race is when two goroutines access the same variable at the same time and at least one is writing, without coordination.
Think of it like
It is like two people writing on the same line of a whiteboard at once.

Critical section

In plain English
A critical section is a piece of code that must not be run by more than one goroutine at a time.
Think of it like
It is like a single-person changing room.

Why it exists

A vote counter should reach 1,000. Under load, some increments disappear.

count++ reads, adds and writes. Two goroutines can read 41 and both write 42, losing one vote.

How it works

Race detector

In plain English
The race detector is a Go tool, enabled with -race, that reports data races while your program or tests run.
Think of it like
Like a smoke alarm for timing bugs.

G1 and G2 each read 41 from Count, then both write 42.

Two increments can produce only one increase.

G1 and G2 each read 41 from Count, then both write 42.

Before (has a data race): 1,000 unprotected vote increments.
Go
package main

import (
	"fmt"
	"sync"
)

func main() {
	count := 0
	var wg sync.WaitGroup
	for i := 0; i < 1000; i++ {
		wg.Add(1)
		go func() { defer wg.Done(); count++ }()
	}
	wg.Wait()
	fmt.Println(count)
}
Trimmed diagnostic from go run -race; locations depend on your file.
Text
WARNING: DATA RACE
Read at ...
Previous write at ...
  • A check followed by a change also needs coordination.
  • A data race does not require two instructions to execute at the exact same instant.
  • The detector observes executed paths; a clean run cannot prove every path safe.

Examples

Find missing votes

Many viewers vote together. An unprotected total can lose votes.

Before (has a data race): 1,000 unprotected vote increments.
Go
package main

import (
	"fmt"
	"sync"
)

func main() {
	count := 0
	var wg sync.WaitGroup
	for i := 0; i < 1000; i++ {
		wg.Add(1)
		go func() { defer wg.Done(); count++ }()
	}
	wg.Wait()
	fmt.Println(count)
}

The printed count varies and may sometimes be correct. A correct count does not make the program race-free.

The insight

Shared read-modify-write operations need coordination.

Trade-offs

Pros

  • Direct access avoids copying large data.
  • Shared data can reduce duplicate storage.

Cons

  • Timing changes can corrupt results.
  • Concurrent map writes can crash.

Use when

  • Several tasks truly need one value.
  • Every conflicting access has protection.

Avoid when

  • Each task can own its input.
  • Passing results avoids shared writes.

Quick check

Why can count++ from many goroutines lose updates?
How do you find data races in Go?

Takeaways

  • Race conditions make results depend on timing.
  • Protect both read-modify-write and check-then-act operations.
  • Run programs and tests with -race.

Try the quick check above whenever you feel ready.

Previous: Processes, Threads & Goroutines