Mastering Graceful Shutdown in Spring Boot 2.3: A Complete Guide

This article explains Spring Boot 2.3's built‑in graceful shutdown feature, how to enable it with server.shutdown=graceful, configure timeout‑per‑shutdown‑phase, compare container behaviors, use the Actuator /shutdown endpoint, and provides code examples and practical steps to avoid data loss during termination.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Mastering Graceful Shutdown in Spring Boot 2.3: A Complete Guide

When a Spring Boot application receives a request that executes business logic, a forced termination (e.g., kill) will close the embedded container (Tomcat, Jetty, etc.) immediately, causing the request to fail and potentially leading to data inconsistency because transactions are not rolled back.

Graceful Shutdown

Since Spring Boot 2.3, graceful shutdown is built in and works with embedded web servers such as Jetty, Reactor Netty, Tomcat, and Undertow, for both reactive and servlet‑based applications.

Graceful shutdown diagram
Graceful shutdown diagram

How to enable

Set the property server.shutdown=graceful. When the web container shuts down, it stops accepting new requests and waits for active requests to finish within a configurable buffer period.

Configuration experience

Configuration screenshot
Configuration screenshot

Buffer period timeout-per-shutdown-phase configuration

The default timeout is 30 seconds; if active requests do not finish within this period, the server shuts down regardless of their state, so the timeout should be set appropriately.

Graceful shutdown workflow

Send a request to the service.

Issue the shutdown command.

The server receives the shutdown signal and stops accepting new requests.

Active requests complete, then the server terminates.

Step 1
Step 1
Step 2
Step 2
Step 3
Step 3

Related knowledge

Use kill -2 (equivalent to Ctrl+C) to trigger Java's ShutdownHook for graceful shutdown, instead of kill -9, which forcefully terminates the process without invoking the hook.

@Override
public void registerShutdownHook() {
    if (this.shutdownHook == null) {
        this.shutdownHook = new Thread(SHUTDOWN_HOOK_THREAD_NAME) {
            @Override
            public void run() {
                synchronized (startupShutdownMonitor) {
                    doClose();
                }
            }
        };
        Runtime.getRuntime().addShutdownHook(this.shutdownHook);
    }
}

Graceful shutdown via Actuator

Send a POST request to /actuator/shutdown to trigger graceful shutdown programmatically.

Shutdown endpoint source code

@Endpoint(id = "shutdown", enableByDefault = false)
public class ShutdownEndpoint implements ApplicationContextAware {
    @WriteOperation
    public Map<String, String> shutdown() {
        Thread thread = new Thread(this::performShutdown);
        thread.setContextClassLoader(getClass().getClassLoader());
        thread.start();
        return Collections.singletonMap("status", "shutdown initiated");
    }
    private void performShutdown() {
        try {
            Thread.sleep(500L);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        this.context.close();
    }
}

Different web container graceful shutdown behaviors

Tomcat 9.0.33+ : Stops accepting new requests; existing requests wait until timeout.

Reactor Netty : Stops accepting new requests; clients wait for timeout.

Undertow : Stops accepting new requests; new requests receive HTTP 503 immediately.

By configuring server.shutdown=graceful and adjusting timeout-per-shutdown-phase, Spring Boot applications can ensure ongoing requests finish gracefully, avoiding data loss and inconsistent states during shutdown.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaSpring BootGraceful ShutdownActuator
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.