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.
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
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.
The 'load balancing' is achieve via sharding. By sharding you actually distribute writes/updates onto individual shards. There is no specific algorithm that does this as mongo will let you partition the data based on whatever key(s) combination you want. The best partition algorithms are the ones that have a compound of sequential entries plus a random one.
For example a user id can be partitioned as follow
Be aware that to implement sharding you need to have three config servers and the data nodes. The data nodes can be grouped into replica sets for redundancy and can be used (only if you can) to read data from secondaries. I say only if you can to read data because the replication is done asynchronous so is no guarantee that the new data will be available at the query time on the secondaries.
Keep in mind that the partition algorithm is totally up to you and should suit your application needs. Also you should consider if you just want to write the data and only on have occasional reads or you need to read it just after you wrote it.