


Detailed explanation of the configuration of max_children in php-fpm
Now nginx fpm has basically become the mainstream configuration, among which we are more concerned about the configuration of pm.max_chindren
First, we pay attention to a setting: pm = static/dynamic
,
This option is to identify the generation mode of the fpm sub-process:
static: means directly when fpm is running Fork out pm.max_chindren
worker processes
dynamic: means that start_servers
processes will be forked out during runtime. Depending on the load, Dynamic adjustment, no more than max_children processes.
It is generally recommended to use static. The advantage is that it does not need to dynamically determine the load situation and improves performance. The disadvantage is that it takes up more system memory resources.
The above tells us the number of worker processes represented by max_chindren. It is generally believed that the more concurrency this configuration can handle at the same time, this is a relatively big misunderstanding:
- #1) In fact, there are more processes, which increases the cost of process switching, which is more core. Yes, the number of fpm processes that can be executed concurrently will not exceed the number of CPUs. It is a wrong understanding to increase QPS by opening more workers. It does not mean that if you open more processes, you will have more CPUs for processing.
- 2) There are fewer worker processes. If the server is busy, it will cause nginx to hit the data to fpm. It will be found that all workers are working and there are no idle workers to accept requests, thus Resulting in 502.
- 3) In actual business, since we have a lot of I/O operations, such as reading the database, or internal RPC calls, while waiting for I/O, the process will be sleep by the system, and It does not occupy the CPU. If fewer workers are configured, the CPU will not be utilized.
So how should the number of workers be configured?
Theoretically, the number of woker processes = the number of CPUs is the most reasonable, but due to point 2, each worker may not have finished processing the request, so 502 will occur frequently. But opening more processes just means to avoid 502 and temporarily hang the request, but this is only a way to alleviate it. In fact, this will not only increase the concurrency of the system, but also increase the load of the system. Therefore, based on 2 and 3, set a A reasonable number of workers is more important.
The only martial arts in the world that can only be fast is to improve the efficiency of the program as much as possible and compress the time of a single request to the minimum. In this way, the processing time of a single worker is shortened, which can be processed in unit time. Naturally, there were more requests.
Then the number of max_children
can be estimated by the number of requests processed by each worker in unit time. If the processing time of the largest request (cpu time in xhprof) is within 100ms, and 100 requests come in at the same time within 100ms, then in theory, 100 worker processes need to be configured to hang the requests first.
However, the maximum request time may be affected by many external circumstances and is difficult to estimate, especially network I/O is also included. We can borrow third-party profile tools, such as xhprof , this type of tool can count CPU time consumption. Calculating the real number of workers through this time is much more reasonable than calculating the total time. In fact, there is a shortcut here to configure your max_children number, which is to set max_childnren in the early stage. into a relatively large value. After running stably for a period of time, observe the max active processes
in the status of fpm, and then configure max_children to be larger than it. It will be ok.
Related learning recommendations: PHP programming from entry to proficiency
The above is the detailed content of Detailed explanation of the configuration of max_children in php-fpm. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use php-fpm for high-performance tuning PHP is a very popular server-side scripting language that is widely used to develop web applications and dynamic websites. However, as traffic increases, the performance of your PHP application may suffer. In order to solve this problem, we can use php-fpm (FastCGIProcessManager) for high-performance tuning. This article will introduce how to use php-fpm to improve the performance of PHP applications and provide code examples. one,

How to use PHP-FPM optimization to improve the performance of PrestaShop applications. With the rapid development of the e-commerce industry, PrestaShop has become the e-commerce platform chosen by many merchants. However, as the size of the store increases and the number of visits increases, the PrestaShop application may encounter performance bottlenecks. In order to improve the performance of the PrestaShop application, a common method is to use PHP-FPM to optimize and improve the application's processing capabilities. PHP-FPM (FastCGI

How to Improve the Performance of WooCommerce Applications Using PHP-FPM Optimization Overview WooCommerce is a very popular e-commerce plugin for creating and managing online stores on WordPress websites. However, as your store grows and traffic increases, WooCommerce apps can become slow and unstable. To solve this problem, we can use PHP-FPM to optimize and improve the performance of WooCommerce applications. What is PHP-FP

Overview of using php-fpm connection pool to improve database access performance: In web development, database access is one of the most frequent and time-consuming operations. The traditional method is to create a new database connection for each database operation and then close the connection after use. This method will cause frequent establishment and closing of database connections, increasing system overhead. In order to solve this problem, you can use php-fpm connection pool technology to improve database access performance. Principle of connection pool: Connection pool is a caching technology that combines a certain number of databases

How to use PHP-FPM to optimize and improve the performance of Phalcon applications. Introduction: Phalcon is a high-performance PHP framework. Combining with PHP-FPM can further improve the performance of applications. This article will introduce how to use PHP-FPM to optimize the performance of Phalcon applications and provide specific code examples. 1. What is PHP-FPMPHP-FPM (PHPFastCGIProcessManager) is a PHP process independent of the web server

PHP-FPM is a commonly used PHP process manager used to provide better PHP performance and stability. However, in a high-load environment, the default configuration of PHP-FPM may not meet the needs, so we need to tune it. This article will introduce the tuning method of PHP-FPM in detail and give some code examples. 1. Increase the number of processes. By default, PHP-FPM only starts a small number of processes to handle requests. In a high-load environment, we can improve the concurrency of PHP-FPM by increasing the number of processes

Using php-fpm process management to achieve load balancing As Internet applications become increasingly complex and the number of users increases, load balancing has become an indispensable technology. The goal of load balancing is to distribute traffic to multiple servers to improve system stability and performance. In PHP applications, php-fpm (PHPFastCGIProcessManager) is a common process management tool that can be used to achieve load balancing and provides flexible configuration options. This article will introduce how to use

Introduction to PHP-FPM Performance Improvement Strategies and Practice Guide: With the rapid development of the Internet and the increasing number of website visits, it is particularly important to improve the performance of PHP applications. PHPFastCGIProcessManager (PHP-FPM) is a commonly used PHP process manager that can improve the performance of PHP applications through a series of strategies and practices. This article will introduce some PHP-FPM performance improvement strategies, combined with specific code examples, to help readers better understand
