<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Connection Pooling (version 1.2.0-1.2.12 *only*)</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongo.connecting.uds.html">Domain Socket Support</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongo.connecting.persistent.html">Persistent Connections (version up to 1.1.4 *only*)</a></div> <div class="up"><a href="mongo.connecting.html">Connecting</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mongo.connecting.pools" class="section"> <h2 class="title">Connection Pooling (version 1.2.0-1.2.12 *only*)</h2> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> This section is no longer relevant as of the 1.3.0 release of the driver and only serves as a historical information on how the pooling used to work. </p> <p class="para"> The latest versions of the driver have no concept of pooling anymore and will maintain only one connection per process, for each connection type (ReplicaSet/standalone/mongos), for each credentials combination. </p> </p></blockquote> <p class="para"> Creating connections is one of the most heavyweight things that the driver does. It can take hundreds of milliseconds to set up a connection correctly, even on a fast network. Thus, the driver tries to minimize the number of new connections created by reusing connections from a pool. </p> <p class="para"> When a user creates a new instance of <a href="class.mongoclient.html" class="classname">MongoClient</a>, all necessary connections will be taken from their pools (replica sets may require multiple connections, one for each member of the set). When the <a href="class.mongoclient.html" class="classname">MongoClient</a> instance goes out of scope, the connections will be returned to the pool. When the PHP process exits, all connections in the pools will be closed. </p> <div class="section"> <h2 class="title">"Why do I have so many open connections?"</h2> <p class="para"> Connection pools can generate a large number of connections. This is expected and, using a little arithmetic, you can figure out how many connections will be created. There are three factors in determining the total number of connections: </p> <ul class="itemizedlist"> <li class="listitem"> <p class="para"> <em> connections_per_pool </em> </p> <p class="para"> Each connection pool will create, by default, an unlimited number of connections. One might assume that this is a problem: if it can create an unlimited number of connections, couldn't it create thousands and the server would run out of file descriptors? In practice, this is unlikely, as unused connections are returned to the pool to be used later, so future connections will use the same connection instead of creating a new one. Unless you create thousands of connections at once without letting any go out of scope, the number of connections open should stay at a reasonable number. </p> <p class="para"> You can see how many connections you have in a pool using the <span class="function"><a href="mongopool.info.html" class="function">MongoPool::info()</a></span> function. Add up the "in use" and "in pool" fields for a given server. That is the total number of connections for that pool. </p> </li> <li class="listitem"> <p class="para"> <em> pools_per_process </em> </p> <p class="para"> Each MongoDB server address you're connecting to gets its own connection pool. For example, if your local hostname is "example.net", connecting to "example.net:27017", "localhost:27017", and "/tmp/mongodb-27017.sock" will create three connection pools. You can see how many connection pools you have open using <span class="function"><a href="mongopool.info.html" class="function">MongoPool::info()</a></span>. </p> </li> <li class="listitem"> <p class="para"> <em> processes </em> </p> <p class="para"> Each PHP process has a separate set of pools. PHP-FPM and Apache generally create between 6 and a couple of dozen PHP worker children. Check your settings to see what the max number of PHP processes is that can be spawned. </p> <p class="para"> If you are using PHP-FPM, estimating the number of connections can be tricky because it will spawn more PHP-FPM workers under heavy load. To be on the safe side, look at the <em>max_children</em> parameter or add up <em>spare_servers</em> + <em>start_servers</em> (choose whichever number is higher). That will indicate how many PHP processes (i.e. sets of pools) you should plan for. </p> </li> </ul> <p class="para"> The three variables above can be multiplied together to give the max number of connections expected: <em>connections_per_pool</em> * <em>pools_per_process</em> * <em>processes</em>. Note that <em>connections_per_pool</em> can be different for different pools, so <em>connections_per_pool</em> should be the max. </p> <p class="para"> For example, suppose we're getting 30 connections per pool, 10 pools per PHP process, and 128 PHP processes. Then we can expect 38400 connections from this machine. Thus, we should set this machine's file descriptor limit to be high enough to handle all of these connections or it may run out of file descriptors. </p> <p class="para"> See <a href="class.mongopool.html" class="classname">MongoPool</a> for more information on connection pooling. </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongo.connecting.uds.html">Domain Socket Support</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongo.connecting.persistent.html">Persistent Connections (version up to 1.1.4 *only*)</a></div> <div class="up"><a href="mongo.connecting.html">Connecting</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>