48

I have a CentOS server. System is nginx/php-fpm. It has 16GB RAM. CPUs : 8

CPU Frequency: 2660.203 MHz

Why am I getting this error in my error log?

php-fpm/error.log:

[02-Aug-2014 17:14:04] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 0 idle, and 21 total children

This is my php-fpm configuration for the www pool:

php-fpm/www.conf:

pm = dynamic

pm.max_children = 32768

pm.start_servers = 10

pm.min_spare_servers = 10

pm.max_spare_servers = 10

pm.max_requests = 5000

How to fix the problem?

Community
  • 1
  • 1
Çağrı Can Bozkurt
  • 653
  • 1
  • 10
  • 17
  • 8
    lol at people who don't understand what his question is. It's in bold and has a question mark at the end of it. – Dan P. Sep 08 '17 at 11:23

1 Answers1

150

It is a tough cookie because there could be numerous factors involved. The first problem with your config is the max_children is ridiculously high. If each child process is using 50MB then 50 x 32768 would easily deplete 16GB.

A better way to determine max_children is to find out how much each child process uses, then factor in the maximum RAM you would like php-fpm to use and then divide the values. E.g. If I have a 16GB server, I can run the following command to determine how much ram each php-fpm child consumes:

ps -ylC php-fpm --sort:rss

Note! It may be required to explicitly specify user if php-fpm is running under the different one.

ps -ylC php-fpm --sort:rss -u www-data

where www-data is the user under which php-fpm is being run.

You are on the lookout for the RSS column; it states resident memory and is measured in KB. If I have an average of 50MB per process and I want to use a maximum of 10GB for php-fpm processes, then all I do is 10000MB \ 50MB = 200. So, on that basis, I can use 200 children for my stated memory consumption.

Now, with regards to the servers, you will want to set the max_spare_servers to x2 or x4 the number of cores. So if you have an 8 core CPU then you can start off with a value of 16 for max_spare_servers and go up to 32.

The start_servers value should be around half of the max_spare_servers value.

You should also consider dropping the max_requests to around 500.

Also, in addition to dynamic, the pm value can also be set to static or ondemand.

  • static will always have a fixed number of servers running at any given time. This is good if you have a consistent amount of users or you want to guarantee you don't breach the max memory.
  • ondemand will only start processes when there is a need for them. The downside is obviously having to constantly start/kill processes which will usually translate into a very slight delay in request handling. The upside, you only use resources when you need them.
  • dynamic always starts X amount of servers specified in the start_servers option and creates additional processes on an as-need basis.

If you are still experiencing issues with memory then consider changing pm to ondemand.

This is a general guideline, your settings may need further tweaking. It is really a case of playing with the settings and running benchmarks for maximum performance and optimal resource usage. It is somewhat tedious but it is the best way to determine these types of settings because each setup is different.

Eje
  • 354
  • 4
  • 8
Rijndael
  • 3,683
  • 2
  • 24
  • 26
  • i didn't know why max_requests should be 500? in my case, i want my server serves up to 5000 CCU – crz Jul 13 '16 at 04:06
  • 24
    max_requests are not about how many concurrent requests can be processed. It means that after php child process 500 requests (one at the time), it is killed and then respawned as a fresh new unused process. It helps to fight memory leaks. – Ján Stibila Dec 08 '17 at 11:00
  • 4
    This is a good explanation, but the RSS value in the ps command will include memory used by loaded shared libraries (https://stackoverflow.com/questions/7880784/what-is-rss-and-vsz-in-linux-memory-management). In testing, I'm finding that where max_children should be 50 based on the above, I'm able to set this to 150 with no problems. I'd say it's a setting to tweak, not to set using a fixed formula. – markdwhite Aug 16 '18 at 05:26
  • 2
    `pm.max_requests` according to [php.net](https://www.php.net/manual/en/install.fpm.configuration.php#pm.max-requests): _The number of requests each child process should execute before respawning. This can be useful to work around memory leaks in 3rd party libraries. For endless request processing specify '0'. Equivalent to `PHP_FCGI_MAX_REQUESTS`. Default value: 0._ – Eje Mar 22 '20 at 09:02
  • Having ```pm = ondamand``` I think, ```pm.max_requests``` does not make any sense. – Terry Sep 20 '20 at 03:35
  • 1
    Are you sure the explanation for max_spare_servers and start_servers is correct? Despite the name, documentation states that this is about "children" as well. These settings should probably scale proportionally with max_children and not be based on number of cores? – marcovtwout Jul 23 '21 at 08:55
  • 1
    I ran the command but it always returns empty results. What am I doing wrong? – LTroya Dec 10 '21 at 15:52