DEV Community

María Arias de Reyna
María Arias de Reyna

Posted on Edited on Originally published at delawen.com

Practical Guide to AOT Cache

This is an article based on my experience helping a few teams adapt their pipelines to introduce the AOT Cache. This means that I am going to give a few pointers, but it is impossible to cover all potential use-cases.

If you don't know what the AOT Cache is, please read first one of my previous articles.

Quarkus Smooth Pipeline

We have been working closely with the Quarkus team to make the experience of using an AOT cache as transparent and easy as possible.

If you are already using Quarkus, congratulations! You should read their Ahead-of-time (AOT) guide to make sure you are using the best practices. The Quarkus team has already been working on making their framework code as compatible and efficient as possible.

For example, Quarkus allows you to generate an AOT cache based on your integration tests. If you have good testing hygiene and your integration tests are covering your most relevant features, the cache created by Quarkus itself could be just what you need. No need to add any canary deployment. No need to add a complex CI/CD pipeline that deploys your app and run some workload.

How to use the AOT Cache

The usage is simple:

# First run the training
java -XX:AOTCacheOutput=route/to/app.aot -jar app.jar

# Then you can use the generated file to run production
java -XX:AOTCache=route/to/app.aot -jar app.jar
Enter fullscreen mode Exit fullscreen mode

As the AOT Cache is based on predictability (because there's no point in caching something that is going to change) there are a few requirements for this to work:

  • The training and production run must have the same source code. This includes the source code of the JVM, which means, the JVM version must match.
  • The training and production must run over the same operative system. This should be obvious from the previous point, as the JVM code is different for each operative system.
  • The training and production must run over the same hardware. There's no point in optimizing over one configuration and then changing it.

Let's digg a bit into what the last constraint means:

Different hardware specs in training and production

There is a temptation on generating the code cache in your development laptop or your CI/CD pipeline and then ship it everywhere. But the hardware used will probably be different. It may be the same hardware family, but the processor, and therefore the instructions available, may differ.

Check if you are using the exact same configuration both in training and production. If you do, you can skip this section. If you don't, keep reading.

Random hardware in the cloud

It may be even worse than a mismatch between your CI/CD pipeline and your production hardware.

Some providers in the cloud offer variable architecture. They warrant a specific type of architecture, like arm or x86. But on every instance spin we may get a completely different hardware specification.

This is a problem when caching compiled code. You may discover that your compiled code uses some set of instructions that is not available on your current hardware.

How to fix it

The solution is to force the training run to compile to the “lower” hardware you are going to be offered.

For example, you can use the following arguments during your training (these are just examples, research which ones adapt to your use case):

-XX:UseSSE=version Enables the use of SSE instruction set of a specified version.

-XX:UseAVX=version Enables the use of AVX instruction set of a specified version.

The bad news is that we won’t get a code compiled with all the potentially available instructions that may make it faster. But the performance gains are still noticeable.

If you have this issue, you should also check the new argument UseAOTCacheCpuFeaturesOnly which is already available in JDK 28. This tells your production run to only use whatever cpu features are available on your cache.

Iterative Training

One of the most overlooked steps when using the AOT Cache on existing pipelines is how to create the cache with a reliable load.

The preferred option is to use a canary deployment, so you can have real world data that generates the cache on the very first deployment of each version of your application. But not all pipelines have canary deployments. And even if you are lucky enough to be able to generate the cache with integration tests, they may not cover every single aspect of a real world load.

Since JDK28 you can now improve an already created cache from a previous run (JDK-8353598).

# Train the framework or skip this if you are using Quarkus integration-tests
java -XX:AOTCacheOutput=first.aot -cp FrameWork.jar FrameWorkTraningHelper

# Train the app maybe using a canary deployment
java -XX:AOTCache=first.aot -XX:AOTCacheOutput=second.aot -cp FrameWork.jar:App.jar MyApp

# Production run using second.aot, which contains artifacts from the first and second training runs
java -XX:AOTCache=second.aot -cp FrameWork.jar:App.jar MyApp
Enter fullscreen mode Exit fullscreen mode

This brings several additional features, like extending the integration tests cache using real world data that gets improved during a production run. Or you can train further parts of the application that are not usually hot during the training run, but could be a potential bottleneck.

Agents

This has been proven one of the most painful points on deploying using the AOT Cache.

If you are using agents in your deployment, you should take a look at the Notes on Agents vs AOT Cache to understand how to properly use it.

Agents can modify the code that runs in your application dynamically (modify bytecode before it is parsed or classes after they have been loaded). But the AOT cache expects predictability. The Leyden Project is based on caching those things that we know are going to happen the same way in all runs, so we can skip doing them. If we start introducing unpredictable outcomes, we can’t rely on what has been cached before.

The compromise we have reached is that any class (and subtypes) modified by an Agent is excluded from the AOT cache. That way we don’t cache something that may or may not be modified by the agent.

Best practices

Excluding classes from the cache may seem harsh, but as a developer you just have to make sure classes that are potentially being modified by your agent are trivial.

Imagine you are developing a REST API. You have an agent that adds some telemetry on each request and modify the entrypoint methods to add that telemetry. On this case you may want to move all your functionality code to another class that is not going to be modified by the agent. Leave in the class modified by the agent just a call to your functionality class. Even if your entrypoint class is not cached because it is tainted by an agent, it won’t matter much for your performance, because that class will be trivial and quickly compiled on the fly.

What if my agent is not compatible?

Not everything depends on you as a developer, the agent may also be problematic. For example, if your agent modifies some basic class (like java.lang.Object), that means no class can be cached, because all classes inherit from it.

Note that native agents aren't supported yet as explained in JDK-8387194.

It may be that your agent is not compatible. On this case, we suggest you contact your agent provider and encourage them to work towards making their agent compatible. Feel free to tell them to reach out to me for help or contact the team on the leyden-dev mailing list.

Some agents like the OpenTelemetry one are compatible. Looking for a replacement for your use case is also an option.

How and when to add the agent

In the AOT training run, do not use agents, but add modules needed for agents. We add those modules and the agent on the production run.

# training run
$ java --add-modules=java.instrument -XX:AOTCacheOutput=app.aot -cp app.jar MyApp

# production run -- with a Java agent
$ java --add-modules=java.instrument -XX:AOTCache=app.aot -cp app.jar -javaagent:agent.jar MyApp

# production run -- without any Java agents
$ java --add-modules=java.instrument -XX:AOTCache=app.aot -cp app.jar MyApp
Enter fullscreen mode Exit fullscreen mode

Quality of the Cache

You can check the quality of your AOT Cache with the Java AOT Cache Diagnostics Tool. I will soon write more articles regarding this tool.

My problem isn't covered here!

If you need help adding the AOT Cache into your services, please, don’t hesitate and reach out to me or contact the team on the leyden-dev mailing list. I am interested in experiences and what problems real world teams are having when using the AOT Cache.

Top comments (0)