Help Center/ Document Database Service/ Best Practices/ What Can I Do If the Number of Connections of an Instance Reaches Its Maximum?
Updated on 2023-09-26 GMT+08:00

What Can I Do If the Number of Connections of an Instance Reaches Its Maximum?

The number of connections indicates the number of applications that can be simultaneously connected to the database. The number of connections is irrelevant to the maximum number of users allowed by your applications or websites.
  • For a cluster instance, the number of connections is the number of connections between the client and the mongos.
  • For a replica set instance, the number of connections is the number of connections between the client and the primary and secondary nodes.

Symptom

When the number of connections to a DDS DB instance reaches the maximum, new connection requests cannot be responded. As a result, the connection to the DB instance fails.
  • If the following information is displayed when you use Mongo Shell to connect to an instance, no more connections can be established.

  • If the following information is displayed when you use Python to connect to an instance, the number of connections reaches its maximum.

    pymongo.errors.ServerSelectionTimeoutError: connection closed, connection closed

Solution

Handle the problem based on its cause of burst traffic or long-term workloads.
  • If the number of connections reaches the upper limit due to burst traffic, restart the instance or node to release the current connections. Also, check whether there is a large number of retry requests on the client. If the number of retry requests is used up, modify the client parameters to prevent the number of connections from being stacked. You need to modify the retry logic and increase the timeout retry duration.

    Restarting an instance will restart the nodes of the instance in turn. Each node will be intermittently disconnected for about 30 seconds. If the number of collections exceeds 10,000, the intermittent disconnection duration will be prolonged. Before restarting an instance, arrange workloads and ensure that the application supports automatic reconnection.

  • If the number of connections is used up due to long-term workloads, increase the maximum number of connections by changing the value of net.maxIncomingConnections. The change takes effect immediately. Ensure that a changed value is within 20% of the original value each time. After the value change, observe the load changes. If the load is too high after the number of connections is increased, the instance load has reached the bottleneck. In this case, upgrade the instance specifications in a timely manner.