Video in progress

A Java service is slow for the first requests after startup and gets faster after a few minutes. Why?

This is JIT warm-up: at first the bytecode is interpreted, the JVM watches which methods are called often, and then compiles them to native code. On top of that, classes load, caches fill and connection pools fill.

📖 Answer

The JVM does not turn code into machine code right away. The interpreter runs first and keeps a counter for every method and every loop. When a method gets "hot", the JIT compiles it and optimizes it based on the profile collected along the way (which branch runs most, which implementation gets called).

So in the first minute:

  • interpreted code can be 10-50 times slower than JIT-compiled code;
  • classes load on first use (Spring, Hibernate, Jackson: thousands of classes);
  • the Hikari pool, Hibernate's query plan cache and Jackson's serializers are empty.

On prod this shows up as a p99 latency spike after a deploy. What you can do:

  • make the readiness probe report "ready" only after warm-up, so traffic arrives once the instance is warm, not immediately;
  • a warm-up script that calls the main endpoints a few hundred times at startup;
  • for startup speed: AppCDS, the AOT cache in Java 24+, or CRaC/GraalVM native image.

To check, -Xlog:jit+compilation=debug or, simpler, -XX:+PrintCompilation shows which method was compiled and when.