System Design Academy

Saving…

Lesson 8 of 10 in Module 40 of 45 lessons complete

Deadlocks

Module 4, lesson 8Concept12 min

Definition

Deadlock

In plain English
A deadlock is when goroutines wait for each other in a cycle, so none can ever continue.
Think of it like
It is like two cars on a narrow bridge, each waiting for the other to reverse.

Why it exists

One branch holds lock A and waits for B. Another holds B and waits for A.

Neither can finish. Go can report a global deadlock, but some stuck requests may remain hidden inside a running server.

How it works

Coffman conditions

In plain English
The Coffman conditions are four situations that must all hold for a deadlock: exclusive resources, holding while waiting, no forced taking, and a circular wait.
Think of it like
Like four ingredients that together make the bridge jam.

Lock ordering

In plain English
Lock ordering means every goroutine acquires multiple locks in the same agreed order.
Think of it like
Like a rule that everyone crosses the bridge north to south.

Goroutine 1 holds A and waits for B; Goroutine 2 holds B and waits for A.

Opposite lock orders create a waiting cycle.

Goroutine 1 holds A and waits for B; Goroutine 2 holds B and waits for A.

ConditionMeaningPossible change
Exclusive resourcesOne holder at a timeRemove unnecessary shared ownership
Hold while waitingKeep A while requesting BRestructure to avoid overlapping holds
No forced takingCannot seize a held resourceUse cooperative release when the design permits
Circular waitA waits for B, B for AUse one global lock order
Before (deadlocks): each transfer holds its first lock before taking the other.
Go
package main

import (
	"sync"
)

func main() {
	var a, b sync.Mutex
	var ready, finished sync.WaitGroup
	ready.Add(2)
	finished.Add(2)
	transfer := func(first, second *sync.Mutex) {
		defer finished.Done()
		first.Lock()
		defer first.Unlock()
		ready.Done()
		ready.Wait()
		second.Lock()
		defer second.Unlock()
	}
	go transfer(&a, &b)
	go transfer(&b, &a)
	finished.Wait()
}
After: distinct branch IDs determine one lock order for both transfers.
Go
package main

import (
	"fmt"
	"sync"
)

type Branch struct {
	id int
	mu sync.Mutex
}

func transfer(a, b *Branch) {
	if a == b {
		return
	}
	if a.id > b.id {
		a, b = b, a
	}
	a.mu.Lock()
	defer a.mu.Unlock()
	b.mu.Lock()
	defer b.mu.Unlock()
	fmt.Println("transfer", a.id, b.id)
}
func main() {
	a, b := &Branch{id: 1}, &Branch{id: 2}
	var wg sync.WaitGroup
	wg.Add(2)
	go func() { defer wg.Done(); transfer(a, b) }()
	go func() { defer wg.Done(); transfer(b, a) }()
	wg.Wait()
}
  • An unbuffered send blocks until a receiver participates.
  • A WaitGroup with missing Done calls can wait forever.
  • A timeout stops waiting only where code checks it; Mutex.Lock does not accept cancellation.
  • An unmatched channel operation can also block forever without two locks; the runtime can report that as a deadlock.

Examples

Order branch locks

Two transfers run in opposite directions. Their locks must still follow one global order.

Before (deadlocks): each transfer holds its first lock before taking the other.
Go
package main

import (
	"sync"
)

func main() {
	var a, b sync.Mutex
	var ready, finished sync.WaitGroup
	ready.Add(2)
	finished.Add(2)
	transfer := func(first, second *sync.Mutex) {
		defer finished.Done()
		first.Lock()
		defer first.Unlock()
		ready.Done()
		ready.Wait()
		second.Lock()
		defer second.Unlock()
	}
	go transfer(&a, &b)
	go transfer(&b, &a)
	finished.Wait()
}
After: distinct branch IDs determine one lock order for both transfers.
Go
package main

import (
	"fmt"
	"sync"
)

type Branch struct {
	id int
	mu sync.Mutex
}

func transfer(a, b *Branch) {
	if a == b {
		return
	}
	if a.id > b.id {
		a, b = b, a
	}
	a.mu.Lock()
	defer a.mu.Unlock()
	b.mu.Lock()
	defer b.mu.Unlock()
	fmt.Println("transfer", a.id, b.id)
}
func main() {
	a, b := &Branch{id: 1}, &Branch{id: 2}
	var wg sync.WaitGroup
	wg.Add(2)
	go func() { defer wg.Done(); transfer(a, b) }()
	go func() { defer wg.Done(); transfer(b, a) }()
	wg.Wait()
}

The Before program waits until both first locks are held, making the cycle reproducible. Use unique, stable branch IDs.

The insight

Consistent lock order breaks circular waiting.

Trade-offs

Pros

  • One lock order prevents lock cycles.
  • Broader locks simplify related updates.

Cons

  • Every caller must follow the order.
  • Broader locks reduce overlapping work.

Use when

  • An operation needs several locks.
  • Shared updates must remain consistent.

Avoid when

  • Separate ownership removes shared locks.
  • A single goroutine can perform the whole operation.

Quick check

Two goroutines lock A then B and B then A. Simplest fix?
What does "fatal error: all goroutines are asleep - deadlock!" mean?

Takeaways

  • Deadlock prevents progress through waiting dependencies.
  • Acquire multiple locks in one agreed order.
  • Start matching channel operations before waiting for them.

Try the quick check above whenever you feel ready.

Previous: Producer–Consumer