Quick revision
The short answer
We treat the AOT cache as a build artifact: a training step inside the image build starts each service with a side-effect-free profile, the cache is written into the final image next to the jars, using the very JDK that will run it, and it is rebuilt on every build. The pipeline then proves the cache is accepted at startup, alerts when it is rejected, and measures the startup and warmup gain per service against the extra image size and build time, rolling out first where startup affects scaling and deployments.
What to remember
In this answer, we treated the AOT cache as a build artifact. A training step in the final image stage runs each service with a side-effect-free profile, the cache is written next to the jars with the same JDK that will run it, and it is recreated on every build. A smoke start with AOTMode=on fails the build on a mismatch, startup logs turn rejections into alerts, and per-service measurements of startup, warmup, image size and build time decide where it pays off, starting with services whose startup affects scaling and deployments.
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.
We treat the AOT cache as a build artifact: a training step inside the image build starts each service with a side-effect-free profile, the cache is written into the final image next to the jars, using the very JDK that will run it, and it is rebuilt on every build. The pipeline then proves the cache is accepted at startup, alerts when it is rejected, and measures the startup and warmup gain per service against the extra image size and build time, rolling out first where startup affects scaling and deployments.
Why this question is asked
Adding -XX:AOTCacheOutput to one service on a laptop takes ten minutes. Doing it for two hundred services in a shared pipeline is a different problem, because the cache is fragile in a specific way: it is valid only for the exact JDK and jars it was trained with, and when it is not valid, the JVM quietly ignores it. Interviewers ask this design question to see whether we can turn a JVM feature into a dependable platform capability with checks, measurements and a rollout plan. In this answer, we will recap what the cache depends on, design the training step in the image build, set rules for when to rebuild, add checks that catch a rejected cache, decide how to measure whether it is worth it, and plan the rollout.
1What the Cache Depends On
The AOT cache, introduced by JEP 483 in JDK 24, stores the classes an application loaded and linked during a training run, and from JDK 25 also method profiles for the JIT. At startup the JVM uses it only if the JDK build is the same one that created it and the class path matches the one used in training, with the same jar files. If anything differs, the default behaviour is to log a warning and start without it.
So the cache must be created inside the image build, where the exact runtime is, and because a rejected cache fails silently, the pipeline must check for it explicitly.
2Adding a Training Step to the Image Build
The cleanest place for training is the final stage of the image build, the stage that already contains the runtime JDK and the application files. Training there means the cache is created by exactly the JVM and the exact files that production will use. We first extract the Spring Boot jar, because the cache works with plain jars on the class path, not jars nested inside a fat jar.
- Build and test the jarnormal CI steps
- Extract the jarjarmode tools extract
- Copy files into the runtime imagesame base image as production
- Training runtraining profile, no external side effects
- Cache written next to the jars
app.aotin /app - Smoke start with AOTMode=onfails the build if the cache is rejected
6 steps in order. Play them, or step through them one at a time.
Everything that the cache depends on is fixed before the training run.
Notice the training profile. The build machine must not connect to production databases, send emails or publish Kafka messages, so the training profile points data sources at an embedded or stub database, disables schedulers and consumers, and replaces outbound clients with no-op versions. The aim is to run the same startup code as production without any side effect outside the container.
There is a catch with spring.context.exit=onRefresh: it records context startup, not request handling. For method profiles that shorten warmup, a fuller training run sends a few hundred scripted requests before shutting down, which costs more build time, so it can be an option per service.
3Rebuilding the Cache Whenever Its Inputs Change
The rule is simple: the cache is created in every image build and never copied from an earlier build. It would be tempting to cache it as a build layer to save a minute, but any change to the JDK, to a dependency, or to the application jar makes it invalid. Recreating it every time costs a little build time and removes a whole class of mistakes.
Notice that the JDK can change without anyone touching the service. When the platform team updates the base image for a security patch, every service image is rebuilt, and with this design every cache is rebuilt with it.
| Change | Cache still valid? | What the pipeline does |
|---|---|---|
| JDK patch release in the base image | No | Rebuild image, retrain |
| Dependency version bump | No | Rebuild image, retrain |
| Application code change | No | Rebuild image, retrain |
| Configuration value change only | Usually yes, but retrain anyway | Every build retrains, so no special case |
| Different JVM options in production | Depends on the option | Smoke start uses the production options |
4Proving the Cache Is Actually Used
Because a rejected cache only logs a warning, we need two checks. The first is in the build: after training, start the image once with -XX:AOTMode=on and the production JVM options, and exit after the context starts. With AOTMode=on the JVM refuses to start if it cannot use the cache, so a mismatch fails the build instead of reaching production.
The second check is at run time. Production starts in the default mode, so a cache that becomes unusable, for example because someone added a jar through an environment variable, does not break startup. We enable the class-sharing log at startup (the cds tag on JDK 24, and the aot tag on newer versions; check your version), and the platform's log pipeline turns the message that the cache was rejected into a metric. An alert fires when any service starts without its cache, and a dashboard shows startup time per service, so a rejection is also visible as a jump in startup time.
5Measuring the Gain Against the Cost
The gain differs a lot between services. A large Spring service with many starters may start 30 to 40 percent faster, while a small one that already starts in a second gains little in absolute terms. We measure three numbers per service, with and without the cache: time to ready (from process start to the readiness probe passing), time to steady latency (how long until p99 latency settles after the first requests), and memory at steady state.
On the cost side, the cache adds to the image, often tens of megabytes and sometimes more than 100 MB for large applications, and the training run adds build time, from a few seconds for a context-only run to a minute or two with warmup traffic. Larger images mean slower pulls on new nodes, which can eat part of the startup gain during scale-out. So the decision per service is a small comparison of numbers: seconds saved per start, times the number of starts per day, against megabytes and build minutes.
6Rolling It Out Across Services
We start where startup time hurts. Services that scale out under load benefit most, because every second of startup is a second of overload while new instances come up. Services with long rolling deployments benefit next, because a 20-instance rollout that waits for each instance to be ready gets shorter. Batch jobs that start often are a third group.
The rollout itself goes through the shared build template: a flag turns on the training step for a service, the first five services are chosen from the groups above, and we compare their numbers for two weeks. If the gains hold and no rejections appear, the flag becomes the default for new services, and teams with a reason to opt out record it. Removing the cache only means removing one JVM option, so the change stays reversible.
7Follow-up questions
Why not train once on a build server and copy the cache into many images?
Because the cache is valid only for the JDK build and the exact jars it was trained with. A copied cache would be rejected, or worse, silently ignored, as soon as either differed. Training inside each image build costs some build time but guarantees that the cache matches the image it ships in.
What if the training run fails because a dependency is not reachable?
Then the training profile is not complete yet. The training profile should replace every external dependency with an embedded or stub version, so the run is deterministic. If a service cannot start without a real dependency, we can still train with a smaller set of beans, or skip the cache for that service until the profile is fixed; the build should fail clearly rather than produce a partial cache without anyone noticing.
8Conclusion
In this answer, we treated the AOT cache as a build artifact. A training step in the final image stage runs each service with a side-effect-free profile, the cache is written next to the jars with the same JDK that will run it, and it is recreated on every build. A smoke start with AOTMode=on fails the build on a mismatch, startup logs turn rejections into alerts, and per-service measurements of startup, warmup, image size and build time decide where it pays off, starting with services whose startup affects scaling and deployments.
Open with
I would make the cache part of the shared image build rather than something each team does by hand. The final image stage already has the runtime JDK and the extracted application jars, so that is where the training run goes: start the app with a training profile that replaces databases, brokers and outbound clients with embedded or stub versions, let it start the context or serve some scripted requests, and write the cache next to the jars with AOTCacheOutput on JDK 25.
Then, if they want more
The cache is recreated on every build and never reused, because any change to the JDK or the jars makes it invalid.
Since a mismatch only produces a warning, I would add a smoke start in the build with AOTMode=on, which fails if the cache cannot be used, and in production I would turn the class-sharing log into a metric and alert when a service starts without its cache.
Then I would measure per service: time to ready, time until p99 latency settles, and memory, against the extra image size and build time. I would roll it out first to services that scale out under load or have long rolling deployments, behind a flag in the build template, and make it the default once the numbers hold.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24, Java 25
- 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 "Integrate AOT cache creation into the build and deployment pipeline for container images...": https://scrivio-interview-library.netlify.app/java/startup-and-footprint/4b34730058/