QA
Intermediate LevelNode JS

How do you handle SIGTERM signals in Kubernetes pods to drain connections before Node.js container termination?

❓ Interview Prompt: On receiving SIGTERM, immediately fail Kubernetes readiness probes, call server.close() to stop accepting new TCP connections, wait for active requests to finish (with a 30s timeout), close DB pools, then process.exit(0).

When Kubernetes terminates a Pod, it sends a 'SIGTERM' signal before forcing 'SIGKILL' after a grace period (default 30s):

Graceful Shutdown Steps:

1
Intercept 'process.on('SIGTERM')'.
2
Fail readiness health check probes immediately.
3
Call 'server.close()' to stop accepting new TCP connections while draining active in-flight HTTP requests.
4
Close database connection pools cleanly ('mongoose.disconnect()').
5
Exit 'process.exit(0)'.

Code Implementation Example

javascript
import http from 'node:http';

const server = http.createServer((req, res) => res.end('OK'));
server.listen(3000);

process.on('SIGTERM', () => {
  console.log('SIGTERM received. Closing HTTP server connections...');
  server.close(() => {
    console.log('HTTP server closed. Exiting process.');
    process.exit(0);
  });
});
💡Handling SIGTERM signals for graceful HTTP connection draining.
🎯 Interview Key Takeaway

Intercept SIGTERM signals to close servers cleanly and drain active connections before container termination.

#SIGTERM#Graceful Shutdown#Kubernetes#Intermediate
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?