Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 11 down vote accepted

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 (, 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.

share|improve this answer

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

xx-sha1(user email) 
xx = time sequence

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.