Graceful Shutdown in Spring Boot
Fast recall
Quick revision
Core answer
server.shutdown=graceful and spring.lifecycle.timeout-per-shutdown-phase to control how long the application waits for in-flight requests to complete before forcing shutdown. When SIGTERM arrives, the embedded server stops accepting new requests while existing requests drain, then the Spring context closes in reverse bean-creation order running @PreDestroy. In Kubernetes, the Spring shutdown timeout must be shorter than `terminationGracePeriodSeconds` — otherwise SIGKILL terminates the JVM mid-drain and clients see connection resets.Whether the candidate knows graceful shutdown is a first-class feature since 2.3 and understands the drain-then-close sequence, not just adding a shutdown hook manually
Implementing a custom `ShutdownHook` via `Runtime.getRuntime().addShutdownHook()` and missing the coordinated context close that Spring already provides
Explain how Kubernetes SIGTERM plus `terminationGracePeriodSeconds` must be coordinated with `spring.lifecycle.timeout-per-shutdown-phase`, and mention `SmartLifecycle` for custom beans that need ordered shutdown
Interview-ready guide
Interview answer
Graceful shutdown means the application stops accepting new requests and waits for in-flight ones to complete before closing — rather than killing active connections immediately on SIGTERM.
Enabling it
server.shutdown=graceful— Spring Boot's web server stops accepting new connections but lets active requests finishspring.lifecycle.timeout-per-shutdown-phase=30s— the maximum wait time; requests still active after this threshold are terminated
The Kubernetes integration pattern
- Kubernetes sends SIGTERM when a pod is removed; waits
terminationGracePeriodSecondsbefore sending SIGKILL - Your
timeout-per-shutdown-phasemust be shorter thanterminationGracePeriodSeconds— if Spring is still draining when Kubernetes sends SIGKILL, active requests are killed anyway - Typical setup:
terminationGracePeriodSeconds: 60,timeout-per-shutdown-phase: 50s— Spring gets 50 seconds to drain, Kubernetes terminates at 60 - Add a
preStop5-second sleep to let kube-proxy remove the pod from iptables before draining starts - Lower readiness probe
periodSecondsto 5-10s for fast de-registration
What not to do
- Do not add
Runtime.getRuntime().addShutdownHook()manually — Spring Boot already registers a shutdown hook that closes theApplicationContext; a second hook creates race conditions
For ordered cleanup
@PreDestroyruns during context close — safe for connection pools, Kafka producers, registry deregistrationSmartLifecyclewithgetPhase()when ordering matters — lower phases stop first- WebSocket and SSE need explicit close via
ContextClosedEventlistener
In an interview, the strong answer is: enable graceful shutdown, align Spring's timeout with the Kubernetes grace period, and never write a custom shutdown hook.
Graceful Shutdown in Spring Boot
Direct Answer
Since Spring Boot 2.3, graceful shutdown is built in: set server.shutdown=graceful and spring.lifecycle.timeout-per-shutdown-phase to control how long the application waits for in-flight requests to complete before forcing shutdown. When SIGTERM arrives, the embedded server stops accepting new requests while existing requests drain, then the Spring context closes in reverse bean-creation order running @PreDestroy. In Kubernetes, the Spring shutdown timeout must be shorter than terminationGracePeriodSeconds — otherwise SIGKILL terminates the JVM mid-drain and clients see connection resets.
Common Mistakes & Pitfalls
Implementing a custom ShutdownHook via Runtime.getRuntime().addShutdownHook() and missing the coordinated context close that Spring already provides
Check your understanding
Answer these without looking back at the article.
- How does the readiness probe interact with graceful shutdown in Kubernetes?
- What is the difference between `@PreDestroy`, `DisposableBean`, and `SmartLifecycle` for shutdown logic?
- How would you test that your application actually handles graceful shutdown correctly in a CI pipeline?
- Why is a `preStop` sleep useful even when `server.shutdown=graceful` is set?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.