MongoDB load balancer for the Replica set
Asked Answered
D

2

12

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

Dysgraphia answered 5/5, 2017 at 11:38 Comment(0)
S
21

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.
Siderolite answered 5/5, 2017 at 15:25 Comment(5)
If it is not recommened to use replicas for extra read capacity, then what is it good for? Backup and fault-tolerance alone?Ottinger
i ask the same question. What are RS for?Attenuation
Replica sets are for redundancy and high availability.Siderolite
What if primary fails? How do you reach the new primary?Celestyna
The driver deals with that: MongoDB drivers can detect the loss of the primary and automatically retry certain write operations a single time, providing additional built-in handling of automatic failovers and electionsSiderolite
H
3

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/

Hurt answered 5/5, 2017 at 12:36 Comment(1)
Actually, the advice is not to send reads to secondaries except in certain specialised cases. See the article on read Preference for details.Siderolite

© 2022 - 2024 — McMap. All rights reserved.