Video in progress

Staff 🌱 Spring

When does shipping a Spring Boot application as a GraalVM native image pay off?

When very fast startup and low memory matter: serverless, many small services, CLI tools, fast autoscaling. For long-running, high-load services, a JVM with JIT often gives higher peak performance, while a native image adds build complexity and compatibility limits.

📖 Answer

What a native image gives you: startup in tens of milliseconds instead of hundreds, several times less memory (no JIT or metadata outside the heap), and a smaller container image.

The price:

  • The build takes several minutes and a lot of memory (8+ GB), so CI time goes up.
  • Closed world. Loading new classes at runtime, reflection and dynamic proxies have to be declared up front. Spring AOT often does this for you, but not every library is compatible, and for some you write "hints".
  • @Profile and @ConditionalOnProperty are frozen at build time: you can't enable a different profile at runtime and get different beans.
  • Over a long run, the HotSpot JIT usually gives higher throughput. PGO narrows the gap, but it's in Oracle GraalVM.
  • Debugging and profiling tools are limited.
Situation Recommendation
AWS Lambda, Cloud Run, scale-to-zero native
Hundreds of small services, memory is expensive consider native
Large, long-running, CPU-heavy service JVM
A somewhat faster start is enough JVM + CDS or CRaC

Whichever you choose, build the native image in CI regularly and run the tests in native mode too. Otherwise compatibility problems show up on release day.