MongoDB load balancer for the Replica set

Geek picture Geek · May 5, 2017 · Viewed 11.1k times · Source

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

Answer

Vince Bowdren picture Vince Bowdren · May 5, 2017

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.