Why is the number of MongoDB connections full?

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

when using MongoDB, you may encounter an issue that causes the client to be unable to connect because the number of mongod connections is full. The maximum number of connections Mongod net.maxIncomingConnectionsspecifies that the default value is 1000000, which is equivalent to No limit, and the production environment strongly recommends configuration based on actual requirements to avoid client misuse causing Mongod load to be too high. Mongod Why do I need to limit the number of connections?

The service model for Mongod is that each network connection is handled by a separate thread, each thread is configured with 1MB of stack space, and when the number of network connections is too high, too many threads cause context switching overhead and memory overhead increases.

Detailed analysis of the reference cloud database MongoDB Why do I need to limit the number of connections?

How is driver used?

The Driver of MongoDB's various languages basically encapsulates an object that contains a mongoclient (the Driver name may be slightly different in different languages) and is typically used to construct a global Mo by using the MongoDB connection string URI. Ngoclient, and then uses the global object in subsequent requests to send the request to Mongod.

The way the app is used is roughly similar to

// 通常的用法// global MongoClient objectmongoClient = new MongoClient("mongodb://root:****@host1:port1,host2:port2/admin?replicaSet=repl00& maxPoolSize=100");// request1db1 = mongoClient.getDatabase("db1");coll1 = db1.getCollection("coll1");coll1.find({...})// request2db2 = mongoClient.getDatabase("db2");coll2 = db2.getCollection("coll2");coll2.update({...})// requestN...

Typically each mongoclient contains a connection pool, the default size is 100, or it 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 releasing the mongoclient (or no release at all), and the problem is that the request model changes from a long connection to a short connection, each short connection
Will increase the cost of "establish TCP connection + MongoDB authentication", and the number of concurrent requests will be limited by the number of connections, which can greatly affect performance, and if mongoclient forget to release, it will cause the connection in the Mongoclient connection pool to remain and eventually consume all the available connections.

// 错误的用法// request1mongoClient = new MongoClient("mongodb://root:****@host1:port1,host2:port2/admin?replicaSet=repl00& maxPoolSize=100");db1 = mongoClient.getDatabase("db1");coll1 = db1.getCollection("coll1");coll1.find({...});mongoClient.close();// request2mongoClient = new MongoClient("mongodb://root:****@host1:port1,host2:port2/admin?replicaSet=repl00& maxPoolSize=100");db2 = mongoClient.getDatabase("db2");coll2 = db2.getCollection("coll2");coll2.update({...});MongoClient.close()// requestN...
Mongoclient Connection pool configuration how much suitable?

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

For example, Mongod has a limit of 2000 connections, and 40 service processes in the application business may have access to the Mongod at the same time, and the number of mongoclient connections in each process should be limited to 2000/40 = 50 (when connecting to replica sets, Mongoclien T also establishes a connection with each member of the replica set to monitor the changes in the replication set back-end role.

Why is the number of MongoDB connections full?

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.