Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP5-FPM static, dynamic or on demand?

Tags:

php

nginx

I have a Nginx + PHP5-FPM server with few high traffic websites.

From my understanding of PHP5-FPM pools config, I understood that:

static = can be used to immediately create N child processes so they do not need to be opened/re-opened, they are already opened and can be used when needed, else they are "sleeping".

dynamic = can be used to open a limited number of child processes and re-spawn then when a limit is reached (min/max servers).

ondemand = I specify the max number of child processes to create, and then child processes are created on demand, when needed, and closed when not needed anymore, maintaining a low memory usage but increasing the response time of few milliseconds.

From my tests with a high traffic WordPress website, I noticed that:

If I use "static", the website is for sure faster and can handle immediately high number of concurrent connections, but the memory always increases its usage, and after N hours it seems to use almost the total RAM available. So I have to use a cronjob to periodically (every 1 hour) reload PHP5-FPM with /etc/init.d/php5-fpm reload.

If I use "dynamic" it uses less RAM but after N concurrent connections there are frequent 502 errors (but maybe I configured it not well).

If I use "ondemand" the site is a little slower (like +50/100ms response time), but it can handle all the high traffic without using too much RAM.

So my personal conclusion would be that "ondemand" is really the best method to use in terms of low/controlled memory usage, the only downside is the +50/100 ms in response time but in my case it is not a big problem.

Are my assumptions correct ?

like image 287
user2972081 Avatar asked Mar 17 '15 22:03

user2972081


People also ask

How does PHP-FPM work?

As PHP-FPM receives a proxied connection, a free PHP-FPM worker accepts the web server's request. PHP-FPM then compiles and executes the PHP script, sending the output back to the web server. Once a PHP-FPM worker finishes handling a request, the system releases the worker and waits for new requests.

What user does PHP-FPM run as?

By default the web server and php-fpm runs with the user called www-data.

Is PHP-FPM better?

Conclusion. PHP-FPM is an efficient method on how to minimize the memory consumption and rise the performance for the websites with heavy traffic. It is significantly faster than traditional CGI-based methods in multi-user PHP environments.

What is PHP-FPM pool?

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

You didn't mention WHY you would want to keep memory low. Assuming this machine is dedicated to serving PHP-FPM, keeping memory low doesn't help your application in anyway. You have memory, use it.

Therefore, in this case, "static" is the best choice, with max_requests set to something that will keep memory leaks (if you have any) under control.

If this machine is shared with other tasks, then keeping memory low is ideal. In this case, "dynamic" is the best compromise between speed and memory usage.

"ondemand" is a good choice only when the PHP-FPM engine will be used infrequently and the machine's primary purpose is something else.

like image 82
Daniel Houlihan Avatar answered Sep 21 '22 16:09

Daniel Houlihan


You can configure PHP-FPM to restart automatically by detecting if children processes die within a determined period of time.

In the global configuration "php-fpm.conf" you can set to restart PHP-FPM if 5 child proccess die within 1 minute and wait for 10 seconds before that happens.

// php-fpm.conf
emergency_restart_threshold = 5
emergency_restart_interval = 1m
process_control_timeout = 10s

So you can continue using "dynamic" without using cron.

like image 39
Mario Rezende Avatar answered Sep 22 '22 16:09

Mario Rezende