PHP-FPM using 100% CPU on a server that has 16 threads

I have a server that has 32 GB of RAM and 16 threads. This server has an API in PHP that receives many requests from other external APIs, I made a configuration in PHP-FPM to support all these requests without slowing down the site, this worked well for 1 year, now the requests have practically doubled and this is leaving website pages are slow, taking 5-6 seconds to load, which is not normal. Maybe I have to redo a configuration in PHP-FPM to support this new demand for requests, what would you do in this situation?

My server has 32 GB of RAM, but uses only 2.5 GB, is that okay? Is it possible to maintain a balance between RAM and CPU to improve CPU performance or am I talking nonsense?

pm.max_children = 32
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 1000

htop command output TOP

There are 32 children of PHP-FPM running the processes.


Solution 1:

I would use php-fpm settings with pm = static. From my experience high traffic websites work better with pm static rather than dynamic/ondemand.

You can get an ideea about the memory used by a php-fpm process using the command bellow:

ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'

I would modify the php-fpm config with something like this:

pm.max_children = 25
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pm.max_requests = 1000

Restart php-fpm afterwards!

As a side note, here's a very interesting article that can explain a lot and guide you to find the best configuration: https://thisinterestsme.com/php-fpm-settings/