The CPU Blocking Threat
Because Node.js executes application code on a single thread, running CPU-heavy calculations (like calculating cryptographic hashes, processing images, or parsing massive JSON arrays) blocks the event loop.
While the event loop is blocked, the server cannot process incoming HTTP requests, degrading performance.
Offloading CPU-heavy tasks to Child Processes maintains event loop responsiveness.
Performance Rule: Keep the main event loop thread free for lightweight routing. Offload all intensive computations to child processes.
Spawn, Exec, and Fork Methods
Node.js provides three methods in the child_process module:
- ◆`spawn()`: Streams data from a process in chunks, ideal for long-running CLI utilities.
- ◆`exec()`: Runs a command and buffers output, suitable for fast, lightweight commands.
- ◆`fork()`: Spawns a new Node.js instance, establishing a dedicated inter-process communication (IPC) channel.
Implementing a Forked Calculation
// Main routing thread in Node.js
const { fork } = require('child_process');
app.get('/api/calculate-reports', (req, res) => {
// Spawn background worker thread
const worker = fork('./report-worker.js');
worker.send({ action: 'start', data: req.query });
// Receive output asynchronously without blocking loop
worker.on('message', (result) => {
res.json(result);
worker.kill(); // Terminate worker process
});
});By scaling processes horizontally, Node.js backends run high-throughput systems without blocking page requests.