Graceful Shutdown
Handle process termination signals to finish in-flight requests and close resources cleanly before exiting.
Description
Graceful shutdown is the practice of handling termination signals (SIGTERM, SIGINT) to allow an application to finish processing in-flight requests, close database connections, flush buffers, and release resources before the process exits. Without graceful shutdown, active requests are abruptly terminated, database transactions may be left in an inconsistent state, and cached data may be lost.
The standard graceful shutdown sequence for an HTTP server involves: (1) receiving SIGTERM, (2) stopping acceptance of new connections, (3) signaling the load balancer or service registry that the instance is draining (e.g., failing readiness checks), (4) waiting for in-flight requests to complete within a configurable timeout, (5) closing database connection pools and other resources, and (6) exiting with code 0. If in-flight requests don't complete within the timeout, the process should force-exit to avoid hanging indefinitely.
In containerized environments, Kubernetes sends SIGTERM and waits for the terminationGracePeriodSeconds (default 30s) before sending SIGKILL. The application's shutdown timeout must be less than this value. Docker has a similar stop_grace_period. Node.js applications must be careful with PID 1 signal handling -- using tini or dumb-init as the init process ensures SIGTERM is properly forwarded to the Node process rather than being ignored.
Prompt Snippet
Implement graceful shutdown by registering handlers for SIGTERM and SIGINT. On signal: (1) log the shutdown initiation, (2) call server.close() to stop accepting new connections, (3) set a shutdown flag checked by the readiness endpoint to return 503, (4) wait for in-flight requests with a 25-second timeout (below Kubernetes' default 30s terminationGracePeriodSeconds), (5) close the PostgreSQL connection pool, Redis client, and any open file handles, (6) flush pending logs, (7) exit with code 0. If timeout expires, log remaining connections and call process.exit(1). Use tini as PID 1 in Docker to ensure signal forwarding.
Tags
Related Terms
Health Check Endpoints
Expose HTTP endpoints that report application health status for use by load balancers, orchestrators, and monitoring systems.
Zero-Downtime Deployments
Deploy application updates without any period of unavailability by gradually replacing old instances with new ones.
Docker Containerization
Package applications and their dependencies into isolated, portable containers using Docker.
Container Orchestration (Kubernetes basics)
Automate deployment, scaling, and management of containerized applications using Kubernetes.
12-Factor App Methodology
A set of twelve principles for building modern, scalable, maintainable software-as-a-service applications.