thread_pool

The `thread_pool` directive configures a thread pool for handling asynchronous requests in NGINX.

Syntaxthread_pool name size [stack_size];
Defaultnone
Contextmain
Arguments2-3

Description

The thread_pool directive is used to define a thread pool that allows NGINX to offload the processing of asynchronous requests to threads, which can improve performance by making better use of multi-core processors. This directive accepts two to three arguments: the name of the thread pool, the size of the pool, and an optional parameter for the thread stack size. The parameters allow for fine-tuning the resources allocated for handling concurrent connection handling with threads, helping to balance workload distribution across server resources.

When the thread_pool directive is set, NGINX creates a specified number of threads in the pool that can be utilized for tasks that are I/O bound or require blocking operations. The threads are managed independently from the worker processes, allowing for efficient handling of operations without blocking the main event loop. This is particularly useful in scenarios such as Lua scripting, file uploads, or other long-running operations that would typically hinder performance if handled directly within a worker process. The optional stack size parameter defines the amount of stack memory allocated for each thread, which can be crucial when deep recursion or stack-heavy operations will be performed.

It is important to carefully manage the size of the thread pool and the number of worker processes since too many threads may lead to excessive context switching, diminishing returns in performance, and ultimately resource exhaustion.

Config Example

thread_pool my_pool 32;

Setting the thread pool size too high can lead to high context switching and decreased performance.

Not using proper thread synchronization mechanisms can lead to race conditions in shared resources.

← Back to all directives