6

I've got an app hosted on Windows Azure with the MongoLab add-on and I have an issue where the app works perfectly well after I've deployed it, however after a period of a couple minutes inactivity I get a database error:

No replica set primary available for query with ReadPreference PRIMARY

This error does not go away until I restart the website where the same pattern returns - works perfectly until a couple idle minutes when the error returns

My basic app code:

mongoConnect(process.env.CUSTOMCONNSTR_MONGOLAB_URI, {
server: {
    auto_reconnect: true,
    socketOptions: {
        socketTimeoutMS: 120000,
        keepAlive: 1
    }
},
replSet: {
    socketOptions: {
        socketTimeoutMS: 120000,
        keepAlive: 1
    }
}
}).then(function (db) {
    //app configure ...

    http.createServer(app).listen(app.get('port'), function () {
        console.log("Express server listening on port " + app.get('port'));
    });
});

*mongoConnect is a simple "promisified" function of mongoclient.connect

I understand this is best practice, I'm establishing one connection to the db and then reusing that db object for all requests. From reading the likes of https://support.mongolab.com/entries/23009358-Handling-dropped-connections-on-Windows-Azure I've set my sockettimeouts to 2 minutes to prevent any firewalls prematurely killing sockets and auto reconnect to true so they reconnect, however neither setting solves the problem.

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
Eriedor
  • 391
  • 2
  • 13
  • Do you use trial account? – Andrei May 02 '13 at 09:27
  • Yes it is on a trial account with Azure, shouldn't make a difference though – Eriedor May 02 '13 at 09:53
  • I noticed that my VMs where getting unavailable for some period of time on trial account. I switched to Pay-As-You-Go, and now it's all good. When trial lasts it should be free, try to switch. – Andrei May 02 '13 at 09:56
  • I'm seeing the same error and similar behavior using mongolab with Heroku – Anton I. Sipos Jun 26 '13 at 22:36
  • Isn't it possible that you are simply experiencing traditional replica set failover when the primary is going down for maint., reboot, etc.? This would take the replica set out of order for a good 20-60 seconds in these scenarios. – Jeff Wilcox Sep 19 '13 at 19:53

0 Answers0