Measuring Lock Contention06/25/2026
Scaling multi-threaded applications is often limited by the proportion of the workload that cannot be parallelized. This concept is captured by Amdahl's law. Code sections guarded by locks are a common example — they restrict scalability by allowing only one thread (or n threads in the case of semaphores) to execute, forcing other threads to wait due to lock contention.
Context
In an HTTP/2 server, requests and responses on a connection are multiplexed. The server needs to write concurrently computed responses to a network stream. Such multiplexing can be achieved in multiple ways:
- Using a Channel in a producer-consumer pattern
- Guarding writes with locks
- Implementing custom scheduling solutions with BlockingCollection or Queues