Video in progress

Your Java service takes 40 seconds to start, which is too slow for autoscaling. How do you choose among CDS, AOT cache, CRaC and GraalVM native image?

First I measure where the time goes: class loading, the Spring context, DB connections or JIT warm-up. I start with the cheap options (AppCDS/AOT cache, lazy init, turning off needless auto-configuration); I take CRaC and native image only if their operational complexity pays off.

📖 Answer

What the 40 seconds consist of matters. In Spring Boot, ApplicationStartup (BufferingApplicationStartup) and the actuator's /startup endpoint show how long each bean took. -Xlog:class+load and JFR fill in what is happening at startup. The finding is often unexpected: Flyway checking migrations, a synchronous call to an external service, Hibernate's schema validation.

The options, from cheap to expensive:

AppCDS / AOT cache. In JDK 13+, -XX:ArchiveClassesAtExit=app.jsa, then -XX:SharedArchiveFile=app.jsa. The AOT cache in JDK 24 (JEP 483) stores classes in a loaded and linked state. No code changes, and a 20-40% speedup is realistic for Spring Boot applications. The downside: the archive is for exactly this JDK and classpath, so you need to add it to the build step in CI.

At the application level. spring.main.lazy-initialization (carefully: errors move to the first request), removing needless starters, Spring AOT.

CRaC. Checkpoint a warmed-up JVM (with its JIT code) and restore it from that in milliseconds. Spring Boot 3.2+ supports it. But: only on some JDK distributions and on Linux; open sockets and files have to be closed before the checkpoint; and secrets (keys, tokens) stay inside the checkpoint, which is a security issue.

GraalVM native image. Startup around 100 ms, low memory. But: the build is long and resource-hungry; reflection and dynamic proxies need metadata; some libraries don't work; peak throughput is often below the JIT (PGO gets it closer); and diagnostic tools are limited (JFR partially, heap dumps work differently).

For autoscaling I usually start with AppCDS + application cleanup + a minimum number of replicas that are already warm. I consider native image for serverless or for small services that scale up and down very often.