IPC and cluster concepts
Prerequisites: previous curriculum lessons on Node streams, HTTP, buffers and asynchronous errors. Local lab baseline: Node.js 22.16.0, Linux, ESM. Use a disposable directory. Static examples are not ByteMind interactive execution. Each example is independent; save the named files together and run node main.mjs. The author harness imposes a five-second process deadline.
How it works
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.
Exchange one structured IPC request and response
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();}process.once('message',m=>{process.send({id:m.id,total:m.quantity*4},()=>process.disconnect());});Expected output:
7 12exit 0The child sends a correlated result and disconnects its IPC channel before exiting.
Observe separate state in one real cluster worker
import cluster from 'node:cluster';import {once} from 'node:events';let count=0;if(cluster.isPrimary){ count=5;const worker=cluster.fork();const exit=once(worker,'exit');const timer=setTimeout(()=>worker.kill(),1500); try {const [message]=await once(worker,'message');console.log('primary',count,'worker',message.count);await exit;} finally{clearTimeout(timer);if(!worker.isDead())worker.kill();}}else{count++;process.send({count},()=>process.disconnect());}Expected output:
primary 5 worker 1The cluster worker starts its own copy of module state. This demonstrates separate memory, not load balancing.
Common mistakes
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.
Practice
Add a correlation ID to a toy message and explain how a parent should react when the worker exits before replying.
Further reading
Official documentation: child_process, cluster. Internal reference candidate: node-ipc-and-cluster-concepts-lookup (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
IPC and cluster concepts — knowledge check
Apply the lesson to a fresh scenario and explain the resource or protocol boundary.
Can cluster workers share an ordinary Map by default?
What should the parent do if a worker exits before a reply?
What does the cluster fixture demonstrate?