jwerre
jwerre

Reputation: 9594

Should I pool db connections or create new connection per cluster?

I've have an Node.js app running on multiple processes and I'm wondering if I should pool my MongoDB (using mongoose) connections our should create a new connection for each cluster fork.

Here's an example:

should I create a new DB connection for each fork like this:

if (cluster.isMaster) {
  var totalCPUs = os.cpus().length;
  var forks = 0;
  while (forks < totalCPUs) {
    cluster.fork();
    forks++;
  }
  cluster.on('exit', function(worker) {
    console.error("Cluster worker " + worker.id + " exited unexpectedly.");
    return cluster.fork();
  });
} else {
  mongoose.connect(uri);
  ...
  app = express()   
  http.createServer(app).listen(8080)
}

or create a single connection with poolSize

if (cluster.isMaster) {

  var totalCPUs = os.cpus().length;
  mongoose.connect(uri, { server: { poolSize: totalCPUs });

  var forks = 0;
  while (forks < totalCPUs) {
    cluster.fork();
    forks++;
  }
  cluster.on('exit', function(worker) {
    console.error("Cluster worker " + worker.id + " exited unexpectedly.");
    return cluster.fork();
  });
}else{
  app = express()   
  http.createServer(app).listen(8080)
}

Upvotes: 2

Views: 1474

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311865

Your code isn't sharing a common pool in the second case, you're just separately creating pools for the master and each worker process. There isn't a way to share a single pool of MongoDB client connections across your cluster.

So assuming you're not actually using mongoose in the master process, it's better to only call mongoose.connect in the worker processes like you are in the first case, with the pool sized however you want it for each process.

Also note that poolSize defaults to 5, so even if you don't include the poolSize option, mongoose.connect is still creating a pool of connections.

Upvotes: 2

Related Questions