Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failover with mongodb

I have to setup a database that can handle failover (if one crashes the other takes over). For that, I decided to use mongodb: I set up a replica set with two instances. Each instance is running on a separate VM. I have several questions:

  • It is recommended to use at least 3 instances in a replica set. Is it ok to use only two ?

  • I have two instances, and then two IP addresses. Which IP should I give to my application that will need to read/write in the database ? When a database is down, how the request will be redirected to the instance that is still up ?

Some help to get started would be great !

like image 882
rmonjo Avatar asked Feb 10 '13 15:02

rmonjo


People also ask

What is failover MongoDB?

Automatic Failover is a sub-feature of Replication that provides fault tolerance and persistence. Here is what a typical replica set looks like: The primary node receives all write operations. A replica set can have only one primary capable of confirming writes.

Is MongoDB a fault tolerance?

Replication — MongoDB ManualFault tolerance, high availability, and automatic failover broadly describe the same behaviour although they may have slightly different connotations. The concept of High Availability includes durability, redundancy, and automatic failover with minimal or no downtime.

What is MongoDB Replicaset?

A replica set is a group of mongod instances that maintain the same data set. A replica set contains several data bearing nodes and optionally one arbiter node. Of the data bearing nodes, one and only one member is deemed the primary node, while the other nodes are deemed secondary nodes.

How does MongoDB achieve high availability?

Replication is performed in MongoDB to maintain data redundancy and high availability. It is performed by creating multiple copies of your data across multiple servers. Replication is achieved by the replica sets that contain primary and secondary nodes.


1 Answers

It is recommended to use at least 3 instances in a replica set. Is it ok to use only two ?

No, the minimum requirement for a replica set is three processes (docs), but the third could be an arbiter even though it is not recommended.

I have two instances, and then two IP addresses. Which IP should I give to my application that will need to read/write in the database ? When a database is down, how the request will be redirected to the instance that is still up ?

There are two alternatives:

#1 (recommended)

You provide the driver with all addresses (for more detailed information how, visit the docs), example with nodejs driver (it is similar with the other). This way, the driver will know all, or at least more then one of, the instances directly, which will prevent problems if all of the specified instances are down (see #2).

var MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://[server1],[server2],[...]/[database]?replicaSet=[name]', function(err, db) {
});

#2

You provide the driver with one of them (probably the primary) and mongodb will figure out the rest of them. However, if your app starts up when the specified instance(s) is down, the driver would not be able to find the other instances, and therefore cannot connect to mongodb.

like image 95
Mattias Avatar answered Sep 24 '22 17:09

Mattias