Nilesh
Nilesh

Reputation: 2180

MongoDb Replication and failover

We have two servers to use for MongoDB database where we want replication and failover.

case 1:

for replication, we have setup Server1 as primary and server2 as secondary...All works fine.

Expectation: when server1 goes down, server2, which is secondary not become primary automatically..it remains secondary. so Is there possibility that server2 will automatically become primary.

case 2: will I need 3 servers compulsory for MongoDB replication so that when server1 goes down, server2 will automatically become primary and server3 will remain secondary. (this works fine)

So, any suggestion for this if we have only 2 servers??

Upvotes: 2

Views: 8580

Answers (2)

kevinadi
kevinadi

Reputation: 13765

when server1 goes down, server2, which is secondary not become primary automatically..it remains secondary. so Is there possibility that server2 will automatically become primary.

In a 2-node replica set, no.

will I need 3 servers compulsory for MongoDB replication so that when server1 goes down, server2 will automatically become primary and server3 will remain secondary.

Yes.

MongoDB replica set was designed to provide high availability and redundancy. To achieve this, a replica set will have a single Primary node (which writes will go to), and Secondaries (which can take over should something happen to the Primary).

To achieve this, MongoDB uses a voting mechanism, and a node must receive the majority vote to become Primary. An offline node will cast no vote.

Thus in a replica set with 3 nodes, you can have one node offline and still have a Primary. In a 5 node setup, you can have 2 offline nodes, etc.

This design was done in order to prevent what is called a "split brain" scenario due to a network partition, where you have two Primaries, and two apps wrote to the two Primaries simultaneously. Once the partition clears, it is impossible to tell which Primary contains the correct data. To prevent this, MongoDB will not allow writes instead to protect your data's consistency.

You may be able to use an Arbiter node if you only have 2 data-bearing nodes. However, please bear in mind that there are caveats with working with an Arbiter, such as doing writes with w:majority (this is offtopic for this discussion, but please feel free to create a new question regarding this).

Upvotes: 6

Devesh
Devesh

Reputation: 4550

With 2 servers also you can workout the solution by setting

Priority of the secondary as 0

https://docs.mongodb.com/manual/core/replica-set-priority-0-member/#replica-set-secondary-only-members . In this case in case primary is down you will have NO availability. Because i think you do not want availability as you do not want your secondary to become primary ever. My solution should work for you.

Upvotes: 0

Related Questions