user3695826 user3695826 - 2 months ago 38
PHP Question

WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning

I have a centos server. System is nginx/php-fpm. has 16gb ram.
cpus : 8

cpu mhz : 2660.203

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

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

Answer

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 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 look out 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 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 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.