mongoose: Failing to automatically re-connect to New PRIMARY after a replica set failover , from Mongoose (MongoDB, NodeJS Driver)

I am not sure if this is a bug or some mis-configuration from my end. How to solve this ?

I made a simple NodeJS App, with Mongoose as MongoDB Driver. And connected to a mongodb replica set. The App is working fine until I shut down the current primary, When the PRIMARY is down, the replica set automatically elected a new PRIMARY. But, after that the node application doesn’t seems to be responding for DB queries.

CODE: DB Connection

var options = { 
    server: { 
        socketOptions: { 
            keepAlive: 1, 
            connectTimeoutMS:30000, 
            socketTimeoutMS:90000 } 
        },
    replset: { 
        socketOptions: { 
            keepAlive: 1, 
            connectTimeoutMS : 30000 , 
            socketTimeoutMS: 90000  
        }, 
        rs_name: 'rs0' 
     } };

var uri = "mongodb://xxx.xxx.xxx.xxx:27017,xxx.xxx.xxx.xxx:27017,xxx.xxx.xxx.xxx:27017/rstest";

 mongoose.connect(uri,options);

CODE: DB Query

  router.('/test',function(req,res){

     var testmodel = new testModel('test') ;
     testmodel.save(function (err, doc,numberAffected) {
         if (err) {
             console.log("ERROR: "+ err);
             res.status = 404;
             res.end;
         }else{
             console.log("Response sent ");
             res.status = 200;
             res.end;
         }
     });
 });

Steps Followed

Created a MongoDB replica set in three VMs. Created a simple nodeJS App (Express + Mongoose) with a test API as above Sent GET request to ‘test’ continuously with some time interval to the app from a local system. Took the PRIMARY instance down APPLICATION STOPPED RESPONDING TO REQUESTS Varsions: “express”: “4.10.6”, “mongodb”: “1.4.23”, “mongoose”: “3.8.21”,

A sample app that I have done for debugging this issue is available at https://melvingeorge@bitbucket.org/melvingeorge/nodejsmongorssample.git

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 36 (2 by maintainers)

Most upvoted comments

There was a bug with the mongodb driver that affected connections to new replica set primaries. It was fixed in https://jira.mongodb.org/browse/NODE-818

However, mongoose versions from 4.6.0 to 4.7.2 inclusive were using a version of the mongodb driver that had the bug.

This affected my project, which is running mongodb 3.0.12. If you have issues where mongoose fails to reconnect after a step down, check what version of mongoose you’re using.

Hopefully this helps someone.

@arv1989 I don’t think that is the case. I use private IPs for all my nodes and I still have the issue.

Heya,

So in theory, this should work fine once a new primary is elected. Looking at your code, I don’t see where you’re using the configuration options you specified. Frankly, it doesn’t look like you’re actually connecting with mongoose at all, looks like you’re connecting with monk and default options. The following example works fine for me even when I kill off the primary:

var mongoose = require('mongoose');

mongoose.connect('mongodb://hostname:27000,hostname:27001,hostname:27002');

mongoose.model('Parent', new mongoose.Schema({ name: String }));

var http = require('http');
http.createServer(function (req, res) {
  mongoose.model('Parent').findOne(function(error) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Error: ' + error + '\n');
  });
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

Can you verify if the above code continues to be responsive with your setup when a primary gets taken down?