Deployment health and shutdown
Prerequisite: node-reverse-proxy-and-linux-process. Objective: Design readiness and drain checks. Each example is independent. Tested with Node.js 22.16.0 on Linux. Save as main.mjs in a fresh disposable directory and run node main.mjs. These are static code blocks; external system behavior is not established by a local model.
How it works
Liveness should indicate a process that needs restarting, not every brief downstream outage. Readiness can fall when a critical dependency or drain state prevents serving. On SIGTERM, mark unready, stop new admission, close listeners, wait for in-flight work within a deadline, close resources, then exit. Long-lived connections need explicit policy. Coordinate the application deadline with the external supervisor or orchestrator grace period.
Serve separate health endpoints
import assert from 'node:assert/strict';import http from 'node:http';import {once} from 'node:events';let ready=true;const server=http.createServer((req,res)=>{res.statusCode=req.url==='/live'?200:req.url==='/ready'?(ready?200:503):404;res.end();});server.listen(0,'127.0.0.1');await once(server,'listening');try{const base=`http://127.0.0.1:${server.address().port}`;const status=async p=>{const r=await fetch(base+p);await r.text();return r.status;};assert.equal(await status('/ready'),200);ready=false;console.log(await status('/ready'),await status('/live'));}finally{server.closeAllConnections();server.close();}Expected output:
503 200A draining service can remain alive while unready.
Track admitted work during drain
import assert from 'node:assert/strict';let draining=false,active=0;function admit(){if(draining)return false;active++;return true;}assert.equal(admit(),true);draining=true;assert.equal(admit(),false);active--;console.log(draining,active);Expected output:
true 0The model separates admission and completion; actual signals and sockets require integration tests.
Common mistake
Do not make every readiness failure restart the process or use immediate process.exit as the normal drain path.
Practice
Choose a 20-second application drain inside a 30-second supervisor grace period and describe what happens if work exceeds it.
Reference candidate: node-deployment-health-and-shutdown-lookup (route unverified). Documentation anchors: https://kubernetes.io/docs/concepts/workloads/pods/probes/. External versions and full integration are pending.
Administrative staged lab: staged-deployment-labs/container/main.mjs. Read its README for prerequisites, execution boundaries and cleanup. Not a verified public route.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Deployment health and shutdown — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
What does readiness failure usually mean?
What should a drain do first?
Why bound shutdown time?