Video in progress

In prod the service has "frozen": CPU is low, requests time out, the logs are silent. How do you diagnose it?

This almost always means the threads are waiting for something: a lock, a connection pool, an external call or a full queue. I take a thread dump 2–3 times a few seconds apart, compare them, and see where the threads are stuck.

📖 Answer

CPU is low and nothing is being written, so the application isn't working, it's waiting. Before restarting you need to collect evidence, otherwise the problem comes back and its cause stays unknown.

Taking a thread dump:

jcmd <pid> Thread.print > dump1.txt; sleep 5; jcmd <pid> Thread.print > dump2.txt
# or through Actuator (if the endpoint is exposed and protected)
curl -s localhost:8081/actuator/threaddump -H 'Accept: text/plain' > dump.txt

In Kubernetes: kubectl exec <pod> -- jcmd 1 Thread.print.

What I look for in the dump:

  • all the http-nio-8080-exec-* threads are on the same stack. For example, TIMED_WAITING in HikariPool.getConnection means the pool is exhausted. Then I look for who is holding the connections: usually an external HTTP call inside a single long transaction.
  • BLOCKED ... waiting to lock <0x...>: I find the thread holding that lock. A classic deadlock is reported by the JVM itself at the end of the dump as "Found one Java-level deadlock".
  • threads stuck in SocketInputStream.read: an HTTP client with no read timeout.
  • if the stacks didn't change between dumps, it's really frozen; if they changed, it's just slow.

Confirmation from metrics: hikaricp_connections_pending, tomcat_threads_busy_threads, jvm_threads_states_threads{state="blocked"}.

Be careful with a heap dump: on a big heap, heapdump causes a stop-the-world pause of tens of seconds and takes disk space equal to the heap size. Take it only if you suspect a memory problem, and after pulling the pod out of traffic.

To find it faster next time: have a timeout on every external call, keep the pool wait time short (connectionTimeout) and alert on pool metrics. Then instead of a "silent freeze" you see a clear error in the log.