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.

1 Answer 1

  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.

  • Hmm ok, i use node.js mongoose, I just added two urls of the replicas.. If repl1 goes down, and repl2 is elected, the driver would switch to using repl2 is that correct?
    – code ninja
    Jan 23, 2014 at 14:07
  • 2
    Exactly, there will be a pause during the election, but that's how it works. BTW, MongoDB recommends at least 3 members in your cluster, to avoid deadlocks in the voting.. a lightweight way of doing that is setting up a copy of mongoDB to run as an Arbiter, who does nothing besides participate in the elections for Primary, and obviously will not vote for himself.
    – NickW
    Jan 23, 2014 at 14:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .