0

I have a mongodb cluster on aws. In order to reduce network latency I want to have multiple shards in different regions. In each region there will be the application nodes that connect to the localhost mongos. How can I ensure that the mongos will always send queries to the nearest shard in order to reduce latency. I found a solution using tag aware sharding that consists in tagging each replica set depending on the region and appending on all queries a field that specifies in what region the query was made from. But I was wondering if there is another solutions to configure mongos to be geographically aware of the shards. Thank you!

1 Answers1

0

There is a read preference value "nearest" which actually uses the latency between the MongoS and MongoD to determine which is the best/fastest set member for the query. You can find the documentation about it here: https://docs.mongodb.org/manual/reference/read-preference/#nearest

The selection is based on the "member selection". For a sharded cluster like yours you can find the way how the MongoS selects the node here: https://docs.mongodb.org/manual/core/read-preference-mechanics/#read-preference-in-sharded-clusters

The most important thing to notice here is, that "nearest" doesn't care about the type of a node. So even if you do a read query it is possible that the MongoS selects a primary (which sometimes isn't the way you would expect it). To fix this the only way is to use the tags you already mentioned.

Hope that helps!

Osterjour
  • 845
  • 8
  • 12