MongoDB Multiple Masters in ReplicaSet

DanielH picture DanielH · Apr 10, 2014 · Viewed 17.4k times · Source

I've got a question concerning multiple masters in a replicaSet with MongoDB. I have the following layout:

Server A --> with MongoDB & several applications

Server B --> with MongoDB & several applications

Both instances of MongoDB are organised in the same replica Set (Server A as Primary, Server B as Secondary). But now there is the problem. Both databases should contain data from the applications on the server.

Is it possible to deploy a replica Set with two masters so that the data from Server A is available in MongoDB at Server B and vice versa?

Thank you very much in advance

Answer

drmirror picture drmirror · Apr 10, 2014

Replica sets in MongoDB can only have a single master at this point. (It is called the primary of a replica set.) For your scenario, the solution is often to use a sharded cluster. In your example, you would have two shards: one for the data of server A, and the other for the data of server B. Both shards are implemented as replica sets, so each has a minimum of three servers. You would then put the primary of the A shard in the same data center A, and the primary of the B shard in data center B. At least one replica of each shard (called a secondary) would be located in the other data center.

This means that all the data is available in each data center, but writes to the A shard always need to happen in data center A, and writes to the B shard in data center B. (Although writes can also be done remotely, so you can write to shard A from data center B, it's just that it's a remote write in this case.)