{"canonical_url":"https://engineer.company/glossary/graceful-shutdown/","content_markdown":"# Graceful shutdown\n\n## What it is\n\nGraceful shutdown is a planned sequence for ending a running process while\naccounting for work it has already started. The process receives a stop request,\nstops taking new work and gives active operations a bounded chance to finish or\nreach a recoverable state. At the end it closes its resources and exits. The\nsequence makes an ordinary restart or deployment predictable for the service and\nits callers.\n\nThe steps depend on what the process does. An HTTP server can stop accepting\nconnections and wait for active requests. A queue worker can stop pulling jobs\nand record a safe point for the ones it already owns. A process that maintains\nan upgraded connection such as a WebSocket can need its own policy for telling\nclients to reconnect. State the expected behaviour of each kind of work. A\nsingle process can serve several kinds at once.\n\nThe platform usually gives the process a deadline. In Kubernetes, the Pod's\ntermination grace period is part of that lifecycle. In a supervised service, a\nstop timeout can determine how long the supervisor waits before forcing the\nprocess to exit. The application should use its deadline to bound its own work\nand leave enough time to release or preserve the resources whose state a later\nattempt needs.\n\n## Why it matters\n\nA process can be replaced during a normal release while a user is uploading a\nform or waiting for an answer. If the application stops abruptly, the client may\nhave no evidence about whether the work was saved. It may try again, and a\nsecond request can produce another effect. A graceful sequence can help a\nstarted operation finish cleanly or return it to a state a retry can handle.\nWhere uncertainty remains, stable identities and durable state give the product\na way to describe it.\n\nQueued work has similar consequences. A job may have downloaded data, generated\na file or sent a message before a worker stops. The queue can deliver it again\nif completion was never recorded. Design the job's durable steps and\ndeduplication policy so that a retry can establish the correct outcome. Waiting\nfor a short task to finish is useful. A documented retry path is useful when a\ntask is longer than the time available.\n\nShutdown also influences how a rollout uses capacity. A process that remains\neligible for requests while it tries to drain can receive new work until the\ndeadline. Coordinate traffic admission with the application and the platform.\nObserve when old connections stop carrying work and how replacement instances\nbecome ready. In a busy service those timings make the difference between a\nquiet release and a wave of client retries.\n\n## What it looks like in practice\n\nImagine an application with a download endpoint and a worker that builds\nrequested reports. First signal that the application is leaving service. Stop\naccepting new report requests in that instance and stop reserving jobs in the\nworker. Let active connections and jobs continue until their respective\ndeadlines. A report whose output is completely written can be published with its\nstable identifier. A job that needs more time should remain eligible for a\ndeliberate retry, and its unfinished output should be easy to distinguish from a\ncomplete file.\n\nTest three cases. Shut down with no active work, with a simple request that\nfinishes within the grace period and with a job that is intentionally held\nlonger. Record which operations finished, what a new worker can see and how a\nclient learns the final status. The third case proves how the system behaves\nwhen its graceful interval is exhausted. Check the result at the normal\napplication entry point and with the queue's own records.\n\nEngineer's\n[durable River job-queue case study](/portfolio/moved-slow-work-onto-a-river-job-queue/)\nexplains why work such as generating a document or rebuilding an index belongs\nin a persistent queue. Its PostgreSQL-backed jobs support retries and a visible\nhistory. The shutdown example uses that same separation between accepting work\nand finishing it. The service can preserve a task across replacement and still\nhave measurable outcomes for operations that were already active.\n\n## Where it goes wrong\n\nA shutdown handler can wait on a resource after another handler has already\nclosed it. Write the order in terms of where each operation still needs access.\nA worker finishing a transaction may need its database connection until its\noutcome is durable. The process can close the connection pool once the relevant\nwork has completed or reached a known stopping point. Keep cleanup bounded and\ntest a failure during the drain itself.\n\nApplication and platform timeouts can also conflict. If the application expects\na minute to finish a job and the platform forcibly stops it after a shorter\ninterval, the application's best-case path may never run in a release. Observe\nactual signals and deadlines in an environment representative of production.\nInclude time consumed by any hook and inspect what happens when the process\nexits unusually fast or fails to exit.\n\nFinally, verify repeatability. Send a second stop request while a drain is\nunderway, and restart the service after it reaches the hard deadline. Check for\nsurviving temporary output, stuck job ownership and operations whose clients\nwere disconnected. Look at the user's outcome as well as a clean process exit.\nThe aim is a transition whose remaining work, finished results and recovery\nobligations are visible to the next instance and the people operating it.\n\n## Further reading\n\n- [The Kubernetes Pod termination lifecycle](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-termination).\n  Describes termination, endpoint state and grace-period behaviour.\n- [Go HTTP Server.Shutdown](https://pkg.go.dev/net/http#Server.Shutdown).\n  Documents how a Go server closes listeners and waits for connections.\n- [systemd service stop timeouts](https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html#TimeoutStopSec=).\n  Explains how a supervisor limits the time spent stopping a service.\n\n\u003chttps://engineer.company/glossary/graceful-shutdown/\u003e\n","date_modified":"2026-11-25T07:18:46Z","description":"Plan how requests and jobs drain before a workload stops, and test what happens when its deadline arrives.","formats":{"gemtext":"https://engineer.company/glossary/graceful-shutdown/index.gmi","gopher":"https://engineer.company/glossary/graceful-shutdown/gophermap","html":"https://engineer.company/glossary/graceful-shutdown/","json":"https://engineer.company/glossary/graceful-shutdown/index.json","markdown":"https://engineer.company/glossary/graceful-shutdown/index.md","pdf":"https://engineer.company/glossary/graceful-shutdown/index.pdf"},"kind":"page","language":"en","title":"What is a graceful shutdown?","url":"https://engineer.company/glossary/graceful-shutdown/","version":1}