Failing to automatically re-connect to New PRIMARY after a replica set failover , from Mongoose (MongoDB, NodeJS Driver)
Problem
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 [code block] CODE: DB Query [code block] 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
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Automatic Reconnection Logic for Mongoose with Replica Set Failover
The application fails to reconnect to the new primary after a failover because the Mongoose connection does not automatically handle replica set changes. This can occur if the connection options are not configured to allow for automatic reconnection or if the application does not properly listen for the 'disconnected' and 'reconnected' events.
Awaiting Verification
Be the first to verify this fix
- 1
Update Mongoose Connection Options
Modify the Mongoose connection options to include 'autoReconnect' and 'reconnectTries' settings. This ensures that Mongoose will attempt to reconnect to the new primary automatically after a failover.
javascriptmongoose.connect('mongodb://<replicaSetHost1>,<replicaSetHost2>,<replicaSetHost3>/dbname', { useNewUrlParser: true, useUnifiedTopology: true, autoReconnect: true, reconnectTries: 5, reconnectInterval: 1000 }); - 2
Listen for Connection Events
Add event listeners for 'disconnected' and 'reconnected' events to handle connection state changes. This will allow you to log the connection status and take any necessary actions when the connection state changes.
javascriptmongoose.connection.on('disconnected', () => { console.log('MongoDB disconnected!'); }); mongoose.connection.on('reconnected', () => { console.log('MongoDB reconnected!'); }); - 3
Test Failover Handling
After implementing the above changes, perform a failover test by shutting down the current primary and observing the application's behavior. Ensure that the application logs indicate reconnection attempts and successful reconnections.
none// No specific code snippet needed; monitor application logs. - 4
Upgrade Mongoose Version
Consider upgrading to a more recent version of Mongoose and MongoDB driver to benefit from improved connection handling and features. The current version (3.8.21) is outdated and may lack critical updates.
bashnpm install mongoose@latest
Validation
To confirm the fix worked, perform the failover test again. The application should log 'MongoDB reconnected!' and continue to respond to API requests without interruption after the primary is switched.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep