Prevent Spring Boot application closing until all current requests are finished
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If a Spring Boot service stops immediately while requests are still running, clients can see dropped connections, partial work, or corrupted workflows. The right solution is graceful shutdown: stop accepting new traffic, let in-flight requests finish, and only then complete the shutdown sequence.
Use Spring Boot’s Graceful Shutdown Support
Modern Spring Boot has built-in graceful shutdown support. Enable it with:
or in YAML:
With this enabled, the embedded server stops accepting new requests and waits for active ones to complete until the shutdown timeout is reached.
What Actually Happens During Shutdown
Graceful shutdown is not “keep the JVM alive forever”. It is a controlled sequence:
- the app receives a termination signal such as
SIGTERM - Spring begins its shutdown lifecycle
- the web server stops accepting new requests
- existing requests are allowed to finish within the timeout
- beans and resources are closed cleanly
That makes it much safer for rolling deployments, container restarts, and orchestrated shutdowns.
Test It with a Long Request
You can verify the behavior with a deliberately slow endpoint:
If graceful shutdown is enabled and the timeout is long enough, a request already inside /slow should still complete after the process receives the shutdown signal.
Containers and Load Balancers Matter Too
Application config alone is not enough if the runtime environment kills the process too quickly. For example:
- Kubernetes needs a termination grace period long enough for the app timeout
- load balancers should stop sending new requests before the pod or instance disappears
- process managers should send
SIGTERMbeforeSIGKILL
If the container runtime force-kills the app after a few seconds, Spring Boot never gets a real chance to finish the in-flight work.
Keep Request Handlers Interrupt-Safe
Graceful shutdown works best when request handlers do not block forever and when downstream calls have sensible timeouts. If a request hangs indefinitely on a remote dependency, shutdown can stall until the configured limit is reached.
That means you should still configure:
- HTTP client timeouts
- database timeouts
- thread-pool sizing
Graceful shutdown is not a substitute for well-bounded request execution.
Background Work Is a Separate Problem
The title often focuses on HTTP requests, but many Spring apps also have async jobs, schedulers, or message consumers. Those workloads need their own shutdown coordination. Letting web requests finish does not automatically guarantee background queues drain safely.
If you use custom executors, make sure they are managed beans and participate in shutdown rather than being ad hoc thread pools left running outside the container lifecycle. That distinction becomes very important in services that mix APIs with queue consumers or scheduled maintenance tasks.
Common Pitfalls
- Expecting graceful shutdown to work without setting
server.shutdown=graceful. - Configuring a long Spring timeout while the container or orchestrator kills the process much sooner.
- Forgetting that new traffic must be drained at the load balancer or platform level too.
- Allowing request handlers to block indefinitely on downstream systems.
- Assuming background jobs are covered automatically just because HTTP shutdown is graceful.
Summary
- Enable graceful shutdown with
server.shutdown=graceful. - Set a realistic
spring.lifecycle.timeout-per-shutdown-phasefor your request durations. - Make sure the runtime environment gives Spring Boot enough time to finish in-flight work.
- Test shutdown behavior with slow endpoints instead of assuming the configuration is correct.
- Treat background task shutdown as a separate concern from request draining.
Related reading
- Prevent Spring Boot from registering a servlet filter
- Prevent stack trace logging for custom exception in Spring Boot application
- Print all the Spring beans that are loaded - Spring Boot
- Print an integer in binary format in Java
- Print the data in ResultSet along with column names
- Printing Even and Odd using two Threads in Java
- Printing HashMap In Java
- Printing Java Collections Nicely toString Doesn't Return Pretty Output

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.