Reputation: 24583
I am using mongoose in my nodejs application and I am wondering what I should set the poolSize to. I know there is probably no hard and fast rule but maybe based on users and usage there is a general recommendation.
I have left the poolSize left at the default which is 5. At some point I can put the server under load and see how 5 performs. I can then start really bumping it up maybe (100, 500, 1000, etc) to see what happens. Anyone else try this yet and care to share?
Upvotes: 17
Views: 11517
Reputation: 20354
Connection pool size is a cache of database connections maintained so these connections can be reused when future requests to the database are required. Connection pools are used to enhance the performance of executing commands on a database.
Note: maxPoolSize
and poolSize
are the same, except they relate to whether you are using the useUnifiedTopology: true
setting.
If you are using useUnifiedTopology: true
, maxPoolSize
is the spec-compliant setting to manage how large connection pools can be.
But if you are using useUnifiedTopology: false
(or omits it), poolSize
is the same thing but from before we had the unified topology.
Note: Each connection consumes about 1MB of RAM.
The connection pool is on a per-mongod/mongos basis, so when connecting to a 3-member replica there will be three connection pools (one per mongod), each with a maxPoolSize
. Additionally, there is a required monitoring connection for each node as well, so you end up with (maxPoolSize+1)*number_of_nodes
TCP connections.
In my opinion, if you don't care about CPU and RAM, you should use all available connections (why not if we already have them, right?).
For example: You have Atlas free cluster with 3 replica sets, that supports maximum number of 500 connections, and you have only one application that connects to it, give all connections to that one application. In order to set the value of poolSize
, you can use above calculation of connections:
poolSize = (maximum_connections/number_of_nodes) - 1
poolSize = (500/3) - 1
poolSize = 165
If you would have 2 applications that will connect to that same cluster, give each application half of connections.
If you have limited RAM memory, check how much you can spear and calculate poolSize
based on that (as I said in the note, you can assume that one connection will consume about 1MB of RAM).
Upvotes: 1
Reputation: 3754
This link would help you out. It is difficult to predict the optimal poolsize for MongoDb , i use apache benchmark tests to test the servers performance and response for different values of poolSize and get an approximation for what suits the best for given number of concurrent requests for your server .
Upvotes: 4