Skip to content

PHP-FPM pool sizing: pm.max_children done right

Calculate pm.max_children from real per-worker RSS and host memory, pick between static and dynamic process management, and avoid the cargo-cult numbers.

The internet’s most-quoted PHP-FPM formula is “max_children = (total RAM - reserved) / per-worker RAM” and every word of that needs unpacking. The number you actually want depends on what counts as “per-worker RAM”, what the workload looks like, and whether the box does anything besides PHP. This article walks through how to measure those inputs and pick pm.max_children (and friends) from data rather than guesswork.

How to verify

Measure what each FPM worker actually uses right now:

ps -eo pid,rss,cmd --sort=-rss | grep "php-fpm: pool" | head
sudo systemctl status php8.3-fpm --no-pager | head
curl -s "http://127.0.0.1/fpm-status?full"
free -h
sudo grep -E '^(pm|memory_limit)' /etc/php/8.3/fpm/pool.d/www.conf /etc/php/8.3/fpm/php.ini

The status endpoint (if enabled in the pool config) reports active processes, idle processes, max active processes, and max children reached. If max children reached is non-zero, you are already capacity-bound and incoming requests have queued.

What’s happening

A PHP-FPM worker’s RSS has three components that grow over time. Opcache and APCu are shared across workers (shared memory) — they count once against the host, not per worker. The PHP runtime itself (interpreter, loaded extensions) is ~30 MiB. The application’s per-request working set (loaded classes, framework, in-memory query results) is what actually varies, usually 40-150 MiB per worker on a modern app like WordPress or Laravel.

The host’s RAM budget needs to subtract: the OS and kernel buffers (~500 MiB on a small box), Nginx (~100 MiB), the OPcache and APCu shared segments (256 MiB is typical), MySQL/Redis if local, and a 20% headroom for the page cache and bursts. Whatever is left is divisible into PHP workers.

pm = static means N workers always running. pm = dynamic keeps pm.min_spare_servers idle and grows up to pm.max_children, then idles back down. pm = ondemand only starts workers on incoming requests and lets them die. For latency-sensitive workloads static is best — no cold-start cost per request burst. For shared hosting where many idle apps coexist, ondemand makes sense.

The fix

  1. Sample real per-worker RSS over an hour of production traffic:

    while true; do
      ps -eo rss,cmd --no-headers | awk '/php-fpm: pool/ {sum+=$1; n++} END {if (n>0) print n, sum/n/1024 " MiB avg"}'
      sleep 60
    done | tee /tmp/fpm-rss.log

    Take the 95th-percentile worker RSS, not the average. A pool sized to the average will OOM during a traffic burst.

  2. Compute the available pool RAM:

    pool_ram = total_ram - 500 (OS) - 100 (nginx) - 256 (opcache+apcu) - 20%_headroom - whatever_else_runs_here

    On a 4 GiB host with only Nginx + FPM + an external DB, that is roughly 4096 - 500 - 100 - 256 - 800 = 2440 MiB for workers.

  3. Divide by p95 per-worker RSS to get pm.max_children:

    max_children = pool_ram / p95_rss

    If p95 is 120 MiB, max_children = 2440 / 120 ≈ 20. Use 18-20 to leave a margin.

  4. Choose the process management mode based on workload shape:

    ; latency-sensitive single app: static
    pm = static
    pm.max_children = 20
    pm.max_requests = 1000
    
    ; bursty traffic, idle most of the time: dynamic
    pm = dynamic
    pm.max_children = 20
    pm.start_servers = 8
    pm.min_spare_servers = 4
    pm.max_spare_servers = 12
    pm.max_requests = 1000
  5. Watch the FPM status max children reached counter for a week. If it stays at 0, you are over-provisioned (or workload is fine); if it climbs every day, raise max_children or add another host.

Operational notes

  • php.ini’s memory_limit is per-request, not per-worker. A worker that handled a 200 MiB request keeps that allocation across requests until it recycles — pm.max_requests exists to bound that drift.
  • Do not confuse pm.max_children with thread count. PHP-FPM workers are processes — each consumes RSS independently. Setting max_children = 100 on a 2 GiB box guarantees OOM.
  • A box where MySQL or Redis also runs needs explicit memory accounting for them — InnoDB buffer pool, Redis maxmemory. Subtract from pool_ram before dividing.
  • pm = ondemand adds visible latency to the first request after an idle period — the worker spawn is ~30 ms. Acceptable for low-traffic sites, painful for any site with response-time SLOs.
  • Slow-log (request_slowlog_timeout = 5s + slowlog = /var/log/php-fpm-slow.log) is the single best diagnostic. It captures the stack trace of any request exceeding the threshold, which surfaces the endpoints actually filling worker capacity.

For investigating runaway worker memory growth, see Tracking PHP-FPM memory leaks. Stack Harbor’s managed operations practice runs the sampling-and-sizing cycle quarterly per host — a number that was right at launch is rarely right a year later as traffic and the app evolve.