How can we create connection pool in mongodb at server & application level

744 views
Skip to first unread message

Mukesh Kumar

unread,
Feb 19, 2016, 5:10:10 AM2/19/16
to mongodb-user
Hi,

We are using php, mongodb v 2.6.10, we have not set connection pool. We want to set connection pool at server level.
Presently we are creating a new connections every time, when application talk to database. When our no of connection reach to 400, our db performance impact so badly, and query time increase up to 4 times.

Thanks in advance.


Mukesh Kumar

unread,
Feb 22, 2016, 11:37:13 AM2/22/16
to mongodb-user
Any update??

Wan Bachtiar

unread,
Mar 3, 2016, 12:30:55 AM3/3/16
to mongodb-user

Hi Mukesh,

Since PHP MongoDB driver v1.3.0 (current stable version is v1.6.12), persistent connections are used to minimise the number of connections made to each database server. These connections are saved by the PHP worker process and may be reused between multiple requests.

See blog: mongodb php driver connections handling for more info.

We want to set connection pool at server level.

Could you elaborate more on what do you mean by server level ?
Note that a connection normally would consists of host, port, process id, (if any) replica set name, or (if any) authentication credentials, etc.

When our no of connection reach to 400, our db performance impact so badly, and query time increase up to 4 times.

Other than the number connections, you should also check what those connections are doing. You should check the active working set of your MongoDB deployment whether it fits in RAM or not.

Use mongostat to find out a quick overview of your mongod status. See mongostat fields to find out their meanings.

Can you provide the following information:

  • PHP MongoDB driver version.
  • Code snippet that creates connection.
  • Number of application instances/machines.
  • MongoDB deployment topology. i.e. standalone, replica set or sharded cluster.

Kind regards,

Wan.

Suvarna Pattayil

unread,
Mar 3, 2016, 12:19:42 PM3/3/16
to mongodb-user
Hi Wan,

Sorry for this interruption but if it is possible can you also point to some links/informations where
I can learn about connection pooling when using the MongoDB C Driver. I have been
through the offical documentation but haven't seen anything related to it.

Wan Bachtiar

unread,
Mar 4, 2016, 2:09:56 AM3/4/16
to mongodb-user

Hi Suvarna,

For MongoDB C driver connection pooling please see mongoc_client_pool_t for a thread-safe pooling example.

If you still have further questions, please open a new thread discussion detailing:

  • MongoDB version.
  • MongoDB C Driver version.

Kind regards,

Wan.

Mukesh Kumar

unread,
Mar 5, 2016, 2:34:13 AM3/5/16
to mongodb-user
Hi Wan,

We are using laravel orm, which manage connection. Please find the details:-

  • PHP MongoDB driver version.  -- 1.6.12
  • Laravel version 4.2
  • Code snippet that creates connection- Laravel manage connections to mongo.
    • Number of application instances/machines- We have 1 instance of mongo & application on each machine. Total no of instances are 12.
  • MongoDB deployment topology. i.e. standalone, replica set or sharded cluster- Replica se(2 secondary & 1 primary).

Wan Bachtiar

unread,
Mar 15, 2016, 8:05:39 AM3/15/16
to mongodb-user

Code snippet that creates connection- Laravel manage connections to mongo.

Hi Mukesh,

There are a number of factors that may affect the number of connections from your usage of Laravel. i.e. number of object models and the code interacting with them, relationships between the object models and query operations, life cycle of the object models, laravel configs, etc.

As a start I would suggest to check out persistent database connections. You should test with and without the persistent connections and see whether you can tweak the number of connections down.

/* For example specify in config/database.php */
'options' => array(
    PDO::ATTR_PERSISTENT => true,
)

See Laravel PDO connection options for an example how to use PDO connections. Generally it is better to share the connection through your application.

If you have further questions on Laravel connection pooling behaviour, you may get faster responses by posting a question on StackOverflow or Laravel Forum.

Total no of instances are 12.

Other than keeping the number of connections low, also check what the connections are being used for. For example you can :

  • Set the profiling level and find out what queries are being executed.
  • Check whether the frequently executed queries are using indexes.
  • Use mplotqueries to graph operations/queries.

Also see Analyzing MongoDB performance for more info.

As previously mentioned, you may also want to check the database working set. As you may need to scale up either vertically and/or horizontally for your current application demands.

Regards,

Wan.

Wan Bachtiar

unread,
Mar 15, 2016, 8:08:48 PM3/15/16
to mongodb-user

Hi Mukesh,

Correction regarding the PDO option part:
PDO is distinct from the MongoDB PHP driver and only applies to SQL drivers.

As previously mentioned, the PHP MongoDB driver already uses persistent connections by design since v1.3.0. Unless your code is manually closing connections via MongoClient::close() - which generally is not recommended, you should expect one socket to each mongod (in the case of a replica set) per PHP process (e.g. Apache worker if PHP is a module, or FPM worker if you’re using FastCGI with something like nginx or Apache).

If you have further questions on the use of Laravel, you may get faster responses and wider audience by posting a question on StackOverflow or Laravel Forum.

Regards,

Wan.

Reply all
Reply to author
Forward
0 new messages