Graceful Shutdown in Spring Boot 2.3: Configuration, Usage, and Best Practices
This article explains how Spring Boot 2.3 provides built‑in graceful shutdown for embedded web servers, how to enable it with configuration, the role of the Shutdown enum and timeout settings, and additional techniques such as Actuator endpoints and signal handling to ensure safe application termination.
Spring Boot 2.3 introduces built‑in graceful shutdown support for embedded web servers (Tomcat, Jetty, Undertow, Reactor Netty). When the application receives a shutdown signal, the server stops accepting new requests and waits for active requests to finish within a configurable timeout.
The article starts with a simple controller example that simulates a long‑running request. It explains that without graceful shutdown, killing the JVM aborts the request and may cause data inconsistency.
To enable graceful shutdown, set server.shutdown=graceful in the application configuration (e.g., application.yml or application.properties). The underlying Shutdown enum defines two modes: GRACEFUL and IMMEDIATE.
@RestController
public class DemoController {
@GetMapping("/demo")
public String demo() throws InterruptedException {
// Simulate business processing delay
Thread.sleep(20 * 1000L);
return "hello";
}
}The timeout for each shutdown phase can be configured via spring.lifecycle.timeout-per-shutdown-phase, which defaults to 30 seconds; if the timeout expires, the server forces termination regardless of ongoing tasks.
/**
* Configuration for shutting down a {@link WebServer}.
*/
public enum Shutdown {
/** Graceful shutdown (timed) */
GRACEFUL,
/** Immediate shutdown */
IMMEDIATE;
}Log excerpts show the graceful shutdown process, where the container logs indicate it is waiting for active requests to complete before finally stopping.
Additional knowledge includes the difference between sending SIGINT (kill ‑2) which triggers Java’s ShutdownHook and SIGKILL (kill ‑9) which bypasses it, using the Actuator endpoint /actuator/shutdown to trigger graceful shutdown programmatically, and how different web containers implement shutdown behavior.
Images in the original article illustrate the configuration UI and the shutdown sequence, and the article concludes with promotional material for an architecture community.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
