Home

Thread Pool Configuration Best Practices

Li

Li Wei

August 3, 20252 min read

Title: Thread Pool Configuration Best Practices

Thread Count Configuration (Maximum Thread Count Calculation)

Calculate based on the average execution time of a thread, ignoring context‑switch overhead:

Thread count (minimum setting):
threads = QPS / (1000 ms / **average** execution time)

Thread count (recommended setting):
threads = QPS / (1000 ms / **maximum** execution time)

Example: If the average execution time is 20 ms, then 1000 / 20 = 50, meaning a single thread can ideally handle 50 requests per second. If the thread pool needs to process 800 QPS, you need at least 800 / 50 = 16 threads.

Recommendation: When the thread count calculated with the maximum execution time ( threads = QPS / (1000 ms / maximum execution time) ) yields, say, 100 threads and the machine’s resources can support it, use that value.

Queue Selection

Synchronous queue: Suitable when the number of tasks is relatively large but traffic is fairly stable—for example, QPS fluctuates between 800 and 1000 and the thread count is sufficient. In this case, a synchronous queue is recommended.

Blocking queue: Suitable when the task volume is unpredictable and varies widely—for example, QPS fluctuates between 10 and 1000. A blocking queue works well here.

Recommendation:

  • High traffic with enough threads → use a synchronous queue.
  • Low traffic with enough threads → use a blocking queue.

The blocking‑queue capacity should be greater than max(200, threads × 3). If task volume varies a lot, configuring the blocking queue with 1,000+ slots is normal.

Notes

Note 1: The queue length must be larger than the maximum thread count; a factor of three is typical. For example, if the max thread count is 100, set the queue length to at least 300.


Originally written by Li Wei (李唯_) and published in Chinese on 后端技术栈全书 (Full-Stack Backend Engineering). Translated and adapted for DriftSeas with permission.

Keep reading

More related articles from DriftSeas.