MongoDB – How to Estimate Max Number of Connections Supported

mongodbreplicationsharding

We have a sharded cluster with 2 replica sets. Each replica set is a 3 member cluster with one primary and 2 secondaries. Each instance is a r3.large (aws-ec2) instance with 15G ram.

mongos's are in app servers and can have minimum 3 and can auto-scale to 10. Is there a way to find out number of max connections supported by sharded cluster. That means number of connections between mongos and cluster. (Like For MySQL max connections are based on size of RAM)

mongo version – 2.6.8

Best Answer

Each connection eats up about 1MB of RAM on the server side. That includes the connections to the config servers and the replica set members. Please keep in mind that those communicate with each other.

So, let's say you want to use 1/3 of the RAM of your shards for connections, that'd be 5GB or 5000MB.

Then, each server connects to two members of it's replica set. These are actually 3 physical connections each. Furthermore, we have connections for managing the sharded cluster, which are 3*3=9. So we have 5000 - 6 (for the replica set) - 9 (for the management) = 4985 connections available. Since we want each mongos be relatively equaling terms of connections, we divide this by 10 (the maximum number of mongos instances we can expect) and get 498, rounded down. That is a relatively normal value, but this of course depends on the use case. If you have a lot of or very big indices, your 15GB of RAM may actually be a bit small.

As per the rule that there should be at least as many database connections available as application threads, your AppServers should be configured accordingly.