Home  >  Article  >  Backend Development  >  The terrible MongoDB-PHP-Driver connection pool uncontrolled connection problem in PHP-FPM mode_PHP tutorial

The terrible MongoDB-PHP-Driver connection pool uncontrolled connection problem in PHP-FPM mode_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:08:051039browse

The environment in which the problem occurred:

Nginx
PHP 5.3.10 as php-fpm extension to nginx
mongodb-php-driver 1.2.12
MongoDB 2.2
This problem is caused by the official features of MongoDB PHP Driver 1.2.x. For descriptions, please see PHP-202 and PHP-347. Simply put, in PHP-FPM mode, each PHP Worker process has its own independent mongodb connection pool, which causes the number of connections to easily exceed the standard and the number of memory to double.
1. PHP service background:
A web application is served by a PHP program running Nginx+factcgi.
The maximum number of child processes of PHP-FPM is set through the max_children parameter of php-fpm.conf (or comprehensively determined by the spare_servers+start_servers parameter when pm=dynamic). This value was previously set to 512.
2. MongoDB service background:
The maximum number of connections limit for a mongodb instance can be set via maxConns in the startup parameters:
maxConns: The default value depends on system limitations (such as ulimit and file descriptor). If this parameter is not set, mongodb itself will not limit the number of connections. However, you cannot set more than 20,000.
Generally do not set the maxConns parameter deliberately.
3. The terrible connection pool feature of MongoDB PHP Driver (BUG?)
The mongodb-php-driver officially provided by MongoDB has a terrible connection pool implementation in versions below 1.3.0 (1.2.0~1.2.1x). When executing any query, it will request a connection pool from the connection pool. The connection is completed and then returned to the connection pool. Completion here means that the variable holding the connection leaves its scope. www.2cto.com
In PHP-FPM mode, the number of concurrent connections a PHP web application can establish to a MongoDB instance is calculated as follows:
Number of processes: max-children = 512, then there are 512 processes;
One MongoDB instance corresponds to one connection pool: the main site is configured with two replica set instances, 165 and 166;
The number of connections in the connection pool: mongodb-php-driver does not impose any restrictions on this and can be increased indefinitely until the handles are exhausted.
——————Zheng Yun: This calculation method comes from mongo.connecting.pools——————
According to the official mongodb documentation, although it is theoretically possible for the number of connections to increase indefinitely, actual observations have found that the number of connections between a Web Server and a mongodb instance is usually stable at a value, and there will not be much change. ups and downs.
So, assuming that the number of connections initiated by a PHP web application to mongodb-165 is:
750 pieces,
The amount of memory this MongoDB instance needs to maintain for this purpose is at least:
750 × default 10MB = 7.5 GB

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477804.htmlTechArticleThe environment where the problem occurs: Nginx PHP 5.3.10 as php-fpm extension to nginx mongodb-php-driver 1.2.12 MongoDB 2.2 This problem is caused by the official feature of MongoDB PHP Driver 1.2.x. For description, please see...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn