Building a Reusable Worker Pool for Throughput
Design a task-queue-backed worker pool that recycles threads to maximize CPU utilization under load.
Why a Worker Pool?
Node.js runs your JavaScript on a single event-loop thread. That is great for I/O, but a CPU-bound task (hashing, image resizing, parsing, compression) blocks the loop and stalls every other request.
The worker_threads module lets you run JavaScript on separate OS threads. But spawning a brand-new Worker for every task is wasteful: thread startup costs tens of milliseconds and memory.
- Goal: create a fixed set of long-lived workers once.
- Recycle them across many tasks via a queue.
- Maximize throughput by keeping every CPU core busy.
That recycled, queue-backed set of workers is a worker pool.
The Blocking Problem
Before building the pool, feel the pain. A synchronous CPU loop on the main thread freezes everything: timers, HTTP responses, even a simple setInterval heartbeat.
Run this and watch the heartbeat go silent while fib(42) burns the CPU.
function fib(n) {
return n < 2 ? n : fib(n - 1) + fib(n - 2);
}
let ticks = 0;
const timer = setInterval(() => {
console.log('heartbeat', ++ticks);
if (ticks >= 3) clearInterval(timer);
}, 50);
console.log('start blocking work');
console.log('fib(38) =', fib(38)); // blocks the event loop
console.log('done blocking work');All lessons in this course
- Why the Event Loop Stalls on CPU-Bound Work
- Spawning Worker Threads and Passing Messages
- Sharing Memory with SharedArrayBuffer and Atomics
- Building a Reusable Worker Pool for Throughput