How Many Processes Should Be Started

How to Set the Number of Processes

The number of processes is determined by the count attribute (process count setting is not supported on Windows systems), for example, the following code:

use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';

$http_worker = new Worker("http://0.0.0.0:2345");

// ## Start 4 processes to provide services ##
$http_worker->count = 4;

...

Factors to Consider When Setting the Number of Processes

  1. Number of CPU cores

  2. Amount of memory

  3. Whether the business is I/O intensive or CPU intensive

Principles for Setting the Number of Processes

  1. The total memory usage of each process must be less than the total memory (generally, each business process occupies about 40M of memory).

  2. If it is I/O intensive, meaning that the business involves some blocking I/O, such as general access to MySQL, Redis, or other storage, which are blocking accesses, the number of processes can be increased, for example, configured to 3 times the number of CPU cores. If the business involves a lot of blocking waits, the number of processes can be further increased, such as 8 times the number of CPU cores or even higher. Note that non-blocking I/O is CPU intensive, not I/O intensive.

  3. If it is CPU intensive, meaning that there are no blocking I/O overheads in the business, for example, using asynchronous I/O to read network resources without being blocked by the business code, the number of processes can be set to be the same as the number of CPU cores.

Reference Values for Setting the Number of Processes

If the business code is I/O intensive, set the number of processes based on the degree of I/O intensity, for example, 3 to 8 times the number of CPU cores.

If the business code is CPU intensive, the number of processes can be set to the number of CPU cores.

Note

Workerman's own I/O operations are non-blocking, for example, Connection->send is non-blocking and is considered a CPU-intensive operation. If you are unsure about which type your business leans towards, you can set the number of processes to about three times the number of CPU cores. Additionally, having too many processes is not necessarily better; if there are too many processes, the overhead of process switching will increase, which can impact performance.