Question

I have a 3 members mongo replica-set over 2 diferent locations (connected via VPN). Here is the configuration :

  • master, datacenter A
  • slave1, priority 1, datacenter A
  • slave2, priority 0, datacenter B

With this conf, the master mongo instance will always be on datacenter A (since mongo slave 2 has a 0 priority).

I have a java web application that runs on datacenter B. When I connect, I give the mongo driver the IPs of the 3 mongo instances (to be sure to connect to one instance if any has failed).

I want to make sure that the java application that runs inside dataceneter B uses, for non consistent read operations, in priority the slave2 instance (because they are on the same datacenter) rather than any of the other 2 (as long as slave2 is up and running).

If the application (in datacenter B) uses master or slave1 for non consistent read operations while slave2 is up and running, it is a waste of resources, bandwidth, and incidentally, money.

How can I ensure that the datacenter B java application will prefer slave2 ?

Was it helpful?

Solution

You have two options using Read Preference:

Using nearest . This will make your app server reach a machine in the nearest group (15 ms ping distance of the closets member). However, if the machines on data center A are within the nearest group, those can be used for read as well. The driver recalculates the nearest group from time to time.

Adding a tag set in conjunction with nearest / secondary / secondary Preferred preference. For a better understanding read this short tutorial. You can set a "dc" tag for your data center B member, and adjust your application (java) driver to use this tag for reading accordingly.

Comment: If the app server / application is the major client of your replica set, it is better to have two members on data center B of course.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top