How many PHP children is too many?

For a particular site I have two Linodes. One that runs NginX and PHP and the other is for the database. Now that I have separate Linodes, I'm wondering if I should crank up the # of PHP children since I have extra CPU/Memory available.

But I'm thinking of leaving the # of children alone, since the real bottleneck is the database. And I'm thinking MySQL won't necessarily perform better if more children are opening more connections.

So how many PHP children is too many? Or do you think it's best to have as many PHP children as I can fit into memory?

6 Replies

Configure 'status' in the php-fpm pool config file. Then you can get a current status report in your browser.

One item in the status report is 'max children reached'. If you see that number incrementing then you may want to increase the number of children.

My rule of thumb is 1.5n to 2.0n children, where n is the number of cores in the system, but I haven't based that on anything in particular as much as gut feeling and "seems to work well for me".

@Guspaz:

My rule of thumb is 1.5n to 2.0n children, where n is the number of cores in the system, but I haven't based that on anything in particular as much as gut feeling and "seems to work well for me".

Great, I have about that many. I'll leave it alone then.

Not exactly sure how many cores I have but I'm guessing it's either 4 or 8.

@sleddog I'll keep that in mind but I'm actually using spawn on this server. (If it ain't broke…)

New linodes should have 8 cores. I'd suggest somewhere between 8 and 16 children is probably appropriate, but it really depends on load. If all you've got with PHP is a few little scripts that execute quickly and are seldom used (or only internally used), then even 8 might be overkill. On the other hand, if you've got lots of long-running streaming ajax scripts that dedicate a PHP process to a single user for minutes or hours on end, then 16 processes won't be anywhere near enough.

Yeah, agreed.

I guess general rule is that if your site is sometimes getting non-responsive because all PHP handlers are busy waiting (not as much if busy processing), and you have RAM for it… add more.

I have been running 20 PHP children on a pre-upgrade 4-core Li512, because some of them are usually just stuck waiting for the DB, or for a http transfer to complete.

The number of children should be primarily based on amount of RAM you have rather than CPU cores, otherwise you'll end up swapping, and no amount of CPUs will help you there.

To work it out is pretty simple. When running your application under "normal" load:

ps -ylC php5-fpm –sort:rss

RSS column will show non-swapped physical memory usage by php5-fpm processes in KB. So, best value for pm.max_children can be calculated as:

pm.max_children = Total RAM dedicated to the web server / Max child process size. In my case it was around 50MB per process, and if I wanted to leave 768MB for php5-fpm:

pm.max_children = 768MB / 50MB = 15.

pm.start, min etc just requires tweaking. Keep in mind you'll need to redo this with any major application (php) upgrade, php config change (xcache settings etc).

If you want to test high load: https://www.blitz.io is pretty good. Keep an eye on your httpd/php5-fpm error logs during the test to see where limits are reached.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct