Scrivio Interview library
ScenarioIntermediate5 min read · updated 12 November 2026

During every Kubernetes rolling deploy a few requests fail with connection reset, although graceful shutdown is enabled. What is wrong and how do you fix it?

Graceful shutdown only protects requests the pod has already accepted. Kubernetes removes the pod from the Service endpoints asynchronously, so for a few seconds after SIGTERM the load balancer and other pods still send it new traffic, which the stopping server refuses with connection resets. The fix is a preStop hook with a short sleep so endpoints update before the app stops accepting, a terminationGracePeriodSeconds longer than the preStop sleep plus the shutdown timeout, readiness probes that report the shutdown, and client retries for idempotent requests.

Why this question is asked

This is one of the most common surprises when moving Spring Boot services onto Kubernetes: the team enables graceful shutdown, tests it locally with a slow request, and it works, yet every rolling deploy still produces a handful of errors. Interviewers ask it because the answer requires understanding two systems at once and the race between them. In this answer, we will recap what happens when Kubernetes stops a pod, reproduce the race, and then fix it with a preStop delay, the right grace period, readiness and client retries.

1A Quick Recap of How Kubernetes Stops a Pod

During a rolling deploy, Kubernetes starts new pods and terminates old ones. When it decides to terminate a pod, two things start at the same moment. The kubelet on the node runs the pod's preStop hook, if there is one, and then sends SIGTERM to the container. In parallel, the control plane marks the pod as terminating, and the endpoints controller removes it from the Service's list of endpoints.

That removal has to travel. kube-proxy on every node must update its routing rules, an ingress controller must reload its upstream list, and a cloud load balancer must deregister the target. Each of these happens on its own schedule, and together they can take from a second to ten seconds or more. Until each one is done, it keeps sending new connections to the old pod.

2Reproducing the Race

Without a preStop hook, SIGTERM arrives almost immediately. Spring Boot begins graceful shutdown and the server stops accepting new connections. But the ingress has not yet heard that the pod is going away.

Step throughWhy requests fail after SIGTERM
Control planeIngressOld podClient
SIGTERM (no preStop)
graceful shutdown: stop accepting
pod removed from endpoints (takes seconds)
new request
forwarded: ingress list still has the old pod
connection refused or reset
502 or connection reset

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

The app stops accepting before every router has stopped sending.

3Fix 1: A preStop Sleep Before SIGTERM

Since we cannot make endpoint removal synchronous, we make the application wait for it. A preStop hook runs before SIGTERM is sent, so a short sleep there keeps the app serving normally while the routers catch up. Only then does SIGTERM start the graceful shutdown, when almost no new traffic is arriving.

YAML
spec:
terminationGracePeriodSeconds: 60 # must be > preStop sleep + Spring shutdown timeout
containers:
- name: orders
lifecycle:
preStop:
exec:
command: ["sh", "-c", "sleep 10"] # needs a shell in the image
# On newer Kubernetes versions a built-in action avoids needing a shell:
# sleep:
# seconds: 10 # check your cluster version
readinessProbe:
httpGet: { path: /actuator/health/readiness, port: 8080 }
periodSeconds: 5
---
# application.properties
server.shutdown=graceful
spring.lifecycle.timeout-per-shutdown-phase=30s

Notice that the sleep is in the pod, not in the application. Distroless images have no sh, which is why the native sleep action is useful where the cluster supports it. Ten seconds is a common starting value; the right number is however long the slowest router in our path needs, measured, not guessed.

4Fix 2: Make the Grace Period Long Enough

terminationGracePeriodSeconds is the total time Kubernetes waits, starting when it begins terminating the pod and including the preStop hook. When it runs out, the container gets SIGKILL, which ends everything at once. The default is 30 seconds. With a 10 second preStop and Spring's 30 second shutdown timeout, the default would kill the pod at 30 seconds, in the middle of the graceful wait. So the grace period must be longer than the preStop sleep plus the shutdown timeout, with a few seconds of margin, which is why the example uses 60.

Time budget for one pod
PhaseStarts atLasts up to
preStop sleep, app still serving0 s10 s
SIGTERM, graceful wait for requests10 s30 s (timeout-per-shutdown-phase)
Close pools, JVM exits40 s at worsta few seconds
SIGKILL if still running60 sterminationGracePeriodSeconds

5Fix 3: Readiness and Client Retries

When the probes are enabled, which Boot does automatically when it detects Kubernetes, Spring Boot changes its readiness state to REFUSING_TRAFFIC as shutdown begins, and /actuator/health/readiness then reports OUT_OF_SERVICE. A failing readiness probe is another signal that removes the pod from endpoints, so it helps, but only if the probe is configured, points at the readiness group, and runs often enough. Because it fires only after SIGTERM, it complements the preStop sleep rather than replacing it.

Finally, some failures will always slip through: a pooled keep-alive connection closed by the server just as a client sends on it, or a node failing. So clients should retry idempotent requests, such as GET or a PUT with the same body, once or twice with a short backoff when they see a connection error. A POST that creates something should only be retried if it carries an idempotency key, so a repeat does not create a duplicate.

6Follow-up questions

Why not just make the Spring shutdown timeout longer?

Because the timeout only controls how long running requests may finish. The failing requests are new ones that arrive after the server has stopped accepting, and no amount of waiting lets them in. Only delaying the stop of accepting, with preStop, gives the routers time.

Does the preStop sleep slow down deploys?

Yes, each old pod lives about ten seconds longer, but new pods are already running and serving, so users do not notice. For a deployment of 20 pods rolling four at a time, it adds well under a minute, which is a small price for error-free deploys.

How do we know the fix worked?

Run a load test at steady traffic and perform a rolling deploy during it. Count 5xx responses and connection errors from the client side and the ingress logs. Before the fix we might see 10 to 30 errors per deploy; after it the number should be zero or close to it.

7Conclusion

In this answer, we saw that graceful shutdown only protects requests already accepted, while Kubernetes removes the pod from endpoints asynchronously, so new traffic keeps arriving after SIGTERM and is refused. A preStop sleep lets endpoints update before the app stops accepting, the grace period must exceed the preStop plus the shutdown timeout, readiness reports the shutdown when probes are configured, and clients retry idempotent requests on connection errors.

What to say in the interviewAbout 80 seconds aloud

Open with

The problem is a race between Kubernetes and the application. When a pod is terminated, Kubernetes sends SIGTERM and, at the same time, removes the pod from the Service endpoints.

Then, if they want more

But that removal is asynchronous: kube-proxy, the ingress controller and any cloud load balancer each update on their own schedule, which can take several seconds.

Graceful shutdown makes Spring stop accepting new requests right away and finish the running ones, so any request routed to the pod during those seconds is refused, and the client sees a connection reset or a 502. Graceful shutdown only protects requests the pod already accepted.

The fix is a preStop hook with a short sleep, around ten seconds, so the app keeps serving while the routers catch up, and only then gets SIGTERM. Next, I make terminationGracePeriodSeconds longer than the preStop sleep plus Spring's shutdown timeout, for example 60 seconds for 10 plus 30, otherwise SIGKILL cuts the graceful wait short.

I also make sure the readiness probe points at the readiness group, because Boot switches readiness to refusing traffic on shutdown. And clients retry idempotent requests on connection errors. Then I verify with a deploy under load and count errors.

About this answer

Last updated
12 November 2026
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
Spring Boot reference
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 "During every Kubernetes rolling deploy a few requests fail with connection reset...": https://scrivio-interview-library.netlify.app/spring-boot/embedded-servers/672c556d46/