Interview frequency: …
Short answer
Intercept the signal, stop accepting new requests through server.close(), wait for in-flight requests to finish, close the database connections, then exit with process.exit(0).
Explanation
In cloud container environments, the system sends a SIGTERM signal to give the app a chance to shut down cleanly. The correct steps include stopping the server, setting a maximum timeout in case any requests hang, closing database connection pools and WebSocket sockets, then exiting with the success code 0.
Code example
process.on("SIGTERM", async () => {
server.close(async () => {
await db.close();
process.exit(0);
});
});Common mistakes
- Not setting a forced timeout in case there is a stuck request or an endlessly open connection.