Mongodb – Can we have two primaries for the same shard in mongodb

mongodb

Can we have a Mongodb deployment in two data centers in which the data set is divided into two shards A and B.

Data Center 1 | Data Center 2

Shard A, Shard B | Shard A, Shard B

So some clients connect to Data Center 1 and write to the primary of Shard A and some other clients connect to Data Center 2 and write to the primary of Shard A there. Then some process does the reconciliation between Shards in two data centers.
The advantage of this setup is that clients can do a write to the data center that is closest to them and they can still read the latest data which is combined from Data Center 1 and 2.

Best Answer

MongoDB does not currently support a multi-master setup such as you describe.

If you were looking to provide localized regional writes via MongoDB, your best bet is to setup a sharded collection, using shard tag ranges. You would have 1 (or more) shard per region, with the primary residing within the region it manages and remote secondaries. Region would be the first member in your shard key and you would then peg a region's range to the shard that owns that region.

Reads for that region's data performed from a different region could then use a read preference of "nearest", which would result in a secondary read to the local secondary.