MongoDB Failover Replica Set

failovermongodbnode.js

I'm newbie to mongodb replica sets and sharding..

I am setting up IP Failover on two servers. I set up DNS failover at AWS route 53 to switch the IP address in case the main server goes down. I also set up Mongo replica set across the main server and secondary server.

The problem is, the secondary server cannot write to the replica set (local instance) because it is secondary..

How could I set this up, so that when AWS switches the IP, traffic goes to the secondary server, (assuming primary mongodb is down)?

EDIT:
Also set up file synchronisation between the primary and secondary server.

Best Answer

  1. MongoDB replication doesn't work that way. When you set up the cluster the cluster will elect a Primary, and that will be the reference point for data (writes always, reads depending upon how you configure the client). If the server goes down, the remaining servers will elect a new Primary (assuming they have quorum), and sync from that.

  2. Your client, if you use an official mongoDB one, can read from any Primary or Secondary (if configured to do so), and the cluster itself will tell your client that the Primary has changed, so that it can write to it.