Comparison of php-fpm and spool core operating mechanism


A few days ago, I saw several articles about spool. Today, I’ll join in the fun. Welcome to help me add, correct.


The early version of PHP did not have a built-in web server, but provided SAPI (Server API) for the third party to do docking. Now very popular PHP FPM is to deal with 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 as follows:

  1. Initialize the module.
  2. Initialize the request. The request here means to request PHP to execute code, not http.
  3. Execute the PHP script.
  4. End request.
  5. Close the module.

Interested in details can click here to see the picture

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


Swoole 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. The worker runs in a multi process mode, receives requests from the reactor thread, and executes callback functions (written in PHP). The process of starting the master process is as follows:

  1. Initialize the module.
  2. Initialize the request. Because spool needs to run in the way of CLI, when initializing the request, PHP global variables will not be initialized, such as$_ SERVER, $_ POST, $_ Get, etc.
  3. Execute the PHP script. Including morphology, syntax analysis, initialization of variables, functions, classes, etc., master enters the listening state and does not end the process.

The principle of spool acceleration

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

Contrast different


  • Master main process / worker multi process mode.
  • Start the master and listen for the request from nginx transmission through fastcgi protocol.
  • Each worker process corresponds to only one connection, which is used to execute the complete PHP code.
  • After the PHP code is executed, all the memory occupied will be destroyed, and the next request needs to be initialized again.
  • For HTTP server only.


  • Master main process (composed of multiple reactor threads) / worker multi process (or multi thread) mode
  • Start the master, initialize the PHP code, and the reactor listens to the change of the socket handle.
  • Reactor main process is responsible for the balance of sub multithreading, and manager process manages worker multi process, including taskworker process.
  • 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 only executed once when the master starts, and the master enters the listening state, and the process will not end.
  • It can not only be used for HTTP server, but also can establish TCP connection and websocket connection.

The above is mainly for the comparison of the production of the core operating machine, and the differences listed are different. For the moment, I think of these points. If there are some missing points, you are welcome to supplement them for me

More learning content can be accessedAs long as you can read it, your salary will go up a step

The above contents hope to help youA lot of PHPer always encounter some problems and bottlenecks when they are upgrading. They write too much business code and have no sense of direction. They don’t know where to start to improve. For this, I have sorted out some materials, including but not limited to:Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, tp6, laravel, yii2, redis, spool, swoft, Kafka, MySQL optimization, shell script, docker, microservice, nginxAnd so on many knowledge points advanced dry goods need can be free to share with you, need can click here for more learning materials detailsAdvanced PHP monthly salary 30K > > > architect growth path [free access to videos and interview documents]

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]