Adjusting Number of Concurrent Threads
Principles
When the program changes from single-thread to multi-thread, the CPU and memory resources are fully used and the performance is improved. However, the system performance does not linearly improve with the increase of the number of threads. As the number of threads increases, scheduling between threads, context switching, and competition for key resources and locks also cause high overheads. When resource contention is severe, the performance may deteriorate significantly. The following figure shows the TPS of different concurrent threads in a service scenario. After the number of concurrent threads reaches 128, the performance reaches the peak and then decreases. We need to perform multiple tests based on different service models and application scenarios to find the optimal number of concurrent threads for the service scenario.

Modification Method
Different software has different configurations and needs to be modified based on code implementation. The following describes how to modify common open-source software:
- For MySQL, you can set the maximum number of concurrent working threads through innodb_thread_concurrency.
- For Nginx, you can set the number of concurrent processes through worker_processes.