Requests Concentrated in Certain Processes

Phenomenon

Sometimes, when we run the command php start.php status, we see that requests are being handled by specific processes, while other processes remain completely idle.

Preemption Mechanism

The way multiple processes in Workerman acquire connections is by default preemptive. This means that when a client initiates a connection, all idle processes have a chance to acquire that connection, with the faster one winning. Ultimately, who is faster is determined by the operating system's kernel scheduling. The operating system may prioritize the process that was most recently used to gain CPU access, because there may still be context information of the previous process in the CPU registers, which can reduce the overhead of context switching. Thus, when the business logic is fast enough or during stress tests, it is more likely for connections to be concentrated in certain processes, because this strategy can avoid frequent process switching, often leading to optimal performance, and is not necessarily a problem.

Polling Mechanism

Workerman can change the way of acquiring connections to a polling method by setting $worker->reusePort = true;. In the polling method, the kernel distributes connections in a nearly equal manner among all processes, so that all processes will handle requests together.

Misconception

Many developers believe that having all processes participate in request handling improves performance, but this is not necessarily true. When the business logic is simple enough, the number of processes handling requests should approach the number of CPU cores for optimal server throughput. For instance, on a 4-core server, setting the number of processes to 4 generally results in the highest QPS for a helloworld stress test. If the number of processes handling requests exceeds the number of CPU cores by too much, the overhead of process context increases, which can actually degrade performance. For typical database-driven applications, setting the number of processes to 3 to 6 times the number of CPU cores may yield better performance.