Let’s take a quick look at how best to set up PHP-FPM to achieve high throughput and low latency

By default, most Settings set PHP-FPM’s PM (process manager) to Dynamic, and use ondemand if you run out of memory

Let’s take a look at the options in the PHp.net documentation and introduce my favorite setting – static:

  • pm = dynamic: The number of child processes is dynamically set according to the following configurationpm.max_children, pm.start_servers, pm.min_spare_servers, pm.max_spare_servers
  • pm = ondemand: processes are created on demand, not dynamically, wherepm.start_serversThe number of processes is created when the service is started
  • pm = static: The number of child processes is determined bypm.max_childrendecision

Similarities between PHP-FPM (PM) and CPUFreq

This seems a bit off topic, but I hope to incorporate it into our phP-FPM tuning topic

We’ve all experienced slow CPUS in laptops, virtual machines, and servers.

Do you remember CPU frequency modulation? (CPUFreq), which is available on Linux and Windows, can be set to onDemand to improve performance and system responsiveness.

Now, let’s compare these descriptions and look for similarities:

  • Governor = ondemand: Quickly and dynamically adjust the CPU frequency as needed. As soon as there is a CPU computation task, it will immediately reach the maximum frequency and reduce the frequency as the idle time increases
  • Governor = conservative: quickly and dynamically adjusts CPU frequency ondemand, more conservatively than ondemand
  • Governor = performance: always run at maximum frequency

See the complete list of CPUFreq governor options for more details

Notice any similarities?

usepm staticTo achieve maximum performance

The PM Static setting depends largely on how much free memory your server has. Basically, if your server is low on memory, PM Ondemand or Dynamic may be a better choice. If you have enough memory, you can set PM static to avoid most of the PM overhead. In other words, when you do the math, set pm.static to the maximum number of processes the server can run, and it won’t suffer from out of memory or cache stress

In the screenshot above, phP-fPM is configured with PM = static and pm.max_children = 100. It has 32GB of memory, and at the time of the screenshot, there were about 200 “active users” in Google Analytics (last 60 seconds). At this level, about 70% of PHP-FPM processes are still idle. This means that when PHP-FPM is set to the maximum capacity of the server’s resources, it doesn’t care about the current traffic and idle processes stay online, waiting for peak traffic to respond immediately, rather than waiting for requests to come in and create processes

I set pm.max_requests very high because this is a production server with no PHP memory leaks. If you have 110% confidence in current and future PHP code, use Pm.max_requests = 0 with PM static

When to use OnDemand and Dynamic

With PM Dynamic, you may get an error similar to the following:

WARNING: [pool xxxx] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 4 idle, and 59 total children
Copy the code

You might try to adjust the PM configuration and still see the same error in this case, the Pm.min is too low, and it can be difficult to adjust with PM Dynamic because traffic and peaks fluctuate so much

The general recommendation is to use PM onDemand. However, the situation gets worse because OnDemand shuts down idle processes when there is no traffic, and then ends up with the same overhead problems as traffic fluctuations (unless you set idle timeouts to be very, very long).

However, PM Dynamic, and onDemand in particular, can be a time saver when you have multiple PM process pools

conclusion

Php-fpm ondemand and Dynamic can limit throughput due to the inherent overhead when traffic fluctuations are high. You need to know your system and set the number of phP-FPM processes to match the maximum capacity of your server. Start with pm.max_children and set according to maximum use of PM Dynamic or ondemand

You’ll notice that in PM Static mode, because you keep everything in memory, the peak traffic will have a smaller peak on the CPU over time, and your server load and CPU average will become smoother. The average size of each phP-FPM process that needs to be manually adjusted will vary

Attached is A/B test chart

Finally, I hope this is a useful article 😁

FPM Tuning: Using ‘PM static’ for Max Performance FPM tuning: Using ‘PM static’ for Max Performance

PHP
PHP-FPM

If you think my article is helpful to you, please click on the top button to reward me

Scan the QR code to share this article