Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Mongoose poolSize work

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

  1. how does mongoose connection pool work
  2. Why is my poolSize doesn't get applied, and
  3. 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?
like image 369
HIT_girl Avatar asked Mar 16 '17 06:03

HIT_girl


1 Answers

How does mongoose connection pool work?

  • Most MongoDB drivers support a parameter that sets the max number of connections (pool size) available to your application. The default pool size varies from driver to driver, for Mongoose is 5. However, Mongoose might open more than poolSize connections for internal use.

Why is my poolSize doesn't get applied?

  • poolSize is the maximum number of sockets the MongoDB driver will keep open for this connection. Mongoose will automatically manage the number of connections to the MongoDB server.

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?

  • MongoDB only allows one operation per socket at a time. So, the connection pool size can be thought of as the max number of concurrent requests that your driver can service.
like image 65
NIkita Koren Avatar answered Sep 28 '22 19:09

NIkita Koren