child_process.fork() and cluster worker lifecycle
Inter-process communication (IPC) allows independent Node processes to exchange structured messages. `child_process.fork()` starts another Node module with an IPC channel. The cluster module builds on process-based workers and can distribute incoming server connections among them. Unlike worker threads, cluster workers have separate process memory. Cluster uses a primary process and workers, and connection scheduling depends on configuration and platform. Do not use a process-local Map as authoritative state for multiple workers. Use appropriate shared persistence or explicit IPC protocols. Each message needs a type, correlation identifier and validated payload where applicable. Sending a message does not guarantee durable application processing. Handle channel disconnect, worker exit, pending requests and bounded restarts. Cluster is not a cross-host orchestrator. Do not assume every worker sees another worker’s in-memory updates. Avoid treating IPC transport success as business acknowledgment or automatically respawning a crashing worker forever. Sources: https://nodejs.org/download/release/v22.16.0/docs/api/child_process.html, https://nodejs.org/download/release/v22.16.0/docs/api/cluster.html.
Formal Syntax & Structure
fork(modulePath[, args][, options]); cluster.fork([env])Working Example
import {fork} from 'node:child_process';import {once} from 'node:events';const child=fork('task.mjs',[],{stdio:['ignore','ignore','ignore','ipc'],env:{LANG:'C.UTF-8'}});const exit=once(child,'exit');const timer=setTimeout(()=>child.kill(),1500);try {const reply=once(child,'message');child.send({id:7,quantity:3});const [message]=await reply;console.log(message.id,message.total);console.log('exit',(await exit)[0]);}finally{clearTimeout(timer);if(child.exitCode===null&&child.signalCode===null)child.kill();}