Quick revision
The short answer
Per-service jlink runtimes make each image much smaller and remove modules an attacker could use, but with thousands of containers a shared JDK base image is often cheaper overall, because its layer is pulled once per node and reused by every service. Per-service runtimes also have to be rebuilt for every JDK patch and can fail at run time when a needed module was left out. The balanced choice for most fleets is a small set of curated jlink base images, each shared by a group of similar services.
What to remember
In this answer, we saw that jlink builds a runtime with only the modules a service needs, which can cut the image from a few hundred megabytes to well under one hundred and reduce the attack surface. At fleet scale, though, a shared base image is cached once per node and patched once per quarter, while per-service runtimes multiply both the layers on each node and the pipelines that must run for every JDK patch, and they add a real risk of run-time failures from missing modules. A few curated jlink base images, tested for TLS and locales and rebuilt centrally, give most of the benefit at a fraction of the cost.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Per-service jlink runtimes make each image much smaller and remove modules an attacker could use, but with thousands of containers a shared JDK base image is often cheaper overall, because its layer is pulled once per node and reused by every service. Per-service runtimes also have to be rebuilt for every JDK patch and can fail at run time when a needed module was left out. The balanced choice for most fleets is a small set of curated jlink base images, each shared by a group of similar services.
Why this question is asked
Image size sounds like a simple win, so this question looks easy until we think about how container images are stored, pulled and patched across a large fleet. Interviewers ask it at senior level because the right answer depends on caching, patch logistics and failure modes, not only on megabytes. In this answer, we will recap what jlink does, compare the two approaches on size, transfer, patching and risk, look at how a missing module shows up in production, and then design a middle option that keeps most of the benefits.
1A Quick Recap of jlink and Runtime Images
Since Java 9 the JDK itself is split into modules, such as java.base, java.sql, java.naming and jdk.crypto.ec. A module is a named group of packages that declares which other modules it needs. Because the JDK knows these dependencies, a tool can assemble a runtime that contains only the modules an application really uses.
That tool is jlink. We give it a list of root modules, it adds everything they require, and it writes out a runtime image: a folder with a bin/java launcher and a trimmed copy of the JDK libraries. The jdeps tool can tell us which modules an application needs, so the two are usually used together.
Notice that the size drop is large: a full JDK image is a few hundred megabytes, while a runtime for a typical service is often 50 to 80 MB. Fewer modules also means fewer tools and libraries inside the container that an attacker could use, and fewer components for a security scanner to report. The exact flags for --compress differ between releases, so check your version.
2Why the Shared Image Often Wins at Fleet Scale
A container image is made of layers, and a node stores and pulls each layer only once, no matter how many images use it. If 300 services all start FROM the same JDK 21 base image, a node that runs 40 of them downloads the 200 MB JDK layer once and then only the small application layers on top. The registry also stores that layer once.
With per-service jlink runtimes, every service has its own runtime layer, because each one contains a slightly different set of modules. Forty services on a node now means forty runtime layers of 60 MB each, which is 2.4 GB against the 200 MB of the single shared layer. So the per-image saving can turn into a per-node loss. jlink only wins on transfer when services rarely share nodes or when cold pulls on fresh nodes dominate.
| Concern | Per-service jlink | Shared JDK base image |
|---|---|---|
| Size of one image | Small, often 50 to 80 MB runtime | Large, a full JDK of a few hundred MB |
| Bytes pulled on a busy node | One runtime layer per service | One JDK layer for all services |
| Attack surface | Only the modules used | Every module and tool in the JDK |
| JDK security patch | Rebuild and redeploy every service | Rebuild one base image, then rebuild apps |
| Missing module risk | Real; found at run time | None |
| Debugging tools inside | Only if jdk.jcmd etc. were added | jcmd, jstack and jfr available |
3The Patching Cost Nobody Sees in the Size Chart
The JDK gets security updates every quarter, in January, April, July and October. With a shared base image, the platform team rebuilds one image, and every service picks it up on its next build. With per-service runtimes, the JDK is baked into each service's own build, so every one of the thousands of pipelines must run again, and every team must verify and deploy. A patch that should take days can take weeks, and the slowest team decides when the fleet is safe.
We can reduce this by doing the jlink step in a shared build stage and pinning the JDK version centrally, but the fact remains: each runtime is a separate artifact that must be rebuilt and tracked. Scanners also need to recognise the JDK version inside a custom runtime, which some do less well than for a standard base image.
4How a Missing Module Fails in Production
The most dangerous part of per-service runtimes is that a missing module rarely fails at startup. jdeps only sees classes that the code refers to directly. Modules that are loaded through a service lookup or by name, such as security providers, locale data and naming providers, do not show up, so the runtime starts fine and fails later on a specific code path.
Notice that neither failure is a ClassNotFoundException. The locale case silently falls back to a default format, and the crypto case appears as a TLS error on the first call to one particular partner. That is why every service with a custom runtime needs integration tests that exercise TLS, locales, JNDI and JMX before the image goes out, and why many teams add jdk.crypto.ec (on older releases), jdk.localedata, jdk.zipfs and jdk.charsets to every runtime as a safe default. On recent JDKs the elliptic curve provider moved into java.base, so the exact module list changes between releases.
5The Middle Option: A Few Curated jlink Base Images
Instead of one runtime per service, the platform team publishes two or three runtimes built with jlink, each a base image. For example, a server image with java.base, java.sql, java.naming, java.management, jdk.crypto.ec, jdk.localedata, jdk.unsupported and jdk.jfr covers almost every Spring Boot service; a small image for batch tools; and the full JDK for the rare service that needs more. Each image is shared, so node caching still works, and each is patched once per quarter by one team.
The result is most of the size and attack surface gain, perhaps 100 MB instead of 300 MB, with the cache and patching behaviour of a shared image. Teams with a real need, such as a function that cold starts thousands of times a day, can still build their own runtime, but they own its patching and testing.
- Does a curated server image cover its modules?YesUse the shared curated imageNoCheck the next question
- Is image size critical, such as fast cold start?YesBuild a per-service runtime and own patchingNoUse the full JDK base image
- Add integration tests for TLS and localesRun on every runtime change
- Rebuild the base images each quarterOne pipeline per image, not per service
4 steps in order. Play them, or step through them one at a time.
6Follow-up questions
How do you keep debugging possible in a trimmed runtime?
A runtime built with jlink only has the tools whose modules were added. If we add jdk.jcmd and jdk.jfr, we can still take thread dumps and start a flight recording inside the container. Many teams keep these modules in the curated image because the size cost is small and the value during an incident is high. The alternative is an ephemeral debug container with a full JDK attached to the same process namespace.
Does jlink work for applications that are not modular?
Yes. jlink only needs modules for the runtime it builds, which are the JDK's own modules. The application stays on the class path as before. We use jdeps on the jar and its libraries to choose the JDK modules, then run java -cp app.jar with the custom runtime. Only if we want the application itself inside the image as a module do we need module-info files.
7Conclusion
In this answer, we saw that jlink builds a runtime with only the modules a service needs, which can cut the image from a few hundred megabytes to well under one hundred and reduce the attack surface. At fleet scale, though, a shared base image is cached once per node and patched once per quarter, while per-service runtimes multiply both the layers on each node and the pipelines that must run for every JDK patch, and they add a real risk of run-time failures from missing modules. A few curated jlink base images, tested for TLS and locales and rebuilt centrally, give most of the benefit at a fraction of the cost.
Open with
I would start by separating image size from what we actually pay for. jlink can shrink a runtime from roughly 300 MB to 60 or 80 MB and removes modules we do not use, which is good for attack surface and cold starts.
Then, if they want more
But with thousands of containers, a shared JDK base image is pulled once per node and stored once in the registry, so forty services on a node share one layer. Per-service runtimes are each different, so a node may actually pull more bytes in total.
The bigger cost is patching: every quarterly JDK update would mean rebuilding and redeploying every service, and the slowest team decides when we are safe.
There is also a real failure mode, because jdeps cannot see modules loaded by name, so a runtime without jdk.crypto.ec on older JDKs or without jdk.localedata starts fine and then fails on a TLS handshake or formats currency wrongly.
So my recommendation is a middle path: the platform team publishes two or three curated jlink base images, for example a server image and a small batch image, each with a safe set of modules including crypto, locale data and jcmd. They are shared, cached and patched once, with integration tests for TLS and locales.
A team that truly needs its own runtime can build one, but it then owns the patching.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 9
- 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
- The Java Language Specification, Java SE 21, Java SE 21 API 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 "Your organisation runs thousands of containers. Evaluate building per-service jlink...": https://scrivio-interview-library.netlify.app/java/modules/4f7aff6abe/