Number of current connections to MongoDB

Hi,
I’m running MongoDB locally. According to my calculations, I should have max 300
“current” connections to the db. But I see that I have north of 1.6k active “current” connections.
What can be the reason for that?

Thanks all,
Moshe

Hello @Moshe_G ,

To understand your setup and use-case better, could you please provide more details, such as:

  • MongoDB version
  • Deployment topology( Standalone, Replica set, Sharded cluster or any other, also include PSA or PSS, if applicable)
  • Can you please share with how you calculated that your maximum number of active connections should be 300 ? Please share any associated connection code snippets redacting any credentials.
  • How did you check the total number of active connections?

There could be many reasons why you may be seeing high number of active connections than expected. Below are a few possibilities:

  • A connection pool is a cache of open, ready-to-use database connections maintained by the driver. Your application can seamlessly get connections from the pool, perform operations, and return connections back to the pool. Connection pools are thread-safe. This can lead to more active connections but should not be an issue if sufficient hardware resources are available. This is because a single client can have multiple connections open at the same time. You can check if this is the case by looking at the number of connections with the help of output of the db.currentOp() command.

  • In case your application uses long-running queries, they can keep connections open for a longer time, which can lead to more active connections. You can check if this is the case by looking at the secs_running field in the output of the db.currentOp() command, which shows the duration of the operation in seconds. MongoDB calculates this value by subtracting the current time from the start time of the operation. Only appears if the operation is running; i.e. if active is true.

  • If your application has recently experienced an increase in traffic, it could be that your application is creating more connections to handle the increased load. Consider scaling up your hardware resources to handle the increased workload and smooth working of database.

I would recommend you go through below links to learn more about connections in MongoDB and how you can monitor and tune self managed installations as per your application’s workload.

Regards,
Tarun

1 Like

Hi @Tarun_Gaur Thank you,
The Version is 5.0 and its a stand-alone deployment.
I use PyMongo 4.1.1 as a Driver.
I have max 300 different physical computers,
each one with thereon NIC,
that runs a python code that uses the same PyMongo client per machine.
I see on the Performance tab on the Compass GUI that I can get north of 1.6 k connections.
Thank you
Moshe

Please refer to below Problem-Solution from Tuning Connection Pool settings:

Problem Solution
Database CPU usage is higher than expected. The server logs or real time panel show more connection attempts than expected. Decrease the maxPoolSize or reduce the number of threads in your application. This can reduce load and response times.
  • 300 different clients does not necessarily mean that it will only use 300 connections. Please see below for more details.
  1. max_pool_size : The maximum allowable number of concurrent connections to each connected server. Requests to a server will block if there are maxPoolSize outstanding connections to the requested server. Defaults to 100. Cannot be 0. When a server’s pool has reached max_pool_size, operations for that server block waiting for a socket to be returned to the pool. If waitQueueTimeoutMS is set, a blocked operation will raise ConnectionFailure after a timeout. By default waitQueueTimeoutMS is not set.

  2. min_pool_size : The minimum required number of concurrent connections that the pool will maintain to each connected server. Default is 0.

so, 300 clients * (max connection pool size default which is 100) == 30,000 connections max possible.

  • The connection pool is automatically managed by pymongo itself, so it will create connections as required by the workload.

  • As per documentation on Connection Pool

Definition

A connection pool is a cache of open, ready-to-use database connections maintained by the driver. Your application can seamlessly get connections from the pool, perform operations, and return connections back to the pool. Connection pools are thread-safe.

Benefits of a Connection Pool

A connection pool helps reduce application latency and the number of times new connections are created. A connection pool creates connections at startup. Applications do not need to manually return connections to the pool. Instead, connections return to the pool automatically. Some connections are active and some are inactive but available. If your application requests a connection and there’s an available connection in the pool, a new connection does not need to be created.

Note: To handle the increase in workload, sufficient hardware resources are required by the database instance also.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.