Skip to content
tezvyn:
Menu

Offloading CPU-bound work with Worker Threads

Source: interviewMediumHow cards are made

Summary

knowing the single thread blocks on CPU work.

Key points

synchronous CPU work freezes the loop and all requests; offload to a Worker, communicate via messages or SharedArrayBuffer, use a pool.

Watch out for

suggesting async I/O fixes CPU blocking.

What's really being asked

The question separates candidates who understand that Node's asynchrony only helps I/O from those who think wrapping work in a Promise magically parallelizes it. CPU-bound code blocks the one JavaScript thread regardless of async syntax.

The full answer

Node runs your JavaScript on a single thread. A tight CPU loop, such as hashing, image resizing, or large JSON crunching, monopolizes that thread, so the event loop never advances to other phases. Every other in-flight request, timer, and I/O callback is frozen until the computation returns, which destroys throughput and latency. The correct fix is to move the work to a Worker Thread, which has its own V8 instance and event loop. The main thread posts input data with postMessage, the worker computes, and it posts results back. For shared numeric buffers you can use SharedArrayBuffer to avoid copying. In production you maintain a worker pool so spawning cost is amortized and concurrency is bounded.

The mistakes people make

Claiming async/await or wrapping the function in a Promise solves it, since neither yields the thread during synchronous computation. Suggesting the cluster module as the primary tool when the real need is sharing a result for one request, or spawning an unbounded worker per request, which exhausts memory.

What usually comes next

Worker pool sizing relative to CPU cores, the cost of structured cloning large payloads, when to prefer a separate microservice or child_process, and how SharedArrayBuffer plus Atomics avoids copies.

A concrete example

An endpoint generates a PDF that takes two seconds of pure CPU. Inline, those two seconds block all other users. Instead, a pool of four workers each handle one PDF job; the handler posts the document spec to an idle worker, awaits the result message, and responds. The main loop stays free to accept and route other requests throughout.

Interview question

Why does wrapping a heavy synchronous computation in an async function fail to keep a Node server responsive?

  • a.The computation is still synchronous and never yields the single JS threadCorrect
  • b.Promises lower the priority of CPU work in the loop
  • c.Async functions run on a separate OS thread automatically
  • d.The garbage collector preempts long Promises
Why?

async/await only helps when there is an awaited asynchronous boundary; a synchronous CPU loop still occupies the single thread, blocking the event loop. Worker Threads provide real parallelism.

Just read this? Test yourself on what you have been reading.

Read the original → nodejs.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on nodejs — each one lists the topics its interview covers.

See open roles