graceful shutdown
Stopping a service in an orderly way: finish current work, close connections, then exit.
In more detail
It relies on the process receiving and handling SIGTERM, the polite stop request, before anything escalates to SIGKILL. For load-balanced services, draining a backend first lets it finish existing connections. Two common traps are starting the app through a shell that does not pass the signal on, and allowing less time than the app needs to finish.
Examples
In the twelve-factor app a web process stops listening, finishes its requests and exits when it receives SIGTERM, and a worker returns its job to the queue. Logging the shutdown proves it was graceful.On SIGTERM: mark unready, wait for load balancers, drain in-flight requests, close database pools, exit with code zero.signal.Notify(stop, syscall.SIGTERM)
<-stop
server.Shutdown(ctx)Spring Boot: server.shutdown=graceful stops accepting new requests and waits for active ones.Related terms
- disposability — The property of a process that can be started or stopped at a moment’s notice without harm.
- SIGTERM — The signal that politely asks a process to finish its work and exit.
- connection draining — Stopping new traffic to a backend while letting its existing connections finish, before removing or restarting it.
- init process — The first process started on a system or in a container, with process ID 1 and special duties.
- exec form — Writing RUN, CMD or ENTRYPOINT as a JSON list such as ["python", "app.py"], so the program runs directly without a shell.
- docker stop — Asks a running container to shut down cleanly, then forces it if it does not.
- readiness probe — A check that decides whether a pod should receive traffic right now; failing it removes the pod from its Service without restarting it.
- terminationGracePeriodSeconds — The pod setting for how long Kubernetes waits after sending SIGTERM before killing the container with SIGKILL, 30 seconds by default.
- @PreDestroy — An annotation marking a bean method to run while the application context is closing, to release resources the bean owns.
- bean lifecycle — The stages a Spring bean passes through: construction, dependency injection, initialization, use, and destruction when the context closes.
- SIGKILL — The signal that ends a process immediately; it cannot be caught or ignored.
Taught in
- Disposability, Dev Prod Parity And Logs — DevOps & Site Reliability Engineering
- Run, Stop, Restart And Remove A Container — Docker Architecture & Production Containers
- Health Probes & Graceful Shutdown: Kubernetes Lifecycles — Production Full-Stack Engineering
- Graceful Shutdown on SIGTERM — Concurrent Systems Programming with Go
- The Spring Bean Lifecycle — Modern Java: Virtual Threads & High-Throughput Services
- Packaging Into A Container — Modern Java: Virtual Threads & High-Throughput Services
- Signals, And What kill Really Sends — Linux Fundamentals & Systems Administration
- Load Balancing: Algorithms, Health Checks And Layers — Linux Fundamentals & Systems Administration
- Close Phase & Signal Dispatch: Sockets & Teardown — Node.js Internals & Backend Services
- Thread Pooling: Piscina & CPU-Bound Workload Pools — Node.js Internals & Backend Services
- Cluster Architecture: node:cluster & Zero-Downtime — Node.js Internals & Backend Services
- Production Hardening: Graceful Shutdown & Docker — Node.js Internals & Backend Services