Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (47.6k points)

I have a REST service built in node.js with Restify and Mongoose and a mongoDB with a collection with about 30.000 regular sized documents. I have my node service running through pmx and pm2.

Yesterday, suddenly, node started crapping out errors with the message "MongoError: Topology was destroyed", nothing more. I have no idea what is meant by this and what could have possibly triggered this. there is also not much to be found when google-searching this. So I thought I'd ask here.

After restarting the node service today, the errors stopped coming in. I also have one of these runnings in production and it scares me that this could happen at any given time to a pretty crucial part of the setup running there...

I'm using the following versions of the mentioned packages:

  • mongoose: 4.0.3

  • rectify: 3.0.3

  • node: 0.10.25

2 Answers

0 votes
by (106k points)

In order to keep Mongodb's connection alive in production you can use the following way:

var options = { 

server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } }, 

replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } } 

}; 

mongoose.connect(secrets.db, options);

0 votes
by (107k points)

Your node server's connection to your MongoDB instance was getting interrupted while it was trying to write to it so by default, mongoose will try to reconnect for 30 seconds then stop retrying and throw errors until restarted.

You can just change this by updating these 2 fields in the connection options

mongoose.connect(uri, 

    { server: { 

        // sets how many times to try reconnecting

        reconnectTries: Number.MAX_VALUE,

        // sets the delay between every retry (milliseconds)

        reconnectInterval: 1000 

        } 

    }

);

Related questions

0 votes
1 answer
0 votes
3 answers
asked Sep 13, 2019 in SQL by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Sep 6, 2019 in SQL by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...