Node.js › Clustering & Worker Threads

What is a worker thread pool, and why would you build one rather than creating a new Worker for every task?

Advanced

Answer

Creating a new Worker has meaningful startup overhead -- a worker pool pre-creates a fixed number of threads once at startup and reuses them across many tasks, queuing work when all are busy, amortizing thread-creation cost across many tasks.
const pool = new Piscina({ filename: './cpu-task.js' });
app.post('/process', async (req, res) => {
  const result = await pool.run(req.body);
  res.json(result);
});
Real-world example An endpoint performing CPU-intensive PDF generation uses a Piscina pool of four pre-warmed workers rather than spawning a new Worker per request, eliminating a per-request latency spike.

Common follow-ups: How do you size a worker pool relative to available CPU cores?;What happens to queued tasks if every worker is busy when a new request arrives?