Awesome MongoDB-PHP-Driver connection pool in PHP-FPM Mode

Source: Internet
Author: User
Tags mongodb documentation

Problem Environment: NginxPHP 5.3.10 as php-fpm extension to nginxmongodb-php-driver 1.2.12MongoDB 2.2 this problem is caused by the official features of MongoDB PHP Driver 1.2.x, please refer to the PHP-202 and PHP-347 description. 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 the 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. Under the PHP-FPM mode of, a PHP Web application can calculate the number of concurrent connections of MongoDB instance as follows: Number of processes: max-children = 512, It is 512 processes; A MongoDB instance corresponds to a connection pool. The master station has two replica set instances, 165 and 166. The number of connections in the connection pool: mongodb-php-driver, it can be infinitely increased until the handle is exhausted. ------ Zheng: This calculation method is from mongo. connecting. pools ------ according to the official mongodb documentation, although the infinite increase in the number of connections is theoretically possible, the actual observation shows that the number of connections between a Web Server and a mongodb instance is usually stable in one value, there won't be much ups and downs. Assume that a PHP Web application has 750 connections to the mongodb-165, And the MongoDB instance must maintain at least 750x10 MB = 7.5 GB by default.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.