PHP-FPM Workers and 504 Timeouts: Check the Database First

Raising pm.max_children to fix 504 timeouts can use a lot of RAM when workers are mostly waiting on Postgres. What I check first and what helped.

· 3 min read · Erik Kubica

When I see HTTP 504 Gateway Timeouts on an Nginx and PHP stack, the first instinct (mine included) is to open /etc/php/8.3/fpm/pool.d/www.conf and raise the worker count.

If twenty workers can't handle the traffic, maybe eighty can.

; /etc/php/8.3/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 80
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.max_requests = 1000

You reload the service, watch your monitoring graphs for ten minutes, and it looks fine. Then peak traffic hits at 2:00 PM. The server grinds to a halt, load average spikes to 28 on an 8-core CPU, and the Linux kernel out-of-memory killer takes down the database daemon.

I made this mistake in 2017. Here is what I learned was going on.

The Memory Math of Forked Workers

PHP-FPM runs as a process pool. Each worker is a separate operating system process with its own memory heap, opcode cache structures, and database driver handles.

On a typical Symfony or Laravel application, I've seen an active worker use between 45 MB and 65 MB of Resident Set Size (RSS).

$ ps -ylC php-fpm8.3 --sort:rss | awk '{sum+=$8} END {print "Total RSS:", sum/1024, "MB"}'
Total RSS: 4320.5 MB

With 80 workers:

80 workers * 55 MB average RSS = 4,400 MB RAM

That is 4.4 gigabytes of memory committed to PHP processes. But are those workers actually busy computing?

You can check with strace or by inspecting the database connection list:

-- Checking active queries on PostgreSQL
SELECT pid, state, wait_event_type, wait_event, query 
FROM pg_stat_activity 
WHERE datname = 'app_production' AND state != 'idle';

On the slow APIs I've looked into, most of the workers (75 of 80 in one case) were sitting in kernel epoll_wait or socket read calls. They were waiting for PostgreSQL to finish a single unindexed sequential scan or release an advisory lock.

More workers don't make a slow query faster. They only give more processes a place to wait.

Postgres Connection Limits

PostgreSQL forks a backend process for every client connection. Each backend claims its own work_mem and connection buffers.

When 80 PHP-FPM workers hit PostgreSQL at the same time:

  1. Context Switching: The CPU cores spend cycles switching between 80 PHP processes and 80 PostgreSQL backend processes.
  2. Buffer Contention: Shared memory locks and page cache contention reduce throughput.
  3. Connection Exhaustion: The database reaches max_connections = 100, and health checks and real background jobs get rejected.
$ vmstat 1 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
22  8      0 182400  42100 892040    0    0   120  4200 4820 98200 42 38 12  8  0

Notice the context switches (cs column): nearly 100,000 per second. The CPU is spending almost as much time shuffling tasks (sy at 38%) as running the application code.

What Helped

Instead of letting PHP-FPM use all the available RAM, this is what I'd try:

  1. Lower pm.max_children: Set it based on your hardware, often somewhere around 2x to 4x your CPU core count (for example 16 to 24 workers on an 8-core box), and adjust from measurements.
  2. Add PgBouncer: Put a lightweight connection pooler between PHP and Postgres in transaction mode. 200 incoming PHP requests can share 15 real database connections.
  3. Profile the Slowest Query: Run EXPLAIN (ANALYZE, BUFFERS) on the query holding up the workers. Often one composite index frees them up quickly.

If your workers are CPU-bound rather than waiting on the database, more workers (or more cores) may be the right answer. It's worth checking where they spend their time before deciding.

Tell me what you need built

Usually a reply within a day, by email or WhatsApp.