Skip to content

PHP-FPM pool sizing on PaaS: pm.dynamic vs pm.static for autoscaling workloads

Picking the wrong process manager mode on a PaaS container wastes memory in pm.dynamic or hides queue pressure in pm.static. Here is how to size pools when the platform is also autoscaling.

You move a Laravel app from a single VM to a PaaS where each node is a container with a 2 GB memory cap, and the platform spins up additional nodes when CPU or memory crosses a horizontal scaling trigger. The previous host had pm = dynamic with pm.max_children = 80. You ship the same www.conf to the PaaS, and within an hour the platform is scaling out aggressively, the bill is climbing, and phpfpmstatus shows pools sitting half-idle while the queue depth is still zero. The configuration that was correct on a long-lived VM is wrong on a PaaS node — and the two are wrong in different directions.

What’s happening

pm.dynamic and pm.static are not interchangeable when the platform itself is autoscaling. On a long-lived VM, dynamic mode reclaims memory between traffic peaks and that’s a feature. On a PaaS node that is one of N short-lived containers behind a horizontal autoscaler, the platform is already doing the elasticity job — at the node level, not the worker level — and dynamic mode now introduces three problems.

First, dynamic mode keeps a moving target for max_children that the platform’s CPU/memory triggers see as a noisy signal. The node sits at 60% memory because PHP-FPM is holding spare workers; the autoscaler sees this and triggers a scale-out that wouldn’t have been needed if the pool were sized to the node. Second, the spare worker logic (pm.start_servers, pm.min_spare_servers, pm.max_spare_servers) was designed for a workstation-style box with elastic free memory, not a 2 GB cgroup where every 60 MB worker counts. Third, when a scale-out does happen, the new node spends the first 30-60 seconds warming up the worker pool — start_servers workers, growing toward max_children as requests arrive — and during that window the new node serves traffic slower than the old ones, distorting your p95.

pm.static flips the contract. You declare exactly how many workers each node runs, the memory ceiling is predictable, and the autoscaler operates on a clean signal: when CPU sustained over N seconds hits the trigger, add a node. The cost is that you can’t borrow memory back from PHP-FPM during quiet periods, but on a PaaS that’s not your job to solve — the platform retires nodes when they go idle.

The decision rule we use on managed PaaS deployments: if the platform autoscales horizontally on CPU or memory triggers, run pm.static and size the pool to the node. If the platform is fixed-size or only scales vertically, pm.dynamic is still the right answer because elasticity has to come from somewhere.

How to verify

Three pieces of evidence the operator can read on a single node.

Memory per worker, sampled while the pool is hot:

ps --no-headers -o rss,cmd -C php-fpm | \
  awk '/pool/ {sum+=$1; n++} END {if (n) print "avg KB/worker:", sum/n}'

Worker saturation over a five-minute window — if listen queue is consistently nonzero, you are under-provisioned; if it is always zero and active processes is well below total processes, you are over-provisioned:

SCRIPT_NAME=/status SCRIPT_FILENAME=/status REQUEST_METHOD=GET \
  cgi-fcgi -bind -connect 127.0.0.1:9000 | \
  grep -E "(active processes|listen queue|max active|max children reached)"

max children reached is the only metric that matters for “is the pool too small.” Anything above zero in the last hour means the pool was saturated and PHP-FPM refused to accept connections — which the user sees as a 502 or a long wait.

Container cgroup memory ceiling, so the math is honest:

cat /sys/fs/cgroup/memory.max 2>/dev/null || \
  cat /sys/fs/cgroup/memory/memory.limit_in_bytes

If the node has 2 GB and each worker is averaging 80 MB, you have room for roughly 20 workers after leaving headroom for the platform agent, opcache, and OS. Sizing the pool above that ceiling means the OOM killer takes the node out — and on PaaS, “the node restarts” is not invisible; it shows up as a brief 503 burst for in-flight requests.

The fix

For an autoscaling PaaS, switch to pm.static and right-size to the node. A representative www.conf for a 2 GB container running a Laravel-style PHP 8.3 workload with opcache and a typical worker footprint of 70-90 MB:

[www]
user = www-data
group = www-data
listen = /run/php/php-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

pm = static
pm.max_children = 18
pm.max_requests = 1000

; status + ping endpoints — wire these to the platform health check
pm.status_path = /status
ping.path = /ping
ping.response = pong

; restart pools that wander into bad state
emergency_restart_threshold = 5
emergency_restart_interval = 1m
process_control_timeout = 10s

; opcache is per-pool memory, account for it in the budget
php_admin_value[memory_limit] = 192M
php_admin_value[opcache.memory_consumption] = 128

; slow log catches the queries the framework hides
request_slowlog_timeout = 5s
slowlog = /var/log/php-fpm/slow.log

; lock the pool to one core if the container has one CPU cgroup
process.priority = 0

; tighten the listen backlog to match what the platform's LB allows
listen.backlog = 256

If your platform exposes a horizontal scaling rule, configure it on CPU sustained over 60 seconds — not on memory. Memory will be flat by design with pm.static, so a memory-based rule never fires; CPU is the honest signal of “this node is full of work.”

For workloads where traffic is genuinely bimodal — a marketing site that sees 10x at campaign launches and idles the rest of the time — pm.dynamic plus the platform’s horizontal trigger can still be the right answer, but only if you constrain it: set pm.max_children to the absolute ceiling the node can hold, and pm.start_servers close to your steady-state load so a cold pool doesn’t drag the p95 during scale-out. That looks like:

[www]
pm = dynamic
pm.max_children = 18
pm.start_servers = 10
pm.min_spare_servers = 6
pm.max_spare_servers = 12
pm.process_idle_timeout = 30s
pm.max_requests = 1000

Two changes that pay off independent of the mode choice: set pm.max_requests to a finite value (1000-2000) so workers recycle and reclaim leaked memory from third-party libraries, and route the FPM status endpoint to your monitoring so max children reached is alerted on. A worker pool that hit saturation at 3:14 AM and recovered on its own is a leading indicator of the next outage, not a non-event.


If you’d rather have us pick the mode, the pool size, and the autoscaling triggers on your specific workload, Cloud PaaS at Stack Harbor includes that tuning as part of environment management — start a scoping call.