Is it normal to have loads of connections between node and mongo when using mongoose?

Problem

I am trying to figure out a performance issue and I was wondering if it is normal to have around 30 (I guess 15 per cpu on 2 cpus in cluster configuration) different connections. That number seams to be consistent but I am not sure why.

Problem courtesy of: Pass

Solution

Firstly, make sure you are closing your connections when the Node process restarts. Something like this:

process.on('SIGINT', function() {
  mongoose.connection.close(function () {
    console.log('Mongoose default connection disconnected through app termination');
    process.exit(0);
  });
});

There's more on managing a default Mongoose connection here: http://theholmesoffice.com/mongoose-connection-best-practice/

Also note that you can specify the poolSize per connection. The default is 5.

var uri = 'mongodb://localhost/test';
mongoose.createConnection(uri, { server: { poolSize: 4 }});

http://mongoosejs.com/docs/connections.html#connection_pools

Solution courtesy of: Simon Holmes

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.