Solution to the problem of MongoDB connection number over limit

Source: Internet
Author: User
Tags db2 mongoclient mongodb mongodb connection string


When you use MongoDB, you may encounter problems that cause clients to not connect because the number of mongod connections is full. The maximum number of Mongod connections is specified by Net.maxincomingconnections, with a default value of 1000000, which is equivalent to No limit, and the production environment is strongly recommended to be configured according to actual requirements to avoid client misuse resulting in Mongod overload.

Mongod Why do I need to limit the number of connections?

Mongod's service model is that each network connection is handled by a single thread, each thread is configured with 1MB of stack space, and when the number of network connections is too high, too many threads can cause context switching overhead to increase and memory overhead.

Detailed analysis Reference Cloud database MongoDB Why do you need to limit the number of connections?

How to use driver?

MongoDB the Driver of each language basically encapsulates an object that contains a mongoclient (the Driver name of a different language may be slightly different) and is typically used to construct a global Mo by MongoDB connection string URI at use. Ngoclient, and then use the global object in subsequent requests to send the request to Mongod.

The way it's used is roughly similar to

The usual usage

Global Mongoclient Object
Mongoclient = new Mongoclient ("mongodb://root:**** @host1:p ort1,host2:port2/admin?replicaset=repl00& Maxpoolsize=100 ");

Request1
DB1 = Mongoclient.getdatabase ("DB1");
COLL1 = Db1.getcollection ("Coll1");
Coll1.find ({...})

Request2
DB2 = Mongoclient.getdatabase ("DB2");
Coll2 = Db2.getcollection ("Coll2");
Coll2.update ({...})

Requestn
...
Usually each mongoclient contains a connection pool, the default size is 100, and can be specified by the Maxpoolsize option when constructing mongoclient.

A typical error usage is that the user constructs a mongoclient for each request, the request ends the release mongoclient (or is not released at all), and the problem is that the request model changes from a long connection to a short connection, each time a short connection
Increases the overhead of "establish TCP connection + MongoDB authentication", and the number of concurrent requests is limited by the number of connections, which greatly affects performance, and if Mongoclient forgets to release, it will cause the connection in the Mongoclient connection pool to be maintained and eventually consume all available connections.

Use of Errors
Request1
Mongoclient = new Mongoclient ("mongodb://root:**** @host1:p ort1,host2:port2/admin?replicaset=repl00& Maxpoolsize=100 ");
DB1 = Mongoclient.getdatabase ("DB1");
COLL1 = Db1.getcollection ("Coll1");
Coll1.find ({...});
Mongoclient.close ();


Request2
Mongoclient = new Mongoclient ("mongodb://root:**** @host1:p ort1,host2:port2/admin?replicaset=repl00& Maxpoolsize=100 ");
DB2 = Mongoclient.getdatabase ("DB2");
Coll2 = Db2.getcollection ("Coll2");
Coll2.update ({...});
Mongoclient.close ()

Requestn
...
How large is the Mongoclient connection pool configuration appropriate?

Usually mongoclient use the default 100 connection pool (the specific default value of the Driver document) is OK, when access to the same source of Mongod a long time, you need a reasonable planning connection pool size.

For example, the number of mongod connections is limited to 2000, and 40 service processes on the application business may access the Mongod at the same time, when the number of mongoclient connections in each process should be limited to 2000/40 = 50 (when connecting to the replication set, Mongoclien T also establishes a connection to each member of the replica set to monitor the change in the backend role of the replication set.

Related Article

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: info-contact@alibabacloud.com 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.