Sub topic 6
Shutting down without dropping requests
This chapter assumes you can build and run a Spring Boot JAR with java -jar and that you have Actuator on the classpath. It builds on the executable JAR and the health endpoints from earlier chapters.
Why shutdown matters
Every deployment stops an old instance. By default, Spring Boot closes the web server immediately when it receives a stop signal. A request that is halfway through, such as a payment or a file upload, fails with a connection error. Users see this as a random failure during each release.
Graceful shutdown changes the order of events. The server stops accepting new requests, lets the in-flight requests finish, and only then closes the application context.
Turning it on
Graceful shutdown is configured with two properties.
# application.properties
server.shutdown=graceful
spring.lifecycle.timeout-per-shutdown-phase=30s
management.endpoint.health.probes.enabled=true
management.endpoints.web.exposure.include=health,infoserver.shutdown=gracefulenables the behavior. The default isimmediate.spring.lifecycle.timeout-per-shutdown-phaseis the longest the application waits for active requests. The default is 30 seconds. Requests still running after the timeout are cut off.- The probes setting exposes
/actuator/health/livenessand/actuator/health/readiness.
What happens on SIGTERM
A process manager asks a program to stop by sending the SIGTERM signal. The JVM runs shutdown hooks, and Spring starts its graceful sequence.
Two details are worth knowing:
- When shutdown begins, the readiness state changes to
REFUSING_TRAFFIC. A load balancer or Kubernetes that polls the readiness endpoint stops sending traffic to the instance. - Liveness stays
CORRECTduring shutdown. The instance is still alive, so it should not be restarted while it drains.
Trying it locally
This controller has a slow endpoint, so you have time to send the signal.
import java.util.concurrent.TimeUnit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SlowController {
@GetMapping("/slow")
public String slow() throws InterruptedException {
TimeUnit.SECONDS.sleep(15);
return "done";
}
}Start the app, then run these commands in another terminal.
curl http://localhost:8080/slow &
sleep 1
kill -TERM "$(pgrep -f myapp-1.0.0.jar)"
waitThe curl command prints done after about 15 seconds, and then the application exits. A new request sent during the wait is refused. With server.shutdown=immediate, curl reports an empty reply from the server.
Common errors
- Signal never reaches the JVM. In a Dockerfile,
ENTRYPOINT java -jar app.jar(shell form) starts a shell as process 1, and the shell may not pass SIGTERM on. Use the exec form:ENTRYPOINT ["java", "-jar", "app.jar"]. - Grace period shorter than the timeout.
docker stopwaits 10 seconds by default. Kubernetes waitsterminationGracePeriodSeconds, which is 30 by default. After that the process gets SIGKILL, which cannot be handled. Set the platform's grace period higher than the Spring timeout. - Exit code 143 treated as a failure. 143 is 128 plus 15, the signal number of SIGTERM. A systemd unit should set
SuccessExitStatus=143. - Traffic still arrives after SIGTERM. Load balancers can take a few seconds to notice that the instance is no longer ready. A short delay before shutdown, such as a Kubernetes
preStopsleep, covers this gap.
Exercise
Your application sets spring.lifecycle.timeout-per-shutdown-phase=60s, but in Kubernetes some long requests are still cut off during a deployment. The pod uses the default terminationGracePeriodSeconds. What is the likely cause, and how do you fix it?
Show answer
Kubernetes sends SIGKILL after 30 seconds (the default grace period), before Spring's 60-second wait ends. Set terminationGracePeriodSeconds higher than the Spring timeout, for example 75, or lower the Spring timeout to fit within the grace period.