Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

During Kubernetes rolling deploys, a Spring Boot service loses in-flight async tasks and some HTTP requests fail. Describe how you would make shutdown graceful end to end.

Make every stage of shutdown wait for the one before it. A short preStop delay and the readiness probe stop new traffic before the app begins shutting down; server.shutdown=graceful makes the web server stop accepting requests and finish in-flight ones within a timeout; the ThreadPoolTaskExecutor is set to wait for its tasks with an await-termination period; terminationGracePeriodSeconds is longer than all of that together; and durable work lives in a queue or outbox so even a forced kill loses nothing.

Why this question is asked

Rolling deploys happen many times a day, and each one stops every old pod. If a pod stops abruptly, the requests it was serving fail and the background tasks it was running disappear. Users see errors during every deploy, and some work, like a confirmation email, silently never happens. Interviewers ask this scenario because graceful shutdown involves Kubernetes, the load balancer, Spring Boot and our own executors, and it only works if they are lined up. In this answer, we will follow what Kubernetes does when it stops a pod, find where requests and tasks get lost, and then fix each stage: traffic draining, the web server, the task executor, the grace period, and durable work.

1What Kubernetes Does When It Stops a Pod

When a rolling deploy replaces a pod, Kubernetes does two things at about the same time. It marks the pod as terminating, which removes it from the Service's endpoints so load balancers and other pods stop sending it traffic. And it runs the container's preStop hook, if there is one, and then sends SIGTERM to the process. The JVM reacts to SIGTERM by running its shutdown hooks, and Spring Boot's hook closes the application context.

Then Kubernetes waits for 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 stops it immediately, with no chance to clean up.

Step throughStopping a pod during a rolling deploy
KubernetesLoad balancerPod (Spring Boot)Task executor
remove pod from endpoints (takes a few seconds to spread)
run preStop: sleep 10 s
last requests still arrive during the delay
SIGTERM
web server: stop accepting, finish in-flight (up to 20 s)
shutdown: finish running and queued tasks (up to 20 s)
terminated
process exits before the grace period ends

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

Each stage waits for the previous one; the grace period covers them all.

2Where Requests and Tasks Get Lost

There are three gaps. First, a race: removing the pod from endpoints takes a moment to reach every load balancer and kube-proxy, while SIGTERM arrives at once. If the app stops accepting connections immediately, requests that are already on their way to it fail with connection errors.

Second, in-flight requests: without graceful shutdown, closing the context stops the web server at once, cutting off requests in the middle. Third, background tasks: a ThreadPoolTaskExecutor by default interrupts its running tasks and drops its queue when the context closes, and anything still left when SIGKILL arrives is gone. The fix is to close all three gaps, in order.

3Stop Traffic First: preStop Delay and Readiness

To close the race, we delay the start of shutdown by a few seconds with a preStop hook, so the endpoint removal has time to reach every load balancer while the app still serves requests normally. We also make sure the readiness probe uses Spring Boot's readiness state: when the application starts shutting down, Boot marks itself as refusing traffic, so the readiness endpoint reports it as not ready.

YAML
# deployment.yaml (container part)
lifecycle:
preStop:
exec:
command: ["sh", "-c", "sleep 10"] # let endpoint removal spread first
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8080
periodSeconds: 5
terminationGracePeriodSeconds: 60 # more than 10 + 20 + 20, see below

Newer Kubernetes versions also offer a built-in sleep action for preStop, which avoids needing a shell in the image; check your cluster version. Ten seconds is a common value; it should be a little longer than the time your load balancers take to notice an endpoint change.

4Finish In-Flight Requests: Graceful Web Server Shutdown

With server.shutdown=graceful, when the context starts closing, the embedded web server stops accepting new requests and waits for the active ones to complete. Spring Boot 3.4 and later use graceful shutdown by default, but setting it explicitly documents the intent and covers older versions. The wait is bounded by spring.lifecycle.timeout-per-shutdown-phase, 30 seconds by default; requests still running after that are cut off.

YAML
server:
shutdown: graceful
spring:
lifecycle:
timeout-per-shutdown-phase: 20s # in-flight requests get up to 20 s
task:
execution:
shutdown:
await-termination: true # wait for @Async tasks on context close
await-termination-period: 20s

The last two properties configure the auto-configured task executor that @Async uses, which brings us to background tasks.

5Finish Background Tasks: ThreadPoolTaskExecutor Settings

A ThreadPoolTaskExecutor that we define ourselves needs the same settings in code. setWaitForTasksToCompleteOnShutdown(true) makes it call shutdown() instead of shutdownNow() when the context closes, so running and queued tasks may finish. setAwaitTerminationSeconds bounds how long the context waits for them, so shutdown cannot hang forever.

Java
@Bean
ThreadPoolTaskExecutor emailExecutor() {
ThreadPoolTaskExecutor ex = new ThreadPoolTaskExecutor();
ex.setThreadNamePrefix("email-");
ex.setCorePoolSize(4);
ex.setMaxPoolSize(4);
ex.setQueueCapacity(500);
ex.setWaitForTasksToCompleteOnShutdown(true); // shutdown(), not shutdownNow()
ex.setAwaitTerminationSeconds(20); // then wait up to 20 s for tasks
return ex;
}
// on SIGTERM: "email-" threads finish their 37 queued emails in 6 s, then the context closes

Spring Framework 6.1 and later also let the executor take part in the context's lifecycle, so it can stop accepting new tasks early in shutdown while still finishing what it has; check the version for the exact behaviour. Either way, the executor's wait comes after the web server's phase, so the times add up.

6The Grace Period and Durable Work

Now we can size terminationGracePeriodSeconds. The stages run one after another: 10 s of preStop delay, up to 20 s for in-flight requests, up to 20 s for background tasks. That is up to 50 seconds, so a grace period of 30 seconds, the default, would SIGKILL the pod in the middle of the executor's wait. We set it to 60 seconds to leave a margin.

Even with all this, some shutdowns are not graceful: a node fails, the kernel kills the process for using too much memory, or a task runs longer than every timeout. So work that must not be lost should never live only in an executor's in-memory queue. The durable pattern is to write the work to a database table or a message broker as part of the request, often with the outbox pattern, where the work row is inserted in the same transaction as the business change, and let a worker process it with retries. The in-memory executor then only speeds things up; if a pod dies, another pod picks the work up from the table or queue.

Shutdown budget for one pod
StageSettingTime
Drain trafficpreStop sleep10 s
Finish in-flight requeststimeout-per-shutdown-phaseup to 20 s
Finish background tasksawaitTerminationSecondsup to 20 s
Margin-10 s
Total grace periodterminationGracePeriodSeconds60 s

7Follow-up questions

Why is a readiness probe not enough without the preStop delay?

Because the probe only runs every few seconds and its result takes time to reach every load balancer. Endpoint removal and SIGTERM start at the same moment, so without a delay the app can stop accepting connections before the rest of the cluster stops sending them.

Is graceful shutdown the default in Spring Boot?

Since Spring Boot 3.4, the default for server.shutdown is graceful; in earlier versions it was immediate. Setting it explicitly makes the configuration clear and works on every version.

What happens to long-running requests such as file uploads or streaming?

They are cut off when the shutdown phase timeout expires. For long operations, clients need to be able to resume or retry, and very long jobs should run as durable, restartable work rather than inside a single request.

8Conclusion

In this answer, we made shutdown graceful end to end by lining up every stage. A preStop delay and the readiness probe stop new traffic before shutdown starts; server.shutdown=graceful with a phase timeout lets in-flight requests finish; a ThreadPoolTaskExecutor with waitForTasksToCompleteOnShutdown and an await-termination period lets background tasks finish; terminationGracePeriodSeconds covers the sum of all of them; and durable work lives in a queue or outbox, so even a forced kill loses nothing.

What to say in the interviewAbout 90 seconds aloud

Open with

I would line up every stage of shutdown so each one waits for the previous one. When Kubernetes stops a pod, it removes it from the endpoints and sends SIGTERM at about the same time, so first I add a preStop hook that sleeps around ten seconds, which lets the endpoint removal reach all load balancers while the app still serves normally, and I use Spring Boot's readiness endpoint as the readiness probe, since Boot marks itself as refusing traffic when shutdown starts.

Then, if they want more

Second, the web server: server.shutdown=graceful, which is the default since Boot 3.4 but I set it explicitly, so on SIGTERM Tomcat stops accepting requests and waits for in-flight ones, bounded by spring.lifecycle.timeout-per-shutdown-phase, say 20 seconds.

Third, background tasks: on our ThreadPoolTaskExecutor I set waitForTasksToCompleteOnShutdown to true, so it uses shutdown rather than shutdownNow, and an awaitTerminationSeconds of about 20; for the auto-configured executor the spring.task.execution.shutdown properties do the same.

Fourth, the grace period: these stages add up, ten plus twenty plus twenty, so the default 30 second terminationGracePeriodSeconds would SIGKILL us halfway; I would set 60.

And finally, since some shutdowns are never graceful, like a node failure or an out-of-memory kill, anything that must not be lost goes into a durable queue or an outbox table written in the same transaction, and the executor just processes it faster. Then a forced kill delays work but never loses it.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3.4, Spring Framework 6
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 "During Kubernetes rolling deploys, a Spring Boot service loses in-flight async tasks and...": https://scrivio-interview-library.netlify.app/java/executors/e8da94ca61/