Hi,
If I deploy a MongoDb replica set across only 2 data centers, if one entire data center fails, can there be automatic failover to the other data center?
As pointed out by Rhys, automatic failover will happen with the set still accepting writes (i.e. with an active primary) if the failing data center contains fewer members vs. the operational data center.
Using your example, if datacenter 2 went offline, the remaining 3 members of the replica set still forms a majority (3 out of 5) and can elect a primary between themselves.
However, should datacenter 1 went offline, there is not enough members in the remaining datacenter to elect a primary (2 out of 5), thus it cannot elect a primary, and the set will become read-only.
This design is intentional, since you can never be sure if datacenter 1 is really offline, or is there a network issue preventing communications between the two datacenters. If you allow members in datacenter 2 to elect a new primary, you could potentially have two primaries in the replica set operating simultaneously. In this “split brain” scenario, it’s difficult to reconcile the writes that happened between the two primaries. Hence a replica set requires a majority vote to be able to elect a primary.
Adding a third data center to house the arbiter is not an option.
Unfortunately you cannot have it both ways and have automatic failover. You cannot have high availability with a guarantee that no split brain scenario can occur, while at the same time require that no arbitration could take place. Without an arbiter, manual intervention will always be required, since there is no way for the members of a replica set to know whether a datacenter is truly offline or it’s just a temporary network issue that could last only a short moment.
One possible intervention method would require you to manually reconfigure the replica set in datacenter 2, by removing two of the members in datacenter 1. After this reconfiguration, it will appear that only one member is offline, and the remaining members in datacenter 2 can thus elect a new primary (since it will be 2 votes out of 3).
Having said that, adding/removing members manually involve a certain amount of risk, not to mention that MongoDB replica set was never designed to work like this.
Please note that it’s generally not recommended to perform writes to a member of a replica set while being started as a standalone node. On another note, the old master/slave model was deprecated and is not recommended anymore for new deployments.
For more information, please see Replica Set Deployment Architectures and Replica Sets Distributed Across Two or More Data Centers.
Best regards,
Kevin