Home » Java » java – Is connectionPoolSetting for single mongos instance or the whole cluster?-Exceptionshub

java – Is connectionPoolSetting for single mongos instance or the whole cluster?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment


I am a server-side developer, working on a project which uses a mongo cluster as persistent database.

I have a question for https://mongodb.github.io/mongo-java-driver/3.8/javadoc/com/mongodb/connection/ConnectionPoolSettings.html

It said to a MongoDB server
But what if I have a connectionString like following one

mongodb://user:[email protected]:port,mongos3:port,mongos3:port,mongos4:port,mongos5:port,mongos6:port/admin?readPreference=secondaryPreferred

A mongodb sharded cluster which has 6 mongos instance.

Is the connectionPoolSetting related to one mongos server? or related to all mongos servers?

E.g. if we have maxSize = 10 in this setting, does it mean single client has max connection pool = 10 for single mongos server (max pool = 60 for my 6 mongos cluster)? Or max connection pool = 10 for the whole cluster no matter how many mongos server we have?

How to&Answers:

max connection pool = 10 means that in the client pool there will be max 10 connections no matter hoe many server are part of your cluster.


Mongo Client

com.mongodb.client.MongoClient interface:

A client-side representation of a MongoDB cluster. Instances can represent either a standalone MongoDB instance, a replica set, or a sharded cluster. Instance of this class are responsible for maintaining an up-to-date state of the cluster, and possibly cache resources related to this, including background threads for monitoring, and connection pools.

MongoClient object is used to get access to the database, using the getDatebase() method and work with collections and documents in it.

From the documentation:

The MongoClient instance represents a pool of connections to the
database; you will only need one instance of class MongoClient even
with multiple threads.

Typically you only create one MongoClient instance for a
given MongoDB deployment (e.g. standalone, replica set, or a sharded
cluster) and use it across your application. However, if you do create
multiple instances:

All resource usage limits (e.g. max connections, etc.) apply per
MongoClient instance.
To dispose of an instance, call MongoClient.close() to clean up resources.

The following code creates a MongoDB client connection object with connection pooling to connect to a MongoDB instance.

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");

MongoClients.create() static method creates a connection object specified by the default host (localhost) and port (27017). You can explicitly specify other settings with the MongoClientSettings which specifies various settings to control the behavior of a MongoClient.

MongoClient mongoClient = MongoClients.create(MongoClientSettings settings)

Connection Pool Settings:

The ConnectionPoolSettings object specifies all settings that relate to the pool of connections to a MongoDB server. The application creates this connection pool when the client object is created. This creating of connection pool is driver specific.

ConnectionPoolSettings.Builder is a builder for ConnectionPoolSettings has methods to specify the connection pool properties. E.g., maxSize​(int maxSize): The maximum number of connections allowed. Default is 100. Other methods include, minSize, maxConnectionIdleTime, etc.

Code to instantiate a MongoClient with connection pool settings:

MongoClientSettings settings = MongoClientSettings.builder()
                                   .applyToConnectionPoolSettings(builder ->
MongoClient mongoClient = MongoClients.create(settings);
// Verify the connection pool settings max size as

Question: Is the connectionPoolSetting related to one mongos server?
or related to all mongos servers?

A client or application connects to the sharded cluster (includes all its shards) via the mongos router. The client program specifies the URL connection string and other options for the connection. In a sharded cluster, a client may connect thru a set of mongoss or a single mongos, or multiple clients can connect thru a single mongos, etc.,; it depends upon your application architecture.

If you are connecting via a single mongos, you can specify the mongos‘s host, port, user/password, etc., in the connection string. If it is a multiple mongos‘s, then multiple host/port values. Irrespective of the number of mongos‘s, the client program connects to the cluster via only one mongos.

The connection pool setting is for one mongos router only, as an application connects to one mongos irrespective of the number of mongoss specified in the connection string.