Over a million developers have joined DZone.
Platinum Partner

Load Balance Your Web Application Servers

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

I began my adventure with the need to ensure a lot of uptime for a particular web service that was running on a tomcat server. I investigated a lot of different avenues and, eventually, ended up on a colleagues blog.

Most of the information on the web about clustering tomcat servers tells you that you need to use the mod_jk apache module. This is not true and, in some cases, not what you really want. One such example can be found at JDBC Tutorial, which isn't a bad way to go. I started doing that and then a colleague of mine directed me to one of his blog entries which is mainly geared toward clustering Glassfish servers but the concept and the process for configuring Apache is the same.

There are a couple of things that he left out, however.

1. You must make sure that you load the proxy load balancer. For instance, in your httpd.conf file there is a section where the apache modules are loaded. Make sure you have a LoadModule for the load balancer:

LoadModule proxy_balancer_module /modules/mod_proxy_balancer.so

2. When you are configuring your cluster as in Manfred's article, consider where you are putting these rules. They have to be included in the startup. So, either in the httpd.conf or maybe a rewrite rules file.

3. The and configurations in Manfred's article contain a couple of mistakes (sorry Manfred). The "Order" parameter does not allow spaces after the comma. So, then the configuration becomes:

Order deny,allow
Allow from all

Order allow,deny
Allow from all

4. Pay attention to what you have put in your tag. This is the context you will use to access the load balancer management console. More on that later....

5. The balancer is defined in the tag. This is referenced in the ProxyPass parameter. Also in the ProxyPass parameter is the context of the application that needs to be load balanced. In Manfred's example, /clusterjsp is the context of the application that is to be load balanced. There is a bunch of information about how to do different configurations for load balancer. A decent place to start for more information is here.

6. You define the nodes in your cluster in the section. The nodes are defined by the BalancerMember entries. These directives take the same parameters as the ProxyPass directive. So you can assign the route and the redirect path for your nodes to take. For instance:

BalancerMember http://localhost:8080 route=myroute1 redirect=myroute2
BalancerMember http://localhost:8081 route=myroute2 redirect=myroute3
BalancerMember http://localhost:8082 route=myroute3 redirect=myroute1

So, if any of your nodes fails, you can specify which route to take. Or, you can leave them off and let apache decide. This is sort of handy, though, nonetheless.

7. Once you have this all set up and working (you have restarted your apache server), the next thing to do is to take a look at the load balancer console. If you have set this up on your local machine, and we are still working from Manfred's example, the URL to this console would be this:


This will load a page where you can enable/disable nodes in the cluster and set the failover node for a particular node. Click the link for one of the nodes. A new section of the page opens to allow you to enter information about the load factor (how much load should that node get), the route (can be anything you want), and the route redirect (which node to go to if this node becomes unavailable). The route redirect is the name of the node. So, if you call your first node "node1", then the other nodes can refer to this node as "node1" in the redirect route box.

Also on this page are statistics about errors, usage, status of a particular node. Some useful information about the health of your cluster.

Finally, if you want to disable a particular node (for some maintenance) you can do so from the load balancer console.

There is lots more stuff that you can do with load balancer. If you are the least bit interested, make sure you read the Apache documentation.


The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


Published at DZone with permission of Jason Kilgrow .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}