KatInterview

Diagnosing Latency Spikes in a Thread-Pool Server

System DesignModel answer
MediumModel answerSystem Designperformanceconcurrencyprofiling

Last updated

Investigate why a network server's P99 latency jumps from 10ms to 2s under load despite low CPU usage, and propose a plan to identify the bottleneck.

Context

You're debugging a production server that handles HTTP requests using an event loop with I/O multiplexing (epoll) and a fixed pool of worker threads. At moderate traffic (5,000 req/s), median and P99 latencies are both under 10ms. When traffic climbs to 8,000 req/s, the P99 shoots up to over 2 seconds.

Observations

  • CPU utilization stays around 60%
  • No elevated disk I/O wait
  • Worker threads spend significant time in WAITING state
  • The task queue backing the thread pool grows continuously during peaks
  • The event loop continues to accept connections at full rate

Your Assignment

  1. Root cause analysis: What are the most plausible explanations for the latency spike? Why does the system break down at 8,000 req/s but not at 5,000?

  2. Architecture interaction: Explain how the event loop and worker pool interact to produce this failure mode. Why does a non-blocking event loop still lead to catastrophic queueing?

  3. Diagnostic plan: Outline a monitoring and instrumentation strategy to isolate which component (event loop, queue, worker logic, or external dependency) is causing the delay.

Considerations

  • Some requests may perform blocking operations: synchronous logging with fsync, DNS resolution, or outbound HTTP calls
  • The event loop uses non-blocking sockets and does not block on network I/O
  • Think about what happens when arrival rate exceeds the effective processing capacity of the pool
  • Consider both per-request blocking and shared resource contention (locks, serialized logging, etc.)

Scenario to Reason Through

If 1% of requests trigger a blocking DNS lookup that takes 500ms, how does that impact overall system throughput when the pool has 64 threads? Walk through the math.

Hints

Hint 1

The low CPU usage is a strong signal: workers are waiting on something other than computation.

Hint 2

Apply Little's Law: throughput is bounded by (number of workers) / (effective time per request), including all blocking time.

Hint 3

Look for serialization points like a shared logger or lock that becomes saturated nonlinearly as load increases.