Video in progress

At night one service's p99 latency went up and within a few minutes other services stopped responding too. What could have happened, and how do you prevent it?

A classic cascading failure: a slow downstream ties up the callers' thread and connection pools, they slow down too, and it spreads up the chain. Defenses: short timeouts, bulkheads, circuit breakers, bounded queues and load shedding.

📖 Answer

It is convenient to start from Little's law: busy threads = arrival rate × response time. The service gets 100 requests per second and usually answers in 50 ms, so 5 threads are busy. The downstream slows to 5 seconds, and now you need 500 threads. Tomcat has 200. The queue grows, the health check waits in the queue too, Kubernetes restarts pods, and the load on the remaining pods goes up. The same thing then starts in whatever calls this service.

My order of checks:

  1. In tracing, which span got slower: usually a single downstream or DB query.
  2. A thread dump (jcmd <pid> Thread.print): if hundreds of threads are stuck on the same socket read, you have your answer.
  3. Connection pool metrics: hikaricp.connections.pending, the number of waiters in the HTTP client pool.

Prevention, layer by layer:

  • Timeouts on every external call, based on p99 and fitted to the chain's budget.
  • Bulkheads per downstream: if the reports service slows down, it takes only its own 20 slots and there's still room for payments.
  • A circuit breaker with slow call rate: it also treats a slow service as "dead" and rejects fast.
  • A fallback: a cached value or a reduced response, where possible.
  • A bounded queue: an unbounded queue only delays the problem.
  • Separate probes: liveness shouldn't check dependencies, otherwise restarts make the problem worse.

Java 21 virtual threads make the first step easier, since threads don't run out. But the problem doesn't disappear, it moves to the next limit: DB connections, memory, the downstream itself. So you still need to set the limits deliberately.