MongoDB load balancer for the Replica set

428 views
Skip to first unread message

avinash....@gmail.com

unread,
May 5, 2017, 8:07:04 AM5/5/17
to mongodb-user

Hi Folks,

In replica set cluster of MongoDB how can i ensure quick response for a concurrent users when my primary is busy in serving another request?

Do i need to use load balancer, or the mongodb itself route the query to available Secondary?

Thanks

Jim Reitz

unread,
May 5, 2017, 11:43:27 PM5/5/17
to mongodb-user
To my knowledge, there is no load balancer for MongoDB. Generally speaking, for a non-sharded system, you want to make sure all of your queries are as fast as possible in order to minimize any blocking due to locking or loading. In some cases, sharding your collection can improve your queries per second if your queries use the shard key (I.e., your queries contain each field in your shard key as query parameters).

Also in general, I don't think they recommend you sending your queries to the secondaries (or maybe this recommendation is only for when you have a sharded collection). However, if you did want to do this, it's accomplished through the client read-preference configuration
https://docs.mongodb.com/manual/core/read-preference/

Jim

Kirk Dawson

unread,
May 6, 2017, 9:40:07 AM5/6/17
to mongodb-user
As Jim says you can achieve "load balancing" by the use of read preference, How you achieve that depends upon the ORM your application is using to access the database.

Yes Mongodb documentation does discourage this but it is a valid approach if your user is happy to receive stale results. It is not appropriate in a banking style application, But works well for many research application where one missed result out of millions is not important.

Pravin Dwiwedi

unread,
May 31, 2017, 2:41:02 PM5/31/17
to mongodb-user
You can think about Tagged Shards and Tagged replication to control read and write for intended host having a particular Tag. you will have to be very careful while tagging the hosts and choosing the shard keys otherwise there may be performance related issues.

Here you go--

Kevin Adistambha

unread,
May 31, 2017, 9:34:23 PM5/31/17
to mongodb-user

Hi,

As most of the points were covered by everyone, I just wanted to add the main reason why secondary reads are not recommended for the general case. Other than stale reads as mentioned by Kirk, secondary reads can interfere with the replica set’s high availability function.

This blog post explains the reasoning in detail: Can I use more replica nodes to scale

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages