← Lessons

quiz vs the machine

Platinum1780

System Design

Lock Contention Reduction

Why threads queueing on a shared lock kills scaling and how to spread the load.

5 min read · advanced · beat Platinum to climb

When threads wait on each other

A lock protects shared state so only one thread enters a critical section at a time. Under load, many threads queue for the same lock, and that waiting, called contention, stops extra cores from helping.

Symptoms

  • CPU is idle yet throughput is flat because threads block.
  • Latency rises sharply as concurrency grows.
  • A profile shows time parked waiting to acquire a lock.

Reducing contention

  • Shard the lock so different keys map to different locks, spreading the load.
  • Shrink the critical section by doing slow work outside the lock.
  • Read write locks let many readers proceed together when writes are rare.
  • Lock free or atomic updates remove the lock for simple counters.
  • Per thread state that merges later avoids sharing at all.

Beware false sharing

Even without a lock, two threads writing nearby memory on the same cache line stall each other. Pad hot fields so they sit on separate lines.

Key idea

Contention turns more cores into more waiting, so shard locks, shrink critical sections, and prefer read mostly or lock free paths to scale.

Check yourself

Answer to earn rating on the learn ladder.

1. What is a classic symptom of lock contention?

2. How does sharding a lock reduce contention?