Structure, logging, testing and shutdown · senior
Graceful shutdown and health checks
On SIGTERM a server should stop accepting new connections, finish the requests already in progress, close database pools and queue workers, and then exit, with a hard timeout in case something hangs.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Platforms send SIGTERM, wait a grace period, then send SIGKILL (Kubernetes waits 30 s by default; docker stop waits 10 s).
- process.on('SIGTERM', shutdown): fail the readiness check so the load balancer stops sending traffic; server.close() (stops new connections; its callback runs when open ones end); server.closeIdleConnections() for idle keep-alive sockets; then close the DB pool, Redis and queue workers; flush logs; exit.
- Add a force-exit timer below the grace period (e.g. 20 s) that exits with code 1 if cleanup hangs; call .unref() on it so it does not keep the process alive.
- In Docker, start Node directly (CMD ["node", "server.js"]) or use docker run --init / tini. npm start can swallow signals, and Node as PID 1 does not respond to SIGINT/SIGTERM by default.
- Liveness = the process is alive (restart it if not). Readiness = it can take traffic now (false during startup and shutdown).
- Rolling deploys depend on this to avoid 502s and half-finished requests.
The red pen
Common traps: mistake → fix
Mistake: Call process.exit() on SIGTERM
Fix: it drops in-flight requests and unflushed logs.
Mistake: server.close() kills open connections
Fix: it only stops new ones; keep-alive connections can keep it open, so close idle ones too.
Mistake: CMD npm start is fine in Docker
Fix: signals may never reach Node; run node directly.
Easy to hard
Questions interviewers ask, easy to hard
- What happens to in-flight requests when you deploy a new version?
- How do you implement graceful shutdown in Node?
- Liveness vs readiness probes?
- Why does our container take 10 seconds to stop and then get killed?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.