Ch. 6 · Node.js

Node.js Worker Threads and CPU Parallelism

Node.js Worker Threads and CPU Parallelism. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readintermediateupdated Oct 3, 2026

Worker threads run JavaScript in separate execution contexts and can parallelize CPU work. Messaging and serialization introduce overhead.

Before you start

You should know JavaScript promises, asynchronous errors and the distinction between a process and a request. When following a server example, identify the resource owner and the point where work completes. Try experiments locally with bounded input instead of assuming production traffic behaves like a single request.

The practical goal is to reason through this situation: A bounded pool processes image transformations while the main thread serves requests. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Estimate task granularity

A CPU-heavy operation must justify worker startup, messaging and data-transfer overhead.

Step 2: Bound worker concurrency

Use a pool with queue limits rather than spawning unlimited workers during traffic spikes.

Step 3: Handle worker outcomes

Associate responses with tasks and define timeout, failure and shutdown behavior without leaking queued work.

Worked scenario

A bounded pool processes image transformations while the main thread serves requests.

Image transformations occupy workers while the main event loop handles requests. If incoming work exceeds processing capacity, an unlimited queue still exhausts memory even though computation is off-thread. Reject, defer or shed excess work under a documented policy, and account for copying large image buffers when measuring benefits.

Common mistake

Creating a new worker per tiny task can cost more than the task itself.

Verify the behavior

Benchmark task time, transfer cost, queue delay and main-thread responsiveness. Kill a worker and verify its owned job gets a defined outcome.

Interview exercise

Decide whether to use a pool.

Answer and reasoning

Benchmark task size, transfer cost and queue delay; cap concurrency and handle worker failure.

Continue learning

Compare the scenario with the Node.js interview questions and test your understanding with the Node.js MCQs. For terminology and implementation details, consult the reference material.

More in Node.js

read ✓Node.js · hard

Node.js Clustering Across CPU Cores

Use cluster to run several workers on all cores, restart crashed workers, and understand shared-port and shared-state limits.

~2 min readread →
read ✓Node.js · hard

Node.js Password Hashing with scrypt

Store passwords as salted hashes with a slow key-derivation function, and compare candidates in constant time.

~2 min readread →
esc