Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What happens when Kubernetes stops a Spring Boot pod during a rolling deployment, and how do you make in-flight requests finish?

Kubernetes sends the container SIGTERM, waits terminationGracePeriodSeconds (30 seconds by default), and then kills it with SIGKILL. Spring Boot's graceful shutdown, server.shutdown=graceful and the default since Boot 3.4, stops accepting new requests on SIGTERM and lets in-flight requests finish, limited by spring.lifecycle.timeout-per-shutdown-phase (30 seconds by default). Because removing the pod from load balancers happens asynchronously, we add a short preStop sleep so traffic stops arriving first, and we set the grace period longer than the preStop delay plus the shutdown timeout.

Why this question is asked

Every rolling deployment stops old pods while they are still serving requests. If that stop is abrupt, users see failed requests on every release, even though nothing is wrong with the new version. Interviewers ask this to see whether we know the exact sequence Kubernetes follows, where the timing gaps are, and which Spring Boot settings close them. In this answer, we will walk through what Kubernetes does to a terminating pod, see what Spring Boot's graceful shutdown does, find the race with endpoint removal, and put the timings together correctly.

1What Kubernetes Does When It Stops a Pod

During a rolling update, Kubernetes starts a new pod, waits until it is ready, and then deletes an old one. Deleting a pod sets it to Terminating, and two things start at the same time. On the node, the kubelet runs the container's preStop hook if there is one, and after it finishes sends SIGTERM to the main process. Elsewhere, the endpoints controller removes the pod from the Service's endpoints, and every kube-proxy, ingress controller and service mesh sidecar eventually updates its routing to stop sending traffic there.

The kubelet then waits up to terminationGracePeriodSeconds, 30 seconds by default, counted from the start of termination and including the preStop hook. If the process is still running at the end, it sends SIGKILL, which cannot be caught, and any request still in progress is cut off.

Step throughTerminating a pod during a rolling update
API serverKubeletSpring Boot appLoad balancer
remove pod from endpoints (asynchronous)
pod is Terminating
run preStop: sleep 10 s
a few late requests still arrive
SIGTERM
readiness REFUSING_TRAFFIC, stop accepting, drain in-flight
process exits
SIGKILL only if grace period is exceeded

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

The preStop sleep covers the time the load balancer needs to stop routing to the pod.

2What Spring Boot's Graceful Shutdown Does

When the JVM receives SIGTERM, it runs its shutdown hooks, and Spring Boot's hook closes the application context. With graceful shutdown enabled, the web server first stops accepting new connections or requests, in a way that depends on the server (Tomcat, Jetty and Undertow stop accepting at the network level; Reactor Netty stops at the request level), and then waits for active requests to complete. Only after that do other beans, such as connection pools, shut down.

Graceful shutdown is enabled with server.shutdown=graceful, and since Spring Boot 3.4 it is the default for all embedded servers. The wait is limited by spring.lifecycle.timeout-per-shutdown-phase, 30 seconds by default. If requests are still running after that, the server is stopped anyway. At the same time Spring Boot sets readiness to REFUSING_TRAFFIC, so the readiness probe starts failing.

YAML
# application.yaml
server:
shutdown: graceful # default since Spring Boot 3.4
spring:
lifecycle:
timeout-per-shutdown-phase: 20s # how long in-flight requests may take
# Log on SIGTERM:
# Commencing graceful shutdown. Waiting for active requests to complete
# Graceful shutdown complete

3The Race: Endpoint Removal Is Asynchronous

Here is the subtle part. SIGTERM and endpoint removal start at the same moment, but they travel at different speeds. SIGTERM arrives almost immediately. Endpoint removal must go through the API server, the endpoints controller, and then every proxy and load balancer in the cluster, which can take from under a second to several seconds. During that gap, new requests are still routed to a pod whose server has just stopped accepting connections, and they fail with connection refused or 502 and 503 errors.

Graceful shutdown does not fix this, because it only protects requests that already arrived. The fix is to delay SIGTERM with a preStop hook that simply sleeps for a few seconds. During the sleep, the application keeps serving normally, the routing updates catch up, traffic stops arriving, and then SIGTERM starts the graceful drain of whatever is still running.

Java
spec:
terminationGracePeriodSeconds: 45
containers:
- name: orders
lifecycle:
preStop:
sleep:
seconds: 10 # native sleep action, newer Kubernetes; check your version
# on older clusters: exec: { command: ["sh", "-c", "sleep 10"] }

The exec form needs a sleep binary in the image, which distroless images do not have; the native sleep action avoids that, and recent Kubernetes versions support it. Check your version.

4Putting the Timings Together

The grace period covers everything: the preStop sleep, then the graceful drain, then a little margin for the rest of the context to close. So terminationGracePeriodSeconds must be greater than the preStop delay plus the shutdown phase timeout. With a 10 second sleep and a 20 second phase timeout, 45 seconds leaves 15 seconds of margin for closing pools and flushing logs. If the grace period were the default 30 seconds, a slow request could still be cut off by SIGKILL in the middle of the drain.

Shutdown budget for one pod
PhaseSettingTime
Load balancers stop routingpreStop sleep10 s
In-flight requests finishtimeout-per-shutdown-phaseup to 20 s
Other beans close, JVM exitsmargina few seconds
Hard limit before SIGKILLterminationGracePeriodSeconds45 s
The grace period must be longer than the preStop delay plus the shutdown timeout.

Notice that the phase timeout should be at least as long as our slowest normal request. If an export endpoint can take 60 seconds, either the numbers grow accordingly or that work moves to a background job that can be resumed.

5Follow-up questions

Why does the JVM not get SIGTERM if the app is started by a shell script?

If the entrypoint is a shell that starts java as a child, the shell receives SIGTERM and may not forward it, so Java is killed later by SIGKILL without shutting down gracefully. Use the exec form of ENTRYPOINT, or exec java in the script, so the JVM is the main process and receives the signal.

Does readiness failing on shutdown remove the pod from traffic?

Not really, and not fast enough to rely on. Kubernetes already removes a terminating pod from endpoints when deletion starts, regardless of readiness. Readiness turning to REFUSING_TRAFFIC is still useful for setups that route by health checks, such as some external load balancers, but the preStop sleep is what closes the gap.

What about long-lived connections such as WebSockets or gRPC streams?

Graceful shutdown waits for active requests, but a stream may never end by itself. The application should tell clients to reconnect, for example with a gRPC GOAWAY or a WebSocket close message, and clients should reconnect to another pod. The phase timeout then bounds how long we wait.

6Conclusion

In this answer, we saw that Kubernetes stops a pod by running the preStop hook, sending SIGTERM, and sending SIGKILL after terminationGracePeriodSeconds, while it removes the pod from endpoints asynchronously. Spring Boot's graceful shutdown, the default since Boot 3.4, stops accepting new requests and drains in-flight ones within spring.lifecycle.timeout-per-shutdown-phase. A preStop sleep of a few seconds lets load balancers stop routing first, and the grace period must be longer than the preStop delay plus the shutdown timeout.

What to say in the interviewAbout 80 seconds aloud

Open with

When Kubernetes deletes a pod in a rolling update, two things start in parallel. The pod is removed from the Service endpoints, which then has to propagate to kube-proxy, ingress controllers and meshes, and the kubelet runs the preStop hook and then sends SIGTERM.

Then, if they want more

It waits terminationGracePeriodSeconds, 30 by default, and then sends SIGKILL. On SIGTERM, Spring Boot's graceful shutdown kicks in. It is server.shutdown=graceful, the default since Boot 3.4: the web server stops accepting new requests, readiness goes to REFUSING_TRAFFIC, and in-flight requests are allowed to finish, bounded by spring.lifecycle.timeout-per-shutdown-phase, which defaults to 30 seconds.

The catch is the race: SIGTERM arrives immediately, but routing updates take a few seconds, so new requests still arrive at a server that stopped accepting them. That is why I add a preStop sleep, around 5 to 10 seconds, using the native sleep action or an exec sleep, so traffic stops before SIGTERM.

Finally the numbers must fit: the grace period has to be longer than the preStop sleep plus the shutdown timeout, for example 10 plus 20 with a grace period of 45. And the JVM must be PID 1 or receive the signal, so I use the exec form of the entrypoint.

About this answer

Last updated
12 November 2026
Versions it names
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.
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 "What happens when Kubernetes stops a Spring Boot pod during a rolling deployment, and...": https://scrivio-interview-library.netlify.app/spring-boot/containers-and-kubernetes/142f455d9e/