Mongo long connection short connection and connection pool

MongoDB versions 1.3 and above are connected through the MongoClient class. The default policy is a long connection, which cannot be modified. So the number of connections actually depends on the number of client processes in fpm. If the amount of fpm is too large, it will inevitably lead to the problem of too many connections. If you have a total of 1000 fpm on all machines, then 1000 long connections will be created. According to the policy of the mongodb server, each connection consumes a minimum of 1M memory, then this 1G memory will be gone. Therefore, the direct solution is to perform a close operation after each use, so that the server does not need to maintain a large number of connections. The close function also has a pit, that is, only the write connection is closed by default (such as the primary of master or replica sets). If you want to close all connections, you need to add the parameter true, that is: $mongo->close(true) The scheme of closing the connection each time can be Effectively reduce the number of concurrent connections to the server, unless your operation itself is very slow. However, it also has its problems. For example, the previous tcp connection cannot be reused every time, and the connection needs to be reconnected, so the connection time will be relatively high, especially when replica sets are used, multiple tcp connections need to be created. So in the end, there may only be two solutions: one is to reduce the number of fpms, and the other is to build a connection pool by yourself, and use the connection pool to converge the connections of each client into a fixed number of connections to MongoDB.

The PHP side is the recommended persistent connection, and it also said that the DB side is not recommended to use pconnect but not applicable to mongo, Mongo strongly recommends the use of persistent connection.

http://www.zhihu.com/question/20012148



http://blog.sina.com.cn/s/blog_4b82753d0100w24w.html long connection short connection

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327101510&siteId=291194637