Video in progress
When do you choose a GraalVM native image for Spring Boot microservices, and what price do you pay?
A native image cuts startup from seconds to milliseconds and memory several times over, which is useful for scale-to-zero, serverless, short jobs and dense packing. The price: a long, memory-hungry build, restrictions on reflection and dynamic code, lower peak throughput than the JIT on long-running services, and different debugging.
📖 Answer
Spring Boot 3 officially supports native images: at build time Spring AOT precomputes the bean definitions, and GraalVM compiles the whole application to machine code under the "closed world" assumption.
./mvnw -Pnative spring-boot:build-image
# or locally: ./mvnw -Pnative native:compile
A typical result: startup around 100 ms instead of 2–4 seconds, and several times less memory. The numbers depend on the application, so measure them yourself.
When it pays off:
- scale-to-zero (Knative, serverless): every cold start is visible to the user;
- CLIs and short jobs: the JVM never gets to warm up;
- hundreds of small, lightly loaded services: the memory savings turn into money.
The cost:
- Build: several minutes and several GB of memory. A native build on every PR is expensive, so it's often done only in the release pipeline, and then native-specific errors show up late.
- Closed world: if reflection, dynamic proxies and resources aren't declared up front, you get an error at runtime. Spring and popular libraries provide hints, but with old or less common libraries there will be problems. Profiles and
@ConditionalOnPropertyare frozen at build time, so using one image with a different set of beans in different environments is limited. - Throughput: in a long-running, heavily loaded service the HotSpot JIT optimizes with a real profile; a native image (especially without PGO) may be slower at peak.
- Operations: APM agents based on
-javaagentdon't work, and heap dumps and profiling are different.
Alternatives: the AOT cache in Java 24+ (JEP 483) and CDS speed up startup noticeably and keep all of the JVM's capabilities; CRaC snapshots the warmed-up state. For many organizations this is "most of the benefit without native".
How I decide: first the problem has to be in numbers: "cold start p99 is 3 seconds and it hurts the business", or "memory is 40% of the cluster cost". Then a pilot on one service and a list of libraries that don't fit native. Moving everything to native at once is rarely the right call.
Sign up to mark studied questions and save your progress.