Skip to content

Lesson 10 of 13 · 15m

Lock Contention Eats Parallelism

Contended locks serialize work — measure wait and hold time, then shrink the critical section instead of adding threads.

A contended mutex converts a parallel system into a serial one with extra steps. One thread holds the critical section; everyone else queues in a futex. Add threads and you do not add throughput — you add queue depth, and the convoy gets slower because the holder now wakes a longer line each round. This is why "scale it horizontally" fails inside a process: contention is a serialization problem, not a capacity problem.

The arithmetic

Amdahl's law again: if 20% of the work is serialized by a lock, no thread count gets you past 5× — and convoy effects mean you do not reach the bound. Hold time is the lever, not lock count. A mutex held across a socket write(), a vsnprintf into a log buffer, or an RPC turns every millisecond of that I/O into serialized time for every waiter. The fix is almost always "do less under the lock," not "a faster lock": precompute outside, batch acquisitions, shard the structure, or switch readers to RCU and copy-on-write.

The measurement trap is that contention is invisible in on-CPU profiles — waiting threads are not running, so they leave no samples. A service "using all its cores" under a convoy is usually showing the lock-holder working while everyone else sleeps. Utilization is the wrong scoreboard; completed requests are the scoreboard. Watch throughput and latency percentiles while changing pool size — the curve flattens or inverts exactly at the contention point, and that knee is your parallelism budget.

The convoy and the cache line

The wake path is what makes contention nonlinear. A futex queue wakes roughly in order; the newly-woken thread runs, acquires, works, releases, wakes the next — one serialized round-trip per waiter, plus a cold-cache penalty when it lands on a different core. Condition variables make it worse: pthread_cond_broadcast wakes everyone, and the thundering herd re-serializes immediately on the mutex guarding the predicate.

The subtler sibling is false sharing: two threads writing different variables in the same 64-byte cache line bounce that line between cores on every store. Nothing ever "waits" — throughput just craters. perf c2c reports the contested lines when scaling goes negative with zero lock traffic.

Measuring it

perf lock contention (perf 6.2+ kernels) aggregates lock waits by lock address with stacks — which mutex, how long waited, how often. For threads blocked on anything — mutexes, condition variables, I/O — offcputime-bpfcc emits the blocked stacks as folded output for a flame graph; contended locks appear as wide futex_wait towers. Go has a mutex profile built in: runtime.SetMutexProfileFraction(1) and then /debug/pprof/mutex reports contention delay per call site. Java's jcmd <pid> Thread.print plus JFR lock events do the same job there.

Evidence

The JVM removed an entire optimization over this mechanism. Biased locking — which skipped atomics on the assumption a lock had one owner — was deprecated in JDK 15 (JEP 374) and removed in JDK 18, because maintaining it cost more than modern uncontended-lock paths saved, and contended paths paid the futex regardless. InnoDB's kernel_mutex wars across the MySQL 5.x era taught the same lesson at larger scale: years of release notes are contention surgery — splitting the mutex, shrinking what it covers. And glibc malloc's per-thread arenas exist for this reason alone: the central heap lock was the bottleneck under threads.

The practice

  • Measure wait time and hold time, not lock count — perf lock contention or offcputime before any redesign.
  • Move work out of the critical section: compute first, lock, swap the values, unlock.
  • Shard or use RCU/copy-on-write for read-mostly structures; reader locks are still locks.
  • More threads under contention means more queue — cap the pool at useful parallelism and fix the section.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.