The terrible MongoDB-PHP-Driver connection pool in PHP-FPM mode has uncontrolled connection issues. Problem occurred environment: NginxPHP5.3.10asphp-fpmextensiontonginxmongodb-php-driver1.2.12MongoDB2.2 this problem is caused by the official features of MongoDBPHPDriver1.2.x, please refer to the problem occurred environment description:
Nginx
PHP 5.3.10 as php-fpm extension to nginx
Mongodb-php-driver 1.2.12
Apsaradb for MongoDB 2.2
This problem is caused by official features of MongoDB PHP Driver 1.2.x. for the description, see PHP-202 and PHP-347. Simply put, in the PHP-FPM mode, every PHP Worker process has its own independent mongodb connection pool, which leads to excessive connections and increased memory.
I. PHP service background:
A Web application provides services through the PHP program running Nginx + factcgi.
The maximum number of child processes of the PHP-FPM, which is set through the max_children parameter of the php-fpm.conf (or pm = dynamic, determined by the spare_servers + start_servers parameter ). This value was set to 512.
II. MongoDB service background:
The maximum number of connections of a mongodb instance can be set through maxConns in the startup parameter:
MaxConns: The default value depends on system restrictions (such as ulimit and file descriptor ). If this parameter is not set, mongodb does not limit the number of connections. However, you cannot set more than 20,000.
Generally, maxConns parameters are not set intentionally.
III. What are the terrible connection pool features of MongoDB PHP Driver ?)
MongoDB-php-driver officially provided by mongodb is earlier than 1.3.0 (1.2.0 ~ 1.2.1x) has a terrible connection pool implementation solution. when any query is executed, a connection will be requested from the connection pool, and then returned to the connection pool. Completion means that the variable holding the connection leaves its scope. Www.2cto.com
In PHP-FPM mode, a PHP Web application can calculate the number of concurrent connections set up for MongoDB instance as follows:
Number of processes: max-children = 512, so it is 512 processes;
A MongoDB instance corresponds to a connection pool: The master station has two replica set instances: 165 and 166;
Connections in the connection pool: mongodb-php-driver does not limit this. it can be infinitely increased until the handle is exhausted.
------ Zheng Yun: this calculation method is from mongo. connecting. pools ------
According to the official mongodb documentation, although the infinite increase in connections is theoretically possible, actual observations show that the number of connections between a Web Server and a mongodb instance is usually stable on one value, there won't be much ups and downs.
Assume that the number of connections a PHP Web application initiates to the mongodb-165 is:
750,
The MongoDB instance must maintain at least the following memory:
750x10 MB by default = 7.5 GB
Upgrade 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 the description, please refer...