Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB read preference - nearest vs tag set

Tags:

mongodb

I have my MongoDB cluster running in 3 DC's.

DC1 - 3 Nodes DC2 - 3 Nodes DC3 - 3 Nodes

Also, running my applications in each nodes.

I was going through the MongoDB documentation and confused b/w nearest and tag set.

https://docs.mongodb.com/manual/reference/read-preference/

Query From Geographically Distributed Members If the members of a replica set are geographically distributed, you can create replica tags based that reflect the location of the instance and then configure your application to query the members nearby.

For example, if members in “east” and “west” data centers are tagged {'dc': 'east'} and {'dc': 'west'}, your application servers in the east data center can read from nearby members with the following read preference:

db.collection.find().readPref('nearest', [ { 'dc': 'east' } ]) Although nearest already favors members with low network latency, including the tag makes the choice more predictable.

Based on my understanding. If we use nearest, Driver will keep track of latency (maxStalenessSeconds also) to decide where to send traffic. If DC1 is overloaded or latency is bad with DC1, Driver will route traffic to other DC. But, if we use tag set, then we are forcing to go with local DC and local applications will be considered as down if local nodes are down. Why do we still recommend tag set than nearest?

So, How driver finds latency and maxStalenessSecods? How does it calculate latency? Will it keep pinging each node in the cluster? Can we configure the ping interval and number of retries before deciding the nodes based on latency?

like image 825
user1578872 Avatar asked Sep 17 '18 18:09

user1578872


1 Answers

When you use readPreference of nearest, you can specify maxStalenessSecods e.g. 100 . minimum should be 90 otherwise it'll throw an error

Latency is calculated when selecting a node to query, so it doesn't ping based on intervals.

When there is a primary:

The client estimates how stale each secondary is by comparing the secondary’s last write to that of the primary

When there is no primary:

by comparing secondary's last write to the secondary with the most recent write

like image 93
Aman B Avatar answered Oct 21 '22 21:10

Aman B