Вопрос

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 !

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top