Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 4 OF 6 · 2 MIN READ

Worker Threads, Cluster and Child Processes

Node.js gives you three ways to use more CPU: cluster runs several copies of your server, worker_threads run CPU-heavy JavaScript on other threads, and child_process runs other programs. Knowing which to use is a common scaling question.

What you will learn

  • Compare cluster, worker_threads and child_process
  • Offload CPU-heavy work without blocking requests
  • Know how PM2, containers and Kubernetes fit in

Key concepts

cluster

Forks several Node.js processes that share one port. Each has its own memory and event loop. In containers you usually run one process per container and let Kubernetes scale replicas instead.

worker_threads

Real threads inside one process, each with its own V8 isolate and event loop. They talk by message passing and can share memory with SharedArrayBuffer. Use a pool (Piscina) instead of creating a thread per request.

child_process

Starts another program (ffmpeg, a Python script) with spawn, exec or fork. Use spawn for streaming output; never pass user input into exec without escaping.

Step by step

Which one?

  • Use all CPU cores for HTTP traffic → cluster or more containers
  • Hash, resize, parse large data inside a request → worker thread pool
  • Run a non-JS tool → child_process.spawn
  • Long jobs (emails, reports) → a job queue and separate worker processes

Examples

A worker thread for a CPU-heavy task

// hash-worker.js
import { parentPort, workerData } from 'node:worker_threads'
import { pbkdf2Sync } from 'node:crypto'
parentPort.postMessage(pbkdf2Sync(workerData, 'salt', 600000, 64, 'sha512').toString('hex'))

// server.js
import { Worker } from 'node:worker_threads'
const hashInWorker = (pw) => new Promise((resolve, reject) => {
  const w = new Worker(new URL('./hash-worker.js', import.meta.url), { workerData: pw })
  w.once('message', resolve)
  w.once('error', reject)
})

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Move a slow image resize into a Piscina pool and compare response times.
  • Explain why in-memory sessions break with cluster.
  • Run ffmpeg with spawn and stream its progress to the client.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

Cluster or worker threads?

Cluster scales request handling across cores with separate processes and memory. Worker threads offload CPU work from one request while keeping the main loop responsive. They solve different problems and can be combined.

Why can in-memory state break when you scale out?

Each process or instance has its own memory, so a session, cache or rate-limit counter stored in one is invisible to the others. Move shared state to Redis or a database.

Summary

cluster or more containers for throughput, worker_threads for CPU work inside a request, child_process for other programs, and queues for long jobs. Keep shared state outside the process.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus