Nginx – PHP-FPM Pool, Child Processes and Memory Consumption

nginxPHPphp-fpm

In my PHP-FPM configuration I have 3 Pools, the eg: Config is:

;;;;;;;;;;;;;;;;;;;;;;;
; Pool 1              ;
;;;;;;;;;;;;;;;;;;;;;;;
[www1]
user = www
group = www

listen = /tmp/php-fpm1.sock;
listen.backlog = -1
listen.owner = www
listen.group = www
listen.mode = 0666
pm = dynamic
pm.max_children = 40
pm.start_servers = 6
pm.min_spare_servers = 6
pm.max_spare_servers = 12
pm.max_requests = 250
slowlog = /var/log/php/$pool.log.slow
request_slowlog_timeout = 5s
request_terminate_timeout = 120s
rlimit_files = 131072

;;;;;;;;;;;;;;;;;;;;;;;
; Pool 2              ;
;;;;;;;;;;;;;;;;;;;;;;;
[www2]
user = www
group = www

listen = /tmp/php-fpm2.sock;
listen.backlog = -1
listen.owner = www
listen.group = www
listen.mode = 0666
pm = dynamic
pm.max_children = 40
pm.start_servers = 6
pm.min_spare_servers = 6
pm.max_spare_servers = 12
pm.max_requests = 250
slowlog = /var/log/php/$pool.log.slow
request_slowlog_timeout = 5s
request_terminate_timeout = 120s
rlimit_files = 131072

;;;;;;;;;;;;;;;;;;;;;;;
; Pool 3              ;
;;;;;;;;;;;;;;;;;;;;;;;
[www3]
user = www
group = www

listen = /tmp/php-fpm3.sock;
listen.backlog = -1
listen.owner = www
listen.group = www
listen.mode = 0666
pm = dynamic
pm.max_children = 40
pm.start_servers = 6
pm.min_spare_servers = 6
pm.max_spare_servers = 12
pm.max_requests = 250
slowlog = /var/log/php/$pool.log.slow
request_slowlog_timeout = 5s
request_terminate_timeout = 120s
rlimit_files = 131072

I calculated the pm.max_children processes according to some example calculations on the web like 40 x 40 Mb = 1600 Mb. I have separated 4 GB of RAM for PHP, now according to the calculations 40 Child Processes via one socket, and I have total of 3 sockets in my Nginx and FPM configuration. My doubt is about the amount of memory consumption by those child processes.

I tried to create high load in the server via httperf hog and siege but I could not calculate the accurate memory usage by all the PHP processes (other processes like MySQL and Nginx were also running). And all the sockets were in use, So, I seek guidance from anyone who have done this before or know how exactly the pm.max_children in PHP Works.

Since I have 3 Pools/sockets with 40 child processes does that count to 3 x 40 x 40 Mb of Memory usage ? or it is just like 40 Max. Child processes sharing 3 sockets (and the total memory usage is just 40 x 40 Mb) ?

Best Answer

There is no simple answer to this question. A single memory figure is hard to put onto a running PHP-FPM process.

First of all, memory usage of one process is usually divided into multiple parts (usually shared and unshared memory). The usage of shared memory is greatly dependent on the usage of the libraries in PHP.

If the guidance you need is to optimize the correct settings for a busy web-server I can recommend the following:

  • Stick to safe values while going into production.
  • Use busy production times to stretch the limits and see how it handles (production usage is the best indicator).
  • Reaping often used processes give a fresh start for a process unshared memory.
  • Stop focusing on memory usage per process.
  • Run benchmarking tools and load generators to find the limits of your system.

I think you are seeking the optimal memory settings fitting the dimensions of your server. Keep in mind that the duration of one request also has a great impact on the amount of processes required to keep your web server responsive.

A good reference to measuring memory of a process is on stackoverflow.