Geek
Geek

Reputation: 1344

MongoDB load balancer for the Replica set

In replica set cluster of MongoDB how can i ensure quick response for a concurent 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

Upvotes: 12

Views: 17101

Answers (2)

Vince Bowdren
Vince Bowdren

Reputation: 9208

You don't need to use a load balancer, or to route queries to secondary nodes; the primary node can handle concurrent queries by itself:

  1. MongoDB supports concurrent queries, both reads and writes, using a granular locking system
  2. It is not advised to use secondaries to provide extra read capacity, as replication design makes this inefficient and unreliable for most use cases
  3. If your primary is taking a long time serving a single request, in such a way that it locks out other requests, that should be addressed by redesigning an inefficient query or adding suitable indexes.
  4. If your server is struggling to serve multiple users despite the queries being optimised, look at whether your hardware is insufficient for the job
  5. If you still find that you need to scale out your reads and writes, the recommended way to do that is by sharding, not by using other nodes of a replica set.

Upvotes: 21

Amit Yadav
Amit Yadav

Reputation: 1039

Normally writes are handled by master and reads should be send to secondaries by setting read preference. Although it might take some negligible time to get data propagated to secondaries, as secondaries use oplog copy for data replication.

You do not need any load balancer, Mongo is capable of doing these things. Read more about it here -

https://docs.mongodb.com/manual/replication/

Upvotes: 3

Related Questions