liveBook · Manning

Thread pool

As explained in JavaScript in Depth

A managed collection of worker threads that execute operations concurrently on behalf of a runtime or library.

An asynchronous operation can look slow even when its underlying work is fast. When Node.js has no available thread for a filesystem, DNS, cryptographic, or compression operation, the request waits in a queue before execution begins. The default pool contains four threads, and the pool is shared across these operation classes. section 11.1.10

JavaScript still runs on one main thread, but libuv dispatches selected work to background threads. Consequently, operating-system CPU usage combines event-loop activity, V8 background work, cryptography, DNS, filesystem operations, and other pool work; it does not directly measure event-loop capacity. section 11.4.1

Use these symptoms to distinguish pool saturation from ordinary I/O latency:

  • Compare DNS lookup duration with the expected cached result; unusually high lookup latency can indicate that DNS work is waiting for a pool thread. section 11.1.10
  • Inspect uvMetricsInfo: loopCount tracks event-loop iterations, events counts processed events, and eventsWaiting reports currently waiting events. section 11.1.10
  • Do not scale infrastructure from aggregate CPU alone: pool-heavy work can leave the event loop available, while one busy event-loop core can block requests despite low total CPU. section 11.4.1
const { performance } = require('node:perf_hooks');
setImmediate(() => {
  console.log(performance.nodeTiming.uvMetricsInfo);
});
{
  loopCount: 1,
  events: 2,
  eventsWaiting: 0
}

Reads event-loop and pending-event metrics through performance.nodeTiming.uvMetricsInfo. section 11.1.10

const fs = require('node:fs');
 
setInterval(() => {
  const start = performance.now();
  fs.stat(__filename, () => {
    const elapsed = performance.now() - start;
    if (elapsed > 10) {
      console.log(`Thread pool delay: ${elapsed.toFixed(1)} ms`);
    }
  });
}, 1000);

Measures delayed fs.stat callbacks as a practical signal of thread-pool delay. section 11.1.10

How JavaScript in Depth covers it

How deep it goes
Explains shared workers, queueing, diagnostic metrics, and misleading symptoms without detailing libuv’s scheduling implementation.
Examples
Uses DNS lookup latency, fs.stat timing, uvMetricsInfo output, and HTTPS cryptographic work as diagnostic examples.
What it assumes
Assumes Node.js asynchronous APIs and the event loop, while distinguishing JavaScript’s main thread from runtime background threads.
What you can do after
Diagnose/debug with uvMetricsInfo, callback timing, DNS latency, and event-loop-versus-pool CPU interpretation.
How it connects
Connects thread pools to the event loop, asynchronous I/O, DNS, filesystem operations, cryptography, compression, V8, and CPU utilization.

Also explained in

Compare how 13 books explain thread pool →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.