Thread Pool
Maintain a fixed set of reusable worker threads that pick up tasks from a queue, avoiding the overhead of spawning a new thread per task.
errorWhat Problem Does the Thread Pool Pattern Solve?
Creating and destroying a thread for every incoming request is expensive — thread creation involves OS-level syscalls, stack allocation, and context-switch overhead. Under high load, unbounded thread creation exhausts system resources and degrades performance or causes crashes.
check_circleHow the Thread Pool Pattern Works
Pre-allocate a fixed pool of worker threads at startup. Incoming tasks are placed in a work queue; idle threads pull tasks from the queue and execute them. When all threads are busy, tasks wait in the queue, providing natural load levelling and a hard cap on concurrency.
Thread Pool Pattern Architecture
Rendering diagram...
Implementation by Language
Thread Pool Pattern in the Real World
“A hotel concierge desk staffed by three concierges represents the thread pool. No matter how many guests check in, only three requests are handled simultaneously. Other guests wait in the lobby queue. When a concierge finishes, they immediately assist the next waiting guest — the staff are never created or dismissed per guest, they simply stay on duty.”
Frequently Asked Questions
helpHow do I choose the right thread pool size?
For CPU-bound tasks, use the number of CPU cores. For I/O-bound tasks, use a larger pool (2-4x cores) since threads spend time waiting. In Node.js, the libuv thread pool defaults to 4 threads (UV_THREADPOOL_SIZE). Profile under realistic load to find the optimal size.
helpDoes JavaScript need thread pools given its single-threaded nature?
JavaScript's main thread is single-threaded, but Worker threads (Node.js) and Web Workers (browser) enable true parallelism. Node.js uses a thread pool internally for file I/O and DNS lookups. For CPU-intensive tasks, worker_threads with a pool gives JavaScript multi-core utilization.