Mongoose stops responding after unspecified period of time Mongoose stops responding after unspecified period of time azure azure

Mongoose stops responding after unspecified period of time


OK, so I haven't had the issue for a while now. I made a lot of small changes to the connection settings, but I think what had the biggest impact was either explicitly setting the ha (high availability) and haInterval replset options, or possibly moving the connection code to the very end of my app.js.

Here's what the final connection code looked like:

// Connect to DBvar connectionOptions = {    replset: {        socketOptions: {            connectTimeoutMS: 300000, // 5 minutes            keepAlive: 120        },        ha: true, // Make sure the high availability checks are on        haInterval: 10000, // Run every 10 seconds    }};//mongoose.set('debug', true);mongoose.connect(envConfig.app.db, connectionOptions, function (err) {    if (err) winstonLogger.error(err);});mongoose.connection.on('connecting', function () {    console.log('Connecting to MongoDB...');});mongoose.connection.on('connected', function () {    console.log('MongoDB connected!');});mongoose.connection.on('open', function () {    console.log('MongoDB connection opened!');});mongoose.connection.on('error', function (err) {    winstonLogger.error(err);    mongoose.disconnect();});mongoose.connection.on('disconnected', function () {    winstonLogger.error('MongoDB disconnected!');    mongoose.connect(envConfig.app.db, connectionOptions, function (err) {        if (err) winstonLogger.error(err);    });});mongoose.connection.on('reconnected', function () {    console.log('MongoDB reconnected!');});mongoose.connection.on('close', function () {    winstonLogger.error('MongoDB closed');});if (mongoose.connection.db.serverConfig.s.replset) {    mongoose.connection.db.serverConfig.s.replset.on('ha', function(type, data) {        console.log('replset ha ' + type);    });    mongoose.connection.db.serverConfig.s.replset.on('timeout', function () {        winstonLogger.error('MongoDB timeout');    });}

I also turned on the AlwaysOn option for the Azure WebApp (which didn't seem to have much effect, but I'm not switching it off now that it's working!).

It does seem like the issue had something to do with not reconnecting after a failover, but I couldn't say with 100% certainty.

Hopefully this will help someone else out who runs into the same problem... and hopefully I haven't jumped the gun only to have it stop working again in a few days (I'll be back if that happens!).

Thanks to everyone who provided a suggestion, I am ETERNALLY grateful :)


This sounds a great deal like an issue I had for months with the same setup (NodeJs on Azure, Mongoose, MongoLab replica set). It was "simultaneously brain-numbing, heartbreaking and unbelievably infuriating", for sure. I finally solved it a few months ago after way too many hours of frustration...

I found that it was triggered by replica set failovers. This was verified by using a test server on Azure and test replica set on MongoLab and manually triggering failovers on Mongolab (I could not repro by running the NodeJs server and MongoDB instance locally, since the problem has something to do with connection times).

The high availability checks that are supposed to help Mongoose to connect to the secondary after a failover weren't running correctly. Due to some kind of race condition, if the server takes a while to boot up after initializing the Mongoose connection, the callback to start the checks fails in mongodb-core.

What I ended up doing is moving the Mongoose connection as late into the server startup process as possible. It used to be before a pretty large file minification task, which was likely the problem.

Hope this helps!

My Mongoose settings:

// OptionsmongooseOptions.replset = {  ha: true, // Make sure the high availability checks are on  haInterval: 5000, // Run every 5 seconds  socketOptions : {    ... // nothing special here  }}mongoose.connect(mongodb_uri, mongooseOptions);// Make sure the high availability checks are occuring// Should see "replset ha start" "replset ha end" pairs every 5 secs// Well, hopefully.if (mongoose.connection.db.serverConfig.s.replset) {  mongoose.connection.db.serverConfig.s.replset.on('ha', function(type, data) {    console.log('replset ha ' + type);  })}


Per my experience, I suggest that you could ping the mongodb on Mlab to make sure whether running out of mongo connections when your webapp has been hanging.

I think you could try to add the connection pool option for mongoose connections, as @BlakesSeven said. Please see the section Connection pools of Mongoose guide topic Connections to configure the pool size.