PHP-FPM vs Swoole


A few days ago, I saw some articles about swote. Today I’m here to join in the fun. The level is limited, the understanding of details may not be in place, welcome to help me to supplement and correct.


Early versions of PHP did not have a built-in web server, but provided SAPI (Server API) for third-party docking. Now the very popular PHP FPM is to handle the communication between PHP and the third-party Web server through fastcgi protocol. For example, the combination of nginx + PHP FPM, which runs in master / worker mode, starts a master process to listen for requests from nginx, and then forks multiple worker processes to process requests. Each worker process can only process one request. The life cycle of a single process is roughly as follows:

  1. Initialize the module.
  2. Initialization request. The request here means to request PHP execution code, not HTTP request.
  3. Execute the PHP script.
  4. End request.
  5. Turn off the module.

If you are interested in details, you can click here to see the picture

The multi process model relies on the number of processes to solve the concurrency problem,A process can handle only one connectionWhen a large number of processes are started, the process scheduling consumption may account for dozens or even 100% of the CPU. For example, for the C10K problem, the multi process model is unable to cope.


Swote also adopts the master / worker mode. The difference is that the master process has multiple reactor threads. The master is just an event generator, which is responsible for monitoring the changes of socket handle events. Worker runs as a multiprocess, receives requests from reactor threads, and executes callback functions (written in PHP). The process of starting the master process is as follows:

  1. Initialize the module.
  2. Initialization request. Since spoole needs to be run in the way of CLI, the global variables of PHP will not be initialized when the request is initialized, such as$_ SERVER, $_ POST, $_ Get, etc.
  3. Execute the PHP script. It includes lexical analysis, syntax analysis, initialization of variables, functions and classes. Master enters the listening state and does not end the process.

The principle of swote acceleration

  • Reactor (epoll’s IO reuse mode) is responsible for monitoring the event changes of socket handle to solve the high concurrency problem.
  • It can save the initialization time of PHP code by the way of memory resident. When using the bulky framework, the effect of swioole is very obvious.

The contrast is different


  • Master process / worker multi process mode.
  • Start the master and listen for requests from nginx transmission through fastcgi protocol.
  • There is only one connection for each worker process to execute the full PHP code.
  • After the PHP code is executed, all the memory occupied will be destroyed, and the next request needs to be initialized again.
  • Only for HTTP server.


  • Master process (composed of multiple reactor threads) / worker multi process (or multi thread) mode
  • Start the master, initialize the PHP code, and the reactor listens for the event changes of the socket handle.
  • The main thread of reactor is responsible for the balancing problem of sub multithreading, and the manager process manages the multi processes of worker, including the process of taskworker.
  • Each worker accepts the request from the reactor and only needs to execute the PHP code of the callback function part.
  • The PHP initialization code is executed only once when the master starts. The master enters the listening state and does not end the process.
  • It can be used not only for HTTP server, but also for TCP connection and websocket connection.

The above is mainly for the core operating machine production comparison, listing the differences, I think of these points for the moment, if there are missing key points, welcome to help me to add~