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 !
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.
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.
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.
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.
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 ?
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) {
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With