I have an application written in java with tomcat that I am ready to start scaling for production. As I understand it, i shouldn't use a load balancer with mongodb router instances as it can cause issues.

However, I understand that at some point I will need more than one Mongo router.
What is the best way to go about this?

The only way I can think of is when I run multiple copies of my Tomcat server, use a load balancer to route traffic to one of those Tomcats, and have each Tomcat pre-configured to use a specific mongodb router. However, I don't think this will work very well, as if a mongodb router goes down then there could be errors, and if a tomcat goes down then I am wasting a mongodb router.


As a practical matter you won't need more MongoDB router instances until you have a LOT of servers (each doing a LOT of traffic). Assuming you get to that point though the problem is really a lot easier to solve than you think it is.

Expanding on what MrKurt said, what you would typically do is split your Tomcat servers into groups, and then configure each Tomcat server group with a set of MongoDB routers for failover.

So if you have 30 Tomcat servers you could split them into 3 Tomcat groups of 10.
Then set up a Mongo router for each group, and configure something like:

Tomcat Group 1 -> Mongo 1, Mongo 2, Mongo 3
Tomcat Group 2 -> Mongo 2, Mongo 3, Mongo 1
Tomcat Group 3 -> Mongo 3, Mongo 1, Mongo 2

  • Each Tomcat server group is pointing at a different Mongo router
    So you're not beating up one single front-end with all your requests.

  • If the default Mongo router for a group fails you fail over to the next one
    This is handled by the MongoDB connection driver.
    Note the staggering of the failover chain as shown above. This prevents you from dumping excessive load onto any one server in a single-fault condition.

  • If you lose a single tomcat instance you're not "wasting" the Mongo router
    You would have to lose all Tomcat instances in a group in order to have one of the Mongo routers go idle (and even then it's not "wasted": It's still redundant capacity in the event of a failure of another Mongo router instance.


Most Mongo drivers these days can handle multiple Mongos's in a connection string, the Java driver implemented this as of 2.9.0: https://jira.mongodb.org/browse/JAVA-381

It's only for failover, however, not load balancing. You normally do not need to load balance between Mongos's, the bottlenecks are typically on the backend shards in a well optimized setup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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