Course contentsShow
← Node.js
Lesson 67 of 1,9801. Node.js Foundations and Runtime InternalsFree lesson

Child Processes and Inter-process Communication

Distribute workloads effectively using child processes and shared communication channels.

In this lesson, you will learn how to scale your application by offloading heavy tasks to separate processes and enabling them to exchange data.

Why use Child Processes?

Node.js runs on a single main thread. If you perform a CPU-intensive calculation (like heavy math or data processing), you block the Event Loop, causing your entire server to lag. By spawning a child process, you create a completely separate instance of Node.js that runs in parallel. This keeps your main process free to handle incoming requests while the child grinds away at the heavy work.

Communication Channels (IPC)

When you spawn a child using child_process.fork(), Node.js automatically sets up an Inter-process Communication (IPC) channel. This acts like a private messaging pipe between the parent and the child.

Because they exist in separate memory spaces, they cannot share variables. Instead, they "talk" by sending JSON-like messages.

A Simple Example

Imagine a main.js that needs to calculate a large prime number:

const { fork } = require('child_process');

// Spawn the child
const child = fork('./worker.js');

// Send data to the child
child.send({ number: 9999999999 });

// Listen for the result
child.on('message', (msg) => {
  console.log('Result from worker:', msg.result);
});

Inside worker.js, you handle the incoming data and respond back:

process.on('message', (data) => {
  const result = heavyCalculation(data.number);
  // Send the result back to the parent
  process.send({ result });
});

Effective Distribution

The goal is to keep your main process "light." Use the main process solely for orchestrating the flow—receiving requests and delegating work—while using child processes as "workers" to perform the actual computations. By distributing the load this way, you ensure that no single operation can stall your entire application, effectively utilizing your server's multi-core capabilities.

Key Takeaway: Use IPC messages to delegate CPU-intensive work to child processes, allowing your main event loop to remain responsive while the workload is handled in parallel.