Video in progress

Staff 🌱 Spring

How do you choose between CDS, AOT and CRaC to reduce Spring Boot startup time and memory on the JVM?

CDS (Class Data Sharing) is the easiest and safest: startup gets noticeably shorter and you don't have to change any code. Spring AOT gives a bit more speed on the JVM, but limits the flexibility of profiles and conditions. CRaC gives the fastest startup, but requires handling connections and secrets across snapshot and restore, and the platform has to support it.

📖 Answer

CDS / AppCDS (Java 21; in Java 24+ the AOT cache, JEP 483). The metadata of loaded classes is written to an archive and isn't re-parsed on the next start. It's convenient in Boot 3.3+: java -Djarmode=tools -jar app.jar extract, then you create the archive once with a "training run". Startup is usually 20-40% faster, and it's nearly risk-free.

Spring AOT (in JVM mode) turns bean definitions into code at build time, so there's less reflection and scanning at startup. The limitation is that conditions and profiles are resolved at build time.

With CRaC (Coordinated Restore at Checkpoint), once the application has started and "warmed up", the process state is written to disk. Restore takes tens of milliseconds and the JIT is already hot too. But:

  • open sockets, files and DB connections have to be closed before the checkpoint and reopened afterward (Spring 6.1+ handles most of this for you);
  • the snapshot contains everything in memory, secrets included, so store it like a secret;
  • you need a special JDK (Azul, Liberica CRaC) and Linux.

I usually go in this order: first I measure where the time goes (often it turns out to be your own @PostConstruct or network calls). Then I turn on CDS, a cheap win. If autoscaling or serverless needs drastically faster startup, I prototype CRaC or a native image and weigh the operational complexity.