Mongodb: replica-set with load balancer

mongodb

Is there an option for load balancing with mongodb replica sets?
I am not asking for a HAProxy or similar, but for a built-in feature from mongodb.
Could not find tips for this in the docs.

Best Answer

If you are using sharding, then the "load balancer" is the mongos process - actually it is more like a router - it keeps an in-memory copy of the config database and can make decisions based on the shard key.

If you mean load balancing across identical replica sets or across the members of the set, then there is a feature request to have mongos handle that scenario too (https://jira.mongodb.org/browse/SERVER-1594), however given how the drivers operate it is not really necessary (it would make the drivers less complex though).

In a single replica set, you cannot distribute writes, they all must go to the primary. You can distribute reads to the secondaries already, via Read Preferences as you deem appropriate. The driver keeps track of what is a primary and what is a secondary and routes queries appropriately.