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.maxIncomingConnections
specifies 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?