Processes, Clustering & Deployment
Child processes, clustering, process managers, graceful shutdown, packaging, and development runtime tooling.
Q54: How does the Cluster module work in Node.js?
The cluster module allows creating child processes (workers) that share server ports, enabling a multi-core system to handle higher application workloads.
- Clustering spawns multiple processes to utilize multi-core CPUs.
- Worker processes share the same networking ports.
How do cluster worker processes communicate with the primary process?
Q55: What is the relationship between Node.js core Cluster module and PM2?
PM2 wraps Node.js core cluster module under the hood, managing process lifecycles, health monitoring, load balancing, and dynamic scaling through simple configurations.
- PM2 builds upon the built-in Node.js cluster capabilities.
- Provides operational management, automatic restarts, and CLI controls.
What PM2 CLI flag activates cluster mode automatically for available CPU cores?
Q56: What is PM2 and why is it used in production Node.js deployment?
PM2 is a production process manager for Node.js. It handles automatic application restarts on failures, application clustering, zero-downtime reloads, and log management.
- Keeps Node.js applications alive continuously.
- Supports process clustering, log rotation, and zero-downtime restarts.
Which PM2 feature enables zero-downtime application updates?
Q57: How do you implement a graceful shutdown in a Node.js server?
Listen for termination signals (SIGTERM, SIGINT), stop accepting new incoming requests using server.close(), complete pending requests, close active database pools, and exit the process.
- Intercept process signals like
SIGTERMandSIGINT. - Close server connections and database client pools cleanly before exiting.
Which process termination signal is typically sent by orchestrators like Kubernetes to stop a container?
Q58: How do you gracefully disconnect a child process spawned via fork()?
When spawning child processes using child_process.fork(), an IPC channel is established. To cleanly disconnect without abruptly killing ongoing tasks, call child.disconnect(). This closes the IPC channel, allows pending events to drain, and permits the child process to exit naturally once its event loop completes.
child.disconnect()closes the Inter-Process Communication (IPC) channel cleanly.- Allows child processes to complete pending tasks and terminate without sending SIGKILL signals.
What happens when child.disconnect() is called on a IPC-connected child process?
Q59: What are Single Executable Applications (SEA) in Node.js?
Node.js supports bundling application code and assets directly into a single executable binary (SEA). Introduced natively in modern Node.js releases, SEA allows distributing Node.js applications as standalone executable binaries without requiring users to install the Node.js runtime environment separately on host targets.
- SEA packages JavaScript applications directly into standalone platform binaries.
- Eliminates external Node.js runtime installation dependencies on target machines.
What is the primary operational benefit of creating a Single Executable Application (SEA)?
Q60: How does Node.js native watch mode work and how does it replace Nodemon?
Node.js v18.11+ introduced a native file watching flag --watch. When started with node --watch app.js, Node.js monitors imported dependency files and automatically restarts the process whenever changes are saved, removing the need for external CLI dependencies like nodemon or pm2-dev during local development.
- Native
--watchflag monitors imported application files for changes automatically. - Eliminates external development dependencies like Nodemon.
Which built-in Node.js CLI flag enables native automatic process restarts on file changes?