Node.js Worker Threads vs Child Processes: Memory & CPU trade‑offs

Last month we needed to parallelise a CPU‑heavy image‑processing pipeline in a Node 18 service that already runs in a container with 2 vCPU and 1 GiB RAM. The obvious options were worker threads or spawning child processes. I tried both and logged memory usage, context‑switch cost and error isolation.

Worker threads share the same V8 heap, so the overhead per thread is roughly a few MB for the thread stack plus the internal bookkeeping. This kept the total RSS around 350 MiB for four threads, but a single uncaught exception could bring down the whole process. Because the threads run in the same event loop, we had to guard shared state with Atomics or move all heavy work into pure functions to avoid race conditions.

Child processes start a fresh V8 instance. Each process consumed about 120 MiB at idle, so four workers pushed RSS to ~500 MiB. The upside was full isolation – a segfault in one worker didn’t affect the others – and we could restart a misbehaving worker without touching the main server. The downside was higher start‑up latency (≈30 ms) and the need to serialize messages via IPC, which added ~0.5 ms per 10 KB payload.

A minimal snippet that swaps the two approaches:

// worker thread
const { Worker } = require('worker_threads');
new Worker('./task.js');

// child process
const { fork } = require('child_process');
fork('./task.js');

Questions

  1. In a production Kubernetes pod, how have you balanced the memory budget when scaling to 8‑12 parallel workers?
  2. Have you observed any measurable CPU throttling differences when using threads versus processes under heavy load?
Backend#Node.js#Backend
0

0 Answers

Post Your Answer

Use ``` code ``` for syntax highlighting