Reputation: 875
I'm trying to improve my app's performance , and read that it's good practice to create multiple client connections so that mongodb can process queries in parallel (each client connection's queue being processed synchronously)
I'm using mongoose, and reading the connection docs, I see that you can set poolSize (default is 5). I haven't set the poolsize on my connections, so assuming it should be the default 5
var mongoOptions = {
server: {
poolSize: Number(process.env.MONGO_POOLSIZE) || 5
}
}
mongoose.connect(DATABASE_URL + "?authMechanism=SCRAM-SHA-1", mongoOptions);
Ran db.serverStatus().connections
in my mongo client, and see the below response
{ "current" : 9, "available" : 195, "totalCreated" : NumberLong(2058) }
I know that includes the mongo shell connection, but why is it 9 current connections vs 6? When I disconnect my server it goes down to 1 as expected.
Then if if I do set poolSize to 180 , I still get the 9 connections vs my settings.
Could someone explain
Upvotes: 11
Views: 12746
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.
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.
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.
Upvotes: 1
Reputation: 177
How does mongoose connection pool work?
poolSize
connections for internal use.Why is my poolSize doesn't get applied?
Does having multiple connections mean that my queries would get process in parallel by mongo db ? or does one mongoose connections means one client connection?
Upvotes: 6