Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we reload one of the PHP-FPM pool without disturbing others

Tags:

php

I've multiple PHP-FPM UNIX socket pools for the same host to have logical separation of codebase / functionality & to address future scaling of the same. Nginx manages the routing to the right socket based on URI patterns. Deployment is working fine.

Whenever I change pool configuration for any one, I am reloading / restarting the FPM process (by USR2 signal).

I don't have any idea about how the internals of FPM work but I assume that as I restart the main process, all pools get restarted / reloaded. Please correct me if I'm wrong.

I want to know if I could reload / restart only one pool when others work as they were (no issues in the undergoing transactions on those pools).

I would also appreciate any other configuration suggestions which could allow me to have desired pool management

like image 759
kaychaks Avatar asked Jun 03 '13 06:06

kaychaks


People also ask

What is pool in PHP-FPM?

Each site is also deployed with a PHP-FPM resource pool, which is owned by the site user. This prevents PHP scripts from reading or modifying files outside of the current site's root directory. Meaning, if a malicious user were to gain access to a site on your server, they would be unable to infect other sites.

What is PHP-FPM service?

PHP-FPM (FastCGI Process Manager) is an alternative to FastCGI implementation of PHP with some additional features useful for sites with high traffic. It is the preferred method of processing PHP pages with NGINX and is faster than traditional CGI based methods such as SUPHP or mod_php for running a PHP script.


2 Answers

php-fpm allows for a graceful restart of childs, usually with the reload keyword instead of restart on the init script, sending USR2 signal.

So by doing a graceful restart you should not loose any running transaction. The children are killed after the end of the current request management for each of them. This should be enough if you do not need a real restart. I made some tests and for example a reload is enough to :

  • empty the APC cache
  • alter log file path
  • alter min/max/start child settings

So I did not find a case where a need a real restart yet. Except that a reload cannot start a stopped service.

If you want to ensure other pools will never be reloaded when you want to reload one of them you will have to manage several php-fpm daemons and one pool per daemon. This implies writing several init scripts and master configuration files.

Using the restart keyword is more dangerous, especially because the init script is maybe killing long running children in the stop step. And with several daemons managed with several PID and configuration files you could even get a start-stop-daemon command with --exec option (that's the case in debian) and this would kill all the daemons running the same php-fpm executable (effectively sending a kill -9 to all the other parallel php-fpm daemons after stopping the right one with the right PID if you run several php-fpm processes, which is very bad).

So using the reload keyword (USR2 signal) is a must.

like image 168
regilero Avatar answered Sep 21 '22 13:09

regilero


Although there is already a best answer, I'm writing to provide more information missed from the best one.

  • After reload is executed, PHP-FPM will wait until all requests are processed but not longer than process_control_timeout. If it reaches process_control_timeout, a 502 error will occur
  • While waiting for all requests processed, PHP-FPM WILL NOT process any new requests, instead, it queues them. Because a new reloaded process will only be created after all old requests are processed.

which leads to some facts and issues:

  • When speaking zero downtime, does delayed processing count?
  • How many requests the server would be able to queue?
  • Of course, delayed processing means the page would get stuck on the user side.
like image 23
Ray Avatar answered Sep 23 '22 13:09

Ray