Let’s take a quick look at how to set up PHP-FPM to achieve high throughput, low latency, and stable CPU and memory usage. By default, most Settings set phP-fPM PM (process manager) todynamicOr if you have problems with available memory, it is often recommended that you use itondemand. Next, let’s base it onphp.netCompare these two management options with my most common SettingsstaticThe difference between:

  • PM = dynamic: The number of child processes is dynamically generated according to the following instructions: pm.max_children, pm.start_servers, pm.min_spare_Servers, pm.max_spare_servers.

  • PM = ondemand: Processes are generated at service startup from the pm.start_Servers directive rather than dynamically.

  • PM = static: The number of child processes is determined by the pm.max_children directive.

To viewA complete listGet to knowphp-fpm.confAll instructions of.

Similarities between phP-FPM process manager (PM) and CPUFreq Governor

Now, we’re going to go off topic here, but I think it has something to do with PHP-FPM tuning. Well, we’ve all had CPU slowdowns at some point, whether it’s laptops, VMS, or dedicated servers. Remember the CPU scaling problem? (CPUFreq Governor) These Settings work on Unix-like systems and Windows, and you can improve performance and speed up system response by changing the CPU Governor from ondemand to Performance. Now, let’s compare the following CPUFreq Governor description with phP-FPM PM:

  • Governor = ondemand: Dynamically adjust CPU frequency based on current load. Set the CPU frequency to its maximum and then reduce it as idle time increases.

  • Governor = conservative: Dynamically adjust frequency based on current load. Slower than onDemand.

  • Governor = Performance: Always run the CPU at maximum frequency.

To viewDetailed list of CPUFreq Governor optionsFor more information.

Notice the similarities? This is the primary purpose of my comparison, and in order to find the best way to write this article, I recommend that you use PM static of PHP-fPM as your first choice.

Using CPU Governor’s Performance setting is a very safe way to improve performance, as it perfectly uses the full performance of your server’s CPU. The only things to consider are things like heat dissipation, battery life (in laptops), and some side effects of having the CPU at 100% all the time. Once set to Performance, it is indeed the fastest setting for your CPU. For an example, see the ‘force_Turbo’ setup on Raspberry Pi, which teaches you to use the Performance Governor on RPi boards, where the performance improvement is even more noticeable due to the low CPU clock speed.

usepm staticOptimize your server performance

The static setting of phP-fpm depends on how much free memory your server has. In most cases, if your server is running low on memory, setting the PM to Ondemand or Dynamic is a better choice. However, once you have free memory available, setting the PM to static will reduce much of the overhead of the PHP process manager (PM). In other words, you should use pm.static to set the maximum number of phP-fpm processes without running out of memory or cache stress. In addition, the use of CUP and other pending PHP-FPM operations cannot be affected.

In the screenshot above, the server Settings (PM = static, pm.max_children =100) use up to 10GB of memory. Notice the highlighted columns. There are approximately 200 active users (within 60 seconds) in the Google analysis graph. At this level of usage, 70% of phP-FPM child processes are idle. This means that phP-FPM is always running enough processes regardless of current traffic. Idle processes are always online and can respond quickly even when traffic peaks, rather than waiting for PM to generate child processes and then kill them after X pm.process_IDLE_TIMEOUT seconds. I set pm.max_requests very high because this is a PHP production server where memory leaks are impossible. If you have 110% confidence in your PHP scripts, you can choose to use Pm.max_requests = 0. However, you are advised to restart the service properly. The number of requests is set high to avoid excessive PM overhead. For example, set Pm.max_requests = 1000, but this depends on the pm.max_children setting and the actual number of requests per second.

Screenshots are filtered using Linux top with the ‘u’ (user) option and phP-fpm username. It only shows the top 50 or so (uncounted) processes, but basically the top command will only show what fits the size of your terminal window — in this case, using %CPU sort. To view all 100 phP-fpm processes, you need to use the following command:

top -bn1 | grep php-fpm

Copy the code

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, when you have multiple PM process pools,pm dynamic, especiallyondemandIt can save you time. For example, on the shared VPS, there are 100+ cPanel accounts and 200+ domain names to usepm.staticOr is itpm.dynamicIs impossible, even though in the absence of any traffic, memory would be used up instantaneously, whilepm.ondemandThis means that all idle child processes are shut down completely, saving a lot of memory. CPanel developers are aware of this problem, and cPanel is now set topm.ondemand. 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

Update: Attached is an A/B test chart.

From PHP/Laravel developer community laravel-china.org/topics/1495…