Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

You need to reduce container image size and startup time for 30 Spring Boot services. What options do you have and what do they cost?

We can shrink images with a minimal JRE base, a custom runtime built with jlink, or a distroless or slim base, and speed startup with class data sharing, which Spring Boot 3.3 and later supports from an extracted jar, or the JDK's AOT cache on JDK 24 and later. GraalVM native images give the smallest footprint and the fastest startup but cost long builds and compatibility work. Each option trades build complexity, debugging ease or compatibility for its gain, so we measure image pull time and startup in the real cluster before choosing.

Why this question is asked

Image size and startup time decide how fast a service scales out under load, how quickly a node replacement recovers, and how much registry and network traffic each deployment generates. With 30 services, a few hundred megabytes per image and 15 seconds of startup add up. Interviewers ask this design question to see whether we know the options and, more importantly, what each one costs. In this answer, we will see where size and startup time come from, then go through base images, jlink, distroless, class data sharing and AOT caches, and native images, and finish with how to choose by measuring.

1Where Size and Startup Time Come From

A typical image built on a full JDK base is 400 to 500 MB: the operating system packages, the JDK with its compiler and tools, and the application's dependencies, often 60 to 100 MB of jars. Of that, only the JVM's runtime modules and our dependencies are really needed.

Startup time for a Spring Boot service is usually 5 to 20 seconds. The JVM loads and verifies thousands of classes, Spring scans and creates beans, Hibernate builds its metamodel, and the JIT compiler warms up. Pulling the image on a fresh node can add more time than startup itself if the image is large and the registry is far away.

2Smaller Base Images: JRE, jlink, Distroless

The first step is a JRE base instead of a JDK: we do not need javac or jshell in production, and that alone often saves around 150 MB. A step further is jlink, a JDK tool that builds a custom runtime containing only the Java modules the application uses. jdeps helps find the list, although Spring applications need some modules that static analysis misses, so we test carefully.

Text
# Build stage: a runtime with only the needed modules
FROM eclipse-temurin:25-jdk AS jre
RUN jlink --add-modules java.base,java.logging,java.naming,java.sql,java.management,\
java.instrument,java.security.jgss,jdk.unsupported,java.desktop \
--strip-debug --no-man-pages --no-header-files --compress=zip-6 --output /jre
# /jre: about 70 MB instead of about 200 MB for a full JRE
FROM debian:bookworm-slim
COPY --from=jre /jre /opt/jre
# ... copy the extracted application layers, then:
ENTRYPOINT ["/opt/jre/bin/java", "-jar", "/app/app.jar"]

Distroless or minimal base images go further on the operating system side. They contain only what is needed to run a program, with no shell and no package manager. That reduces size and, more importantly, the number of packages a vulnerability scanner can flag. The cost is debugging: we cannot exec into the container and run a shell, so we rely on kubectl debug with an ephemeral debug container, jcmd from a sidecar, or good logs and metrics.

3Faster Startup: CDS and the AOT Cache

Class data sharing (CDS) saves the JVM's parsed and verified form of classes in an archive file, so the next start can map it instead of loading and verifying each class again. Spring Boot 3.3 and later documents a training run: extract the jar, start the application once with -XX:ArchiveClassesAtExit and -Dspring.context.exit=onRefresh, which stops right after the context is ready, and then run with -XX:SharedArchiveFile. Startup typically drops by 30 to 40 percent.

JDK 24 introduced the AOT cache (JEP 483), which goes further: it stores classes already loaded and linked, and JDK 25 made creating it a single step with -XX:AOTCacheOutput. Startup gains of around 40 percent or more are reported for Spring applications, and combining it with Spring's own AOT processing helps more. Both need the same JDK and the same classpath at training and at runtime, so the archive is built inside the image build.

Properties
# In the Dockerfile, after extracting the jar to /app (Boot 3.3+ tools mode, without --layers)
RUN java -XX:AOTCacheOutput=app.aot -Dspring.context.exit=onRefresh -jar /app/app.jar # JDK 25
ENTRYPOINT ["java", "-XX:AOTCache=app.aot", "-jar", "/app/app.jar"]
# startup: 8.1 s -> about 4.5 s (measure your own service)
# JDK 21 alternative with CDS:
# RUN java -XX:ArchiveClassesAtExit=app.jsa -Dspring.context.exit=onRefresh -jar /app/app.jar
# ENTRYPOINT ["java", "-XX:SharedArchiveFile=app.jsa", "-jar", "/app/app.jar"]

The cost is a few seconds or tens of seconds of extra build time and a slightly larger image, since the archive is stored in it. The training run starts the application context, so beans that connect to databases at startup need care, for example lazy connections or a profile for the training run.

4Native Images: Fastest, With a Price

A GraalVM native image compiles the whole application ahead of time into one executable. Spring Boot supports it through its AOT processing and the native build tools. A native Spring Boot service can start in under 0.2 seconds and use a fraction of the memory, and the image can be 80 to 120 MB on a minimal base.

The price is significant. A native build takes several minutes and several gigabytes of memory per service, so 30 services stress the CI. Libraries that rely on reflection, dynamic proxies or resources loaded by name need hints, and some do not work. Peak throughput can be lower without the JIT unless profile-guided optimization is used. And we must test the native build, not only the JVM build, because behaviour can differ.

5Choosing by Measuring

The right choice depends on what we are optimizing. If the problem is slow scaling during traffic spikes, startup matters most. If it is node replacement and deployment time, pull time and image size matter. So before committing, I would take two or three representative services, build each variant, and measure in the real cluster: image size, pull time on a cold node, time until the readiness probe passes, memory at steady state, and p99 latency under load.

Options for 30 Spring Boot services
OptionGainCost
JRE base instead of JDKabout 150 MB smallernone worth mentioning
jlink custom runtimeruntime about 70 MBmodule list to maintain and test
Distroless or slim basesmaller, fewer CVEsno shell for debugging
CDS archive (Boot 3.3+)startup 30 to 40 percent fastertraining run in build
AOT cache (JDK 24+)startup about 40 percent or more fastersame JDK and classpath required
GraalVM native imagestartup under 1 s, low memorylong builds, hints, lower peak throughput
Measure pull time and readiness time in the real cluster before choosing.

For most fleets, I would apply a JRE or distroless base and CDS or the AOT cache everywhere, since they are cheap and safe, and reserve native images for the few services where scale-to-zero or burst scaling makes their cost worthwhile.

6Follow-up questions

Does a smaller image really speed up deployment?

It speeds up pulls on nodes that do not have the layers yet, such as new nodes from the autoscaler. On nodes that already have the base layers, only changed layers are pulled, so layering matters as much as total size. Measuring cold-node pull time shows the real effect.

How do we debug a distroless container?

With kubectl debug, which adds an ephemeral container with tools into the running pod and can share the process namespace. For the JVM itself, jcmd from such a container, JFR recordings written to a volume, and good metrics cover most needs.

Can we combine CDS or the AOT cache with Buildpacks?

Yes. Recent Paketo buildpacks for Java can perform the training run and include the archive when an environment variable such as BP_JVM_CDS_ENABLED is set. The variable names depend on the buildpack version, so check your version.

7Conclusion

In this answer, we went through the options to reduce image size and startup time for Spring Boot services: JRE and jlink runtimes and distroless or slim bases for size, class data sharing and the JDK's AOT cache for startup, and GraalVM native images for the largest gains at the highest cost. We saw what each one costs in build time, debugging and compatibility, and that the right choice comes from measuring pull time, readiness time and memory in the real cluster.

What to say in the interviewAbout 90 seconds aloud

Open with

I would split it into size and startup. For size, the first easy step is a JRE base instead of a JDK, which saves around 150 MB. Further, jlink can build a custom runtime with only the modules we need, around 70 MB, but someone has to maintain the module list and test it.

Then, if they want more

Distroless or slim bases remove the shell and package manager, which shrinks the image and the CVE count but makes debugging harder, so we rely on kubectl debug and good telemetry.

For startup, class data sharing is the cheap win: Spring Boot 3.3 and later supports a training run on the extracted jar with spring.context.exit set to onRefresh, and startup typically drops 30 to 40 percent. On JDK 24 and later, the AOT cache goes further, and on JDK 25 it is one flag in the build.

Native images with GraalVM give sub-second startup and low memory, but builds take minutes and gigabytes, reflection-heavy libraries need hints, and peak throughput can be lower. So I would measure on two or three representative services in the real cluster: image size, cold-node pull time, time to readiness and memory.

Then I would roll out JRE or distroless plus CDS or AOT cache to everyone, and keep native images for the few services that scale to zero or burst.

About this answer

Last updated
12 November 2026
Versions it names
Java 24, Java 25, Spring Boot 3.3, Java 21
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Kubernetes documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "You need to reduce container image size and startup time for 30 Spring Boot services....": https://scrivio-interview-library.netlify.app/spring-boot/containers-and-kubernetes/518a7e02e1/