Apache & PHP system resource usage | mpm_prefork + mod_php vs. mpm_event + php-fpm

As descriped previously there are several different ways to serve dynamic requests via Apache. Today we will compare performance thanks to vegeta, a http load testing tool and library written in Go. To compare performance test duration has been fixed at 30 seconds and rate of requests increases from 10 to 50 requests per second. This suite of tests is running against WordPress 4.8.1 with PHP 7.1.7 [opcache and xdebug enabled] and Apache 2.4.27 [mpm_prefork + mod_php vs. mpm_event + php-fpm].

echo "GET https://example.com/wordpress/" | ./vegeta attack -insecure -duration=30s -rate=10 -keepalive=false | tee results.bin | ./vegeta report

All tests use prebuild packages of apache2 and php7.1 distributed by Ondřej Surý via Launchpad. Default configuration has not been modified. To be more prezise the following package versions were used:

  • Apache/2.4.27 (Ubuntu) with mod_http2
  • PHP 7.1.8-2+ubuntu17.04.1+deb.sury.org+4 (fpm-fcgi)
    • with Zend OPcache v7.1.8-2+ubuntu17.04.1+deb.sury.org+4
    • with Xdebug v2.5.5

Not altering configuration files results in a similar amount of successfully served requests. Monitoring system performance via top suggested that system resource usage is quite different because of the huge amount of apache2 processes spawning when using mpm_prefork.

Additional tests were taken to monitor memory and cpu utilization every second via Syrupy. Syrupy stores resource usage for matching processes in a log file every second.

# mpm_prefork + mod_php
syrupy.py --separator=, --no-align -c '(apache2)'

# mpm_event + php-fpm
syrupy.py --separator=, --no-align -c '(apache2|php-fpm)'

When using mpm_prefork and mod_php Resident Set Size [the non-swapped physical memory (RAM)] is definitely higher when comparing to mpm_event and php-fpm.

When using php-fpm you’ll also benefit from using mod_http2 (which will allow multiple requests per process at the same time). With the release of Apache 2.4.26 some setups broke in the past because of inproper use of H2MaxWorkers in conjuction with mpm_prefork. That’s why devs decided to disable mod_http2 when running mpm_prefork.

Loading the login page of phpMyAdmin shows that simultanous requests are not blocked anymore and resources are loaded approx. 100ms faster when using HTTP/2 as transport layer.