Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

Certificates for your services now rotate every few days through an internal CA. Java services keep failing after rotation until they restart. How do you make TLS reload work?

Java services usually build their SSLContext, with its key manager and trust manager, once at startup, so they keep presenting and trusting the old material in memory after the files on disk are rotated. To make rotation work, we either use a key manager that reloads from disk, or rebuild the SSLContext when the files change; Spring Boot SSL bundles can do this for the embedded server when reload-on-update is set (check your version for supported servers). We also trust the CA rather than leaf certificates, so trust stores do not change at rotation, and we monitor expiry and handshake failures.

Why this question is asked

Short-lived certificates are a good security practice: a stolen key is only useful for a few days. But they turn certificate rotation from a yearly event into a routine one, and every piece of the system has to cope with it. Interviewers ask this scenario to see whether we understand where Java keeps TLS material and how to change it in a running process. In this answer, we will recap what an SSLContext holds, see exactly why the services fail after rotation, look at reloading on the server side with Spring Boot SSL bundles and on the client side with a reloading key manager, and finish with trust and monitoring.

1A Quick Recap: What an SSLContext Holds

In Java, everything a TLS connection needs comes from an SSLContext. It holds a key manager, which supplies our own certificate and private key when we must prove who we are (always on a server, and on a client using mutual TLS), and a trust manager, which decides whether to trust the certificate the other side presents. Both are created from key store files: a key store with our certificate and key, and a trust store with the CA certificates we trust.

The important detail is when the files are read. KeyManagerFactory.init() and TrustManagerFactory.init() load the key stores into memory, and the SSLContext built from them keeps those objects for its whole life. The JVM never looks at the files again. An embedded Tomcat, a Netty server or a java.net.http.HttpClient each take an SSLContext when they start and use it for every handshake after that.

2Why the Services Fail After Rotation

Now follow a rotation. The internal CA issues a new certificate, an agent writes it to /etc/tls/tls.crt and tls.key, and the old certificate expires two days later. The process still holds the old certificate in memory, so its server keeps presenting a certificate that has expired, and every client rejects the handshake with a certificate expired error. On the client side, a service using mutual TLS keeps sending its old client certificate, and the servers refuse it. Restarting fixes everything, because the new process reads the new files at startup, which is exactly the pattern in the question.

Step throughRotation without reload
Internal CADiskService JVMClient
write new cert and key
still holds old cert from startup
TLS handshake
presents old cert
reject: certificate expired
restart reads new files

6 messages in order. Play them, or step through them one at a time.

The new files sit on disk unused until the process restarts.

So the root cause is not the rotation; it is that the files and the memory have drifted apart. The fix is to give the process a way to notice new files and swap in new key material without restarting.

3Server Side: Spring Boot SSL Bundles With Reload

Spring Boot 3.1 introduced SSL bundles, named groups of TLS material defined in configuration, and Boot 3.2 added the option to reload a bundle when its files change. The embedded server picks up the new certificate for new handshakes without a restart.

YAML
spring:
ssl:
bundle:
pem:
server:
reload-on-update: true # watch the files and reload on change
keystore:
certificate: "file:/etc/tls/tls.crt"
private-key: "file:/etc/tls/tls.key"
truststore:
certificate: "file:/etc/tls/ca.crt"
watch:
file:
quiet-period: 10s # wait for writes to settle before reloading
server:
ssl:
bundle: server

Notice the quiet period. A rotation agent usually writes the certificate and the key as two files, and if we reloaded between the two writes we would pair a new certificate with an old key. Waiting until the files have been quiet for a few seconds avoids that. Reload on update is supported for the embedded servers Boot lists in its documentation, Tomcat and Netty when it was introduced, so check your version before relying on it for Jetty or Undertow. Existing connections keep the session they negotiated; the new certificate is used from the next handshake.

4Client Side: A Key Manager That Reloads

For HTTP clients, and for servers the framework cannot reload, we can keep one SSLContext forever and make its key manager reload instead. The idea is a small delegating key manager that holds the real one in an AtomicReference and swaps it when the files change.

Java
final class ReloadingKeyManager extends X509ExtendedKeyManager {
private final AtomicReference<X509ExtendedKeyManager> current = new AtomicReference<>();
private final Path keyStore; private final char[] password;
ReloadingKeyManager(Path keyStore, char[] password) throws Exception {
this.keyStore = keyStore; this.password = password; reload();
}
void reload() throws Exception { // called by a file watcher
KeyStore ks = KeyStore.getInstance("PKCS12");
try (InputStream in = Files.newInputStream(keyStore)) { ks.load(in, password); }
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks, password);
current.set((X509ExtendedKeyManager) kmf.getKeyManagers()[0]); // new handshakes use it
}
@Override public String chooseEngineClientAlias(String[] t, Principal[] i, SSLEngine e) {
return current.get().chooseEngineClientAlias(t, i, e);
}
@Override public X509Certificate[] getCertificateChain(String alias) {
return current.get().getCertificateChain(alias);
}
// getPrivateKey, chooseClientAlias, chooseServerAlias and the others delegate the same way
}
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[] { keyManager }, trustManagers, null);
HttpClient client = HttpClient.newBuilder().sslContext(ctx).build(); // built once, never rebuilt

The reason this design works well is that the HttpClient and its connection pool never change; only the material behind the key manager does. The alternative, rebuilding the SSLContext and creating a new client, also works, but then we must drain and close the old client and anything that captured it. Two practical notes. In Kubernetes, mounted secrets are updated by swapping a symbolic link, which a WatchService on the file itself may not report, so many teams simply check a hash of the file contents every 30 seconds. And the alias names should stay stable across rotations, so that an alias chosen just before a swap still finds a key just after it.

5Trust the CA, Not the Leaf

Rotation should only change the key store, never the trust store. If clients trusted each service's leaf certificate directly, every rotation would require updating every client, which is impossible every few days. Instead, every trust store contains the internal root CA (and, where needed, the intermediate), and any certificate the CA issues is trusted automatically. The one event that does touch trust stores is rotating the CA itself, which happens rarely, and we handle it with an overlap: distribute the new root alongside the old one, wait until every service trusts both, then start issuing from the new one, and only later remove the old root.

6Monitoring Expiry and Handshake Failures

Reloading can fail silently: the agent stops renewing, the watcher thread dies, or the new file is malformed and the reload throws. So we watch the outcome rather than the mechanism. Each service exports a metric with the number of seconds until its current certificate's notAfter date, and we alert when it falls below a third of the certificate's lifetime, for example below 24 hours for a three-day certificate. Spring Boot 3.4 and later expose SSL bundle certificate details through the actuator, including an SSL health indicator, which makes this easy (check your version). We also count handshake failures on both clients and servers, because a spike right after a rotation is the earliest sign that some service did not reload.

7Follow-up questions

Do existing connections break when the certificate is reloaded?

No. A TLS session's certificates are checked during the handshake. Connections that are already established keep working; new handshakes and resumed sessions after their lifetime use the new material. Long-lived connections, such as a database pool, should have a maximum lifetime so they eventually renegotiate with the current certificate.

Could a sidecar or service mesh handle this instead?

Yes. If a sidecar proxy terminates TLS, as in Istio or Linkerd, the proxy receives and rotates certificates automatically and the Java process speaks plain HTTP to localhost. That moves the whole problem to the platform, which is often the simplest answer for a large fleet, at the cost of running the mesh.

8Conclusion

In this answer, we saw that services fail after rotation because the SSLContext, its key manager and its trust manager are built once at startup and keep the old certificate in memory. We fixed it with Spring Boot SSL bundles and reload-on-update for the embedded server, and with a delegating key manager that reloads from disk behind a long-lived client. We kept trust stores stable by trusting the CA instead of leaf certificates, and we monitored certificate expiry and handshake failures so a failed reload is noticed before the old certificate expires.

What to say in the interviewAbout 90 seconds aloud

Open with

The services fail because Java builds its TLS state once. At startup the key store and trust store are loaded into a KeyManager and TrustManager inside an SSLContext, and the server or HTTP client keeps that SSLContext for its whole life.

Then, if they want more

When the rotation agent writes new files, the process still presents the old certificate until it expires, and a restart is the only thing that reads the new files. So I would make reload part of the design.

For the embedded server in Spring Boot I would use an SSL bundle with reload-on-update, which watches the files and swaps the certificate after a short quiet period; it is supported on Tomcat and Netty, and I would check the version for other servers.

For clients, or anything the framework does not reload, I would use a delegating key manager that holds the real one in an AtomicReference and reloads it when a hash of the files changes, so the HttpClient never needs to be rebuilt.

I would make sure every trust store trusts the internal CA rather than leaf certificates, so rotation never touches trust, and handle CA rotation with an overlap period. Then I would monitor seconds until expiry per service, handshake failures, and probe endpoints to see the certificate clients actually receive.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3.1, Spring Boot 3.4
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.
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 "Certificates for your services now rotate every few days through an internal CA. Java...": https://scrivio-interview-library.netlify.app/java/networking/00b6a427f0/