1

I want to create a "X" number of connections to mongodb on my application startup (i.e before my application starts taking traffic).

MongoDB Version: 4.0.11

Mongo Java Driver Version (maven): 3.4.1

I have tried setting the "minConnectionsPerHost" to the required number, but when i execute the code it barely open 1 or 2 connections. But, when i put load on my application the connection count is slowly going up to accommodate the traffic. I want to create those connections before my application starts taking traffic.

        ServerAddress address = new ServerAddress("localhost", 27017);
        List<ServerAddress> serverAddresses = Arrays.asList(address);

        MongoCredential credential = 
        MongoCredential.createCredential("XXXX", "XXXX", 
              "XXXX".toCharArray());
        List<MongoCredential> mongoCredentials = 
              Arrays.asList(credential);

        MongoClientOptions clientOptions = 
              MongoClientOptions.builder().connectionsPerHost(100).
              minConnectionsPerHost(50).build();
        MongoClient mongoClient = new MongoClient(serverAddresses, 
              mongoCredentials, clientOptions);

Is there a way to achieve this using the mongo java driver?

Vinil
  • 33
  • 5
  • you mean `connection pool`? – bananas Aug 02 '19 at 04:17
  • I think internally it already creates many connections and queues up for you. So if one connection is busy fetching information, your application can use up another connection in the queue to fetch another information. And it does all that with the code that you already have. – Faraz Aug 02 '19 at 04:18
  • connection objects are heavy and that is the reason they are not created upfront and kept. Connection pooling is the common strategy used in database connections. Why do you feel that it must be created upfront? – Kris Aug 02 '19 at 04:18
  • @FarazDurrani Is there a way to verify this? When i explain the server stats on mongo db, i see only one connection. `db.serverStatus().connections { "current" : 1, "available" : 203, "totalCreated" : 29, "active" : 1 }` – Vinil Aug 02 '19 at 04:20
  • @Vinil please look at this tread https://stackoverflow.com/questions/24557774/mongo-connection-poolingchanging-the-size-of-connection-pool/50407284#50407284 – bananas Aug 02 '19 at 04:21
  • @Vinil server stats shows the number of clients which is connected active. You can see the available connections as 203. Totally 29 connections were created before. The connections are closed when they are idle. Keeping the connections open is not a good idea, if you have high concurrency – Kris Aug 02 '19 at 04:23
  • @Kris i have a requirement where there is a huge burst of traffic and what i have observed is the initial few hundred requests are taking way more than expected time. So, i wanted to create those connections upfront so that the application is better equipped to serve the traffic. – Vinil Aug 02 '19 at 04:23
  • From what I have seen, you can set a minConnectionsPerHost() in the options, and then use a warmup script to create many connections. The connection pool will keep the minConnectionsPerHost connections alive without closing. – Kris Aug 02 '19 at 04:29
  • @Kris Thanks for the suggestion. Could you please elaborate more on the warm up script? Is it something that can be done with in the application? Also, how can we create those connections individually? – Vinil Aug 02 '19 at 04:32

1 Answers1

1

You can set a minConnectionsPerHost() in the options builder, and then use a warmup script to create many connections. The connection pool will keep the minConnectionsPerHost connections alive without closing.

The warmup script can have a program which spawns 2*minConnectionsPerHost number of threads, which will connect and do may be a dummy read operation. This way connections will be opened, minimum connections will be kept alive.

This seems to be dirty solution :-) But might work!

Kris
  • 8,680
  • 4
  • 39
  • 67