Zane Claes
Zane Claes

Reputation: 14955

Is it a good idea to have multiple Mongo connections open from a single Node.js app?

Background: I'm trying to optimize the speed of my Node.js API, built with Express and Mongoose on the Amazon Cloud. I have one API call that takes a good amount of time to run (my /stats API call compiles data from lots of sources, and thus makes hundreds of mongo queries and thus takes about 20 seconds to run). I have noticed that, while this API call is running, other API calls that also hit the Mongo replica set are slow to return. My first thought was that the stats queries were slow, thus blocking, but according to my stat panel I don't have any queries taking > 100ms to run, and also my Mongo DB stats are all in pretty healthy ranges (20+ queries per second, <1% btree miss, <5% lock).

Question: Right now, my Node.js app establishes a single connection to the Mongo set on startup, and all queries use that connection. Would it be better to establish multiple connections? Eg, should I establish a new Mongo connection for each inbound API request, instead? Or, maybe I should have a static number of connections to the replica set, and load-balance between those connections when executing queries?

Or maybe I'm completely off-base?

Upvotes: 11

Views: 17720

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311835

You absolutely should be using more than a single connection as otherwise only one query can be executing at a time. The cleanest way to do this is to simply enable connection pooling on your mongodb.Server object when creating your Db object. For example:

var serverOptions = {
    'auto_reconnect': true,
    'poolSize': 5
};

var mdb = new mongodb.Db('test', new mongodb.Server('127.0.0.1', 27017, serverOptions));
mdb.open(function (err, mdb) { ... }

The reason you're not seeing slow query times in the stats is that your concurrent queries are simply queuing up on the client side waiting for the connection to become available. That waiting time won't show up in the server-side query times.

Upvotes: 19

Related Questions