Quick revision
The short answer
The first requests are slow because the JVM is still doing one-time work: loading and linking classes on first use, running code in the interpreter until the JIT compiles the hot methods, and initialising caches, connection pools and lazy Spring beans. To warm up safely, we keep the instance out of the load balancer until a warmup phase has run representative, side-effect-free requests against the service itself, and only then report ready. CDS and the AOT cache cut the class loading part of that cost.
What to remember
In this answer, we saw that the first requests are slow because classes load on first use, code runs interpreted before the JIT compiles it, and applications defer work into connection pools, caches and lazy beans. We gated readiness on a warmup phase that sends representative dry-run requests through the real HTTP stack, kept warmup free of side effects and close to real traffic so the profiles match, and used CDS or the AOT cache to cut the class loading part of startup.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The first requests are slow because the JVM is still doing one-time work: loading and linking classes on first use, running code in the interpreter until the JIT compiles the hot methods, and initialising caches, connection pools and lazy Spring beans. To warm up safely, we keep the instance out of the load balancer until a warmup phase has run representative, side-effect-free requests against the service itself, and only then report ready. CDS and the AOT cache cut the class loading part of that cost.
Why this question is asked
Almost every Java team meets this the first time they autoscale or deploy under load: the new instance answers its first requests in hundreds of milliseconds while the old ones take ten. Users see timeouts, and dashboards show a latency spike after every deploy. Interviewers ask about it to see whether we know what the JVM does during its first minutes and how to keep that cost away from users. In this answer, we will go through the sources of first-request slowness, see how readiness gating works, write a safe warmup, look at what CDS and the AOT cache add, and discuss what goes wrong when warmup is not representative.
1A Quick Recap of What Happens on First Use
Java loads classes lazily. A class is read from its JAR, parsed, verified, linked and initialised the first time code needs it, not when the application starts. A Spring Boot service may load 15,000 classes or more, and many of them are loaded only when the first request takes a particular path: a JSON serialiser for a specific type, a validation class, a database driver feature.
Then there is the JIT. Every method starts in the interpreter, which is much slower than compiled code. Only after a method has been called enough times does C1 compile it, and later C2 compiles the hottest methods with full optimisation. Until that happens for the request path, each request runs largely interpreted.
2The Application's Own First-Use Costs
On top of the JVM, applications defer work. Connection pools such as HikariCP may open connections lazily, so the first queries wait for TCP and TLS handshakes and database authentication. Caches start empty, so the first requests go to the database or a remote service. Spring beans marked @Lazy, or the whole context with lazy initialisation enabled, are created on first use. Frameworks build reflection metadata, compile templates or create JSON serialisers the first time a type is seen. Each of these is cheap once but adds up on the first requests.
| Source | First request | Later requests |
|---|---|---|
| Class loading and linking | tens of ms | none |
| Interpreted code before JIT | 5-20x slower | compiled code |
| Connection pool and TLS setup | tens to hundreds of ms | reused connections |
| Empty caches | remote calls | cache hits |
| Lazy beans and serialisers | created on demand | already built |
3Gating Readiness on Warmup
Kubernetes and most load balancers distinguish between an instance that is alive and one that is ready for traffic. The readiness probe tells the platform when to start routing requests. The idea is simple: the instance reports not ready until it has warmed itself up. Spring Boot exposes its readiness state through Actuator, at /actuator/health/readiness, and an application can change that state.
Notice that the warmup goes through the real HTTP stack, not just the service methods, so the web server, JSON mapping, filters and security code are all loaded and compiled too. The number of iterations should be enough to get the hot methods to C2; we find it by measuring when latency stops falling, which is often a few thousand requests per main endpoint. In Kubernetes, the readiness probe must allow enough time for this, so its timing settings or a startup probe have to be set accordingly.
4Keeping Warmup Safe
Warmup requests run real code, so they must not have real effects. No orders placed, no emails sent, no payments, no messages published to other services. Common ways to ensure this are a dry-run parameter that the code honours at the boundary where effects happen, warmup-specific test accounts and data that downstream systems ignore, or read-only endpoints that exercise the same code paths. The safest designs make the side-effecting call itself check the flag, so a new code path cannot forget it.
Warmup requests should also resemble real traffic: the same endpoints, similar payload sizes, a realistic mix of types and options. The JIT optimises for what it sees, so warmup is also training the profile.
- Application context startedreadiness:
REFUSING_TRAFFIC - Run representative dry-run requestsreal HTTP path, no side effects
- Latency stable at steady state?Yesmark readyNokeep warming, within a time limit
- Readiness: ACCEPTING_TRAFFICload balancer starts routing
4 steps in order. Play them, or step through them one at a time.
5What CDS and the AOT Cache Add
Class Data Sharing stores classes in an archive in a pre-parsed form, so the JVM can map them in instead of reading and parsing them from JARs at startup. Spring Boot supports creating a CDS archive with a training run of the application. Project Leyden's AOT cache extends this: since JDK 24 it also stores classes in a loaded and linked state, and since JDK 25 it can include method profiles, so the JIT starts with profile data from the training run. These reduce the class loading part and part of the JIT warmup, which shortens the warmup phase, but they do not warm application caches or connection pools, so readiness gating is still useful.
6Follow-up questions
What can go wrong if warmup traffic exercises different paths from real traffic?
The JIT optimises for the profile it sees. If warmup only calls one endpoint with one kind of payload, C2 compiles code that assumes that shape: specific receiver types inlined, other branches left as uncommon traps. When real traffic arrives with a different mix, those assumptions fail, the JVM deoptimises and recompiles, and the latency spike we tried to avoid happens anyway, just a little later. Unrepresentative warmup can also leave some classes and caches cold, and in the worst case produce a profile that is worse than no warmup.
Is it enough to call the service methods directly instead of over HTTP?
It warms the business code but misses the web layer: the server's request handling, filters, security, JSON mapping and error handlers. Those are a large part of a request's code path, so calling through the real HTTP endpoint on localhost gives a much more complete warmup.
How long should warmup take?
Long enough that latency of the warmup requests reaches the steady-state level, which we measure. We also cap it with a time limit, so a slow dependency cannot keep an instance unready forever, and we track time to ready as a metric.
7Conclusion
In this answer, we saw that the first requests are slow because classes load on first use, code runs interpreted before the JIT compiles it, and applications defer work into connection pools, caches and lazy beans. We gated readiness on a warmup phase that sends representative dry-run requests through the real HTTP stack, kept warmup free of side effects and close to real traffic so the profiles match, and used CDS or the AOT cache to cut the class loading part of startup.
Open with
The first requests pay for work that only happens once. Java loads classes lazily, so the first request on each path loads, verifies and links many classes. All code starts in the interpreter, and only after enough calls does C1 compile it and later C2, so early requests run mostly interpreted.
Then, if they want more
And the application itself defers work: connection pools open connections on demand, caches start empty, lazy Spring beans and JSON serialisers are created on first use. To warm up safely, I keep the instance out of the load balancer until it is warm.
In Spring Boot I would publish REFUSING_TRAFFIC for readiness, run a warmup phase that calls the service's own endpoints over HTTP on localhost, a few thousand representative requests, and then publish ACCEPTING_TRAFFIC.
The requests must have no side effects, so a dry-run mode or test accounts that never place orders or send emails, and they should look like real traffic, because the JIT optimises for what it sees; otherwise real traffic triggers deoptimisation later. I would cap warmup with a time limit and track time to ready.
CDS or the AOT cache from Project Leyden reduce the class loading cost, and from JDK 25 the cache can also carry profiles.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Java 24
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Why are the first requests after a Java service starts slower than later ones, and how...": https://scrivio-interview-library.netlify.app/java/jit-compilation/d981159668/