Lesson 9 of 9 · 23m
Concurrent Systems in Production
How to observe contention, capture useful thread state, debug stalls and starvation, and operate concurrent services without guessing.
Production concurrency failures rarely announce themselves as concurrency failures. They look like elevated p99 latency, a queue growing for no obvious reason, CPU pinned with little useful work, or a process that appears alive but stops processing. The difference between guessing and debugging is instrumentation built before the incident.
Measure queueing, not just work
A worker can be fast while the system is slow because work waits before reaching it. Track at least these signals per executor or queue:
- queue depth, oldest-item age, enqueues, dequeues, rejections, and drops
- lock wait time, executor starvation, and active versus configured workers
- task runtime histograms, not just averages The most important one is oldest-item age. A queue depth of 10,000 with fast turnover may be fine; a depth of 20 whose oldest item is 45 seconds old means the system is already missing deadlines.
Thread dumps are the first forensic artifact
When a JVM service stalls:
jcmd 18423 Thread.print > threads.txtLook for repeated stacks, monitor ownership, and waits. One thread inside a synchronized method while everyone else waits to enter is contention. Threads waiting on each other in a cycle is deadlock. A single runnable thread doing a huge allocation or serialization inside the critical section is a critical-section sizing bug.
For native Linux processes, gdb can capture all stacks:
gdb -p 18423 -batch -ex 'thread apply all bt' -ex quit > native-stacks.txtTake more than one sample thirty seconds apart. A static snapshot can mislead; repeated identical waiting stacks are stronger evidence.
CPU hot is not the same as making progress
Profile before blaming the scheduler. On Linux:
perf top -p 18423
perf record -p 18423 -g -- sleep 20
perf reportIn contended systems, the profile often shows spin loops, atomic retry loops, futex syscalls, or allocator contention rather than application logic. In oversubscribed systems, the kernel spends time scheduling and migrating threads. Both are symptoms of architecture, not reasons to blindly increase worker count.
Async runtimes fail differently
In async systems there may be only a handful of OS threads. Blocking one of them starves thousands of tasks. Symptoms include all latency rising together while CPU remains low. The fix is to move blocking work to a dedicated pool:
let rows = tokio::task::spawn_blocking(move || {
blocking_database_query(&connection)
}).await?;The same rule applies in Python asyncio, Node.js, .NET, and every event-loop runtime: never perform blocking I/O, expensive CPU work, or lock-heavy critical sections on the event loop.
Operate with backpressure and shutdown contracts
A production concurrent system needs explicit answers to four questions:
- What happens when the input queue is full?
- What cancellation signal stops in-flight work?
- How long may shutdown wait before abandoning work?
- Can work be executed twice, and is downstream processing idempotent?
If those answers are absent, concurrency bugs eventually become availability bugs. Timeouts without cancellation merely move work out of sight while it continues consuming resources.
Field notes
- Emit thread pool names and queue names in metrics. "executor latency" is useless when a process has six pools.
- Use per-request deadlines and propagate cancellation into workers; otherwise retries amplify load behind the scenes.
- During an incident, capture evidence before restarting: thread dumps, queue metrics, heap state, and recent deploys.
- Beware synchronized deploy behavior, and ship every concurrency fix with a metric or assertion that would have caught the original failure earlier.
Was this lesson helpful?