Java EE Load Balancing with Tomcat and Apache

This tutorial explains how to configure an Apache HTTPD server to map a specific path on a series of load-balanced Apache Tomcat.
The first step is to define the Virtual Host in the Apache configuration files.
In this case the root directory (on file system) of the site is located in /path/to/your/site/, the name of the site is and the path where the Tomcat servers may be reached is /javaee.

In few words, an URL like is mapped on the file /path/to/your/site/home.html.

An URL like is mapped to the hello.jsp file contained in javaee.war application deployed on all the Tomcat servers defined in the load balanced cluster.

The configuration of the Apache virtual host:

<VirtualHost *>
	ServerAdmin webmaster@localhost
	DocumentRoot /path/to/your/site/
	<Directory /path/to/your/site/>
		Options MultiViews
		AllowOverride All
		Order allow,deny
		allow from all

	ErrorLog /var/log/yoursite-error.log

	LogLevel warn

	CustomLog /var/log/yoursite-access.log combined

    <Proxy balancer://tomcatservers>
	BalancerMember ajp:// route=tomcatA retry=60
        BalancerMember ajp:// route=tomcatB retry=60
	BalancerMember ajp:// route=tomcatC retry=60

    <Location /javaee>
	Allow From All
        ProxyPass balancer://tomcatservers/javaee stickysession=JSESSIONID nofailover=off


The most important settings are Proxy and Location.
In Proxy it’s defined a load balancer made with 3 tomcat servers and an URL is assigned to the balancer, in this case balancer://tomcatservers.

The balancer has three members, everyone with its own URL based on the ajp protocol. In this case Apache will connect to the Tomcat servers on their AJP connectors (an alternative would be to use their HTTP connectors).

The Tomcat servers run on the hostname and each of them opens its own AJP connector on a different port: the first on 8009 (the default one), the second on 8010, the third on 8011 (obviously if they run on the same hostname/IP they must bind to different ports).

Each Tomcat is identified by a route name: tomcatA, tomcatB and tomcatC. The importance of it will be explained later.

In the Location section, a specific path /javaee of the virtual host is mapped on the previously defined balancer balancer://tomcatservers/javaee. So when someone asks for the virtual host will request that JSP to a randomly chosen Tomcat in the balancer members.

What’s the stickysession attribute? It’s a very useful configuration parameter used in conjunction with the route attributes, defined before.

As probably every Java EE (or Web) developer should know, while browsing on a server, it keeps trace of some data about the browsing session in a server-side HttpSession object. For example an ecommerce web application needs to store somewhere the information about the shopping cart of non registered users.

How the server can associate the remote session data with the specific navigation session? This is done through a cookie (or via a GET parameter in the URL) that gives to the server the session ID value.

In Java EE applications, the cookie name to identify the sessions is JSESSIONID.

This is closely related to the management of the load balancing between the Tomcat servers.

If Apache picked randomly one of the Tomcat to handle a single request and if the next request from the same user/browser was forwarded by the balancer to another Tomcat in that battery, things wouldn’t work correctly.

Each Tomcat doesn’t know anything of the existence of other Tomcat in that balancer configuration and especially a single Tomcat server cannot access the information of http sessions handled by another Tomcat.

In few words, when a Tomcat is chosen to handle the first request from a user/browser, it’s absolutely required that, to keep valid session data, the same Tomcat must be used to handle the following requests coming from that browser/user.

If not, on each request, the session data would be lost and simple tasks, such as building a shopping cart would result impossible.

So, it’s required to tell to Apache what is the session cookie name: JSESSIONID and which is the identifier of the routes to each single tomcat Server: tomcatA, tomcatB, tomcatC.

In this way, Apache will append to the end of the cookie value the information about the route to the specific Tomcat.

Java EE Tomcat Load Balancing

Java EE Tomcat Load Balancing

Finally, the last thing to set-up Apache, is obviously to add to it the modules required by the previous configuration:

  • proxy
  • proxy_balancer
  • proxy_ajp

About Tomcat configuration, there are just few changes to apply to the default configuration, in the Engine section it’s required to add the jvmRoute attribute.

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcatA">

This is related to the route parameter defined in the Apache load balancer. So, tomcatA should be set in the configuration of the first Tomcat server, tomcatB in the second and tomcatC in the third one.

Then, the port where AJP connector listens, has to be set accordingly to the apache configuration, so 8009, 8010, 8011 respectively on the first, second and third Tomcat.

The following is the the configuration of the AJP connector:

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

It’s not directly related to the setup of the load-balancer, but since they run on the same host, each Tomcat should have its own set of ports.

To prevent any conflict you should change the following settings on the second and third servers: Server port="8005" and Connector port="8080".

I hope this tutorial has given a complete overview about every step required to setup Apache and Tomcat to create a simple load-balanced cluster.

26 thoughts on “Java EE Load Balancing with Tomcat and Apache

  1. If you are going to be doing load balancing I would have thought that you would be running your Tomcats on different hosts (or blades), therefore there is no requirement for using different ports. This would mean that you could use the same Tomcat configuration on each node (or blade).

    • I agree with you.
      If each Tomcat is deployed on a different server/ip (as would probably happen in production environment), it’s possible to use the same port for each connector.

      Anyway it can be better to have multiple Tomcat on the same box, than having just one, because if for some reason of of them crashes, there are others that can handle requests until an administrator will restart the failed one and look for the cause.

    • I have the same problem with bfeileattld 2 locking up. I was like WTF so i tried bfeileattld 1942 and that locks up as well but takes longer. Battlefield 2 locks up allmost right away during game play. It plays the intro’s fine. I have a P4 3ghz, 1gb of ram Geforce 6200 Oc. Do you think i shold get more ram to fix the problem?

    • If Glassfish had a AJP connector and the jvmRoute configuration you can apply this tutorial to Glassfish too.

  2. Is it possible to use HTTP protocol instead of AJP protocol?

    How should I choose between Apache HTTPD proxy_balancer and Apache Traffic Server?

      • Jim,I saw your posts at one of the forums. The Magnavox tuernd out to be a puzzle.The 6F6 is a like an older version of the 6V6. Push comes to shove you could use a 6v6. The 6F6 was probably a metal can. The GT just means Glass Tube, so they are the same tube.The strange tube is probably a 6sl7, 6sj7, 6sn7 or something like that. These are old style preamp tubes with a sweet sound, but without much gain. Probably a 6SJ7 tube. Look at the oldest champ circuits.Buzz is 99.9% electrolytic cap. This amp is old enough that the caps all have to be dried out so change all the big caps. It can sometimes (rarely) be one of the fat cement style resistors or very fat resistor with high watt and low value. These resistors sometimes crack or change their value over the years. It is cheap and never hurts to change these resistors to get the amp back into spec.

  3. Fantastic items from you, man. I’ve bear in mind your stuff prior
    to and you are simply extremely excellent. I really like what you’ve received here, really like what you’re
    saying and the way in which through which you are saying it.
    You’re making it entertaining and you continue to take care of to stay it
    sensible. I cant wait to learn much more from you.
    That is actually a wonderful web site.

  4. It’s hard to find your page in google. I found it on 13
    spot, you should build quality backlinks , it will help you to rank to google top
    10. I know how to help you, just search in google – k2 seo tricks

    • I have a old Magnavox tube amp that I am trying to make into a harp amp it has a 12AX7,2 6v6,s and a 5Y3 tube I am wlnliig to do what ever it takes to make this the best it can be for a harp amp anything I can do to improve it the reason I bought this amp is that I have read in different articals that these are very good tube displacement to use on harp amps I have not tried it out yet to see what it sounds like the way it is,because I am new to all this and need to install an input jack,I am not sure where it would wire in? I have a book about old tube amps it has a schematic of a Fender Princeton 5F2 it is spot on down to every resistor cap everything looks the same wired the same,anything I can do to improve this amp?

  5. I read a lot of interesting articles here. Probably you spend
    a lot of time writing, i know how to save you a lot
    of time, there is an online tool that creates high quality, google friendly articles in seconds, just type in google
    - laranitas free content source

  6. I found a schematic for this estceromule as it turns out they were made by Valco the ones with the estceromule name did not have model numbers on them but i think it is same as a Valco model ,10A anyway the tubes in the schematic are the one by the output transformer by it,s self is a 6J5GT,labeled preamp 2 on schematic mine is a 6F6GT the next one below that tube in the schematic is a 6SH7GT, labeled preamp 1. mine has a tube without markings,but I think I see a 7 on it,so it is probably a 6SH7GT, the next tube is a 6V6GT,in the schematic,labeled power amp,mine has a metal 6F6 tube. the last one in the schematic is a 6X5GT,labeled rectifier,mine has a 5Y3 in that place. I guess my question is are the tubes in my amp interchangeable with the ones in the schematic. do the tubes in the schematic do the same function as the tubes that are in my amp?

  7. On the MASCO gallery: The MA-60 had four 6L6 s four 6J7 s one 6sj7 two 6sc7 s and two 5v4 retrifiecs. I think you might have slipped a digit on this amp. I have a 1951 MA-60, and the tube layout is four 6J7 s at the input, 6SC7, 6SJ7, 6SL7GT, 6SN7GT, two 807 power tubes, and two different retrifiecs- 5Y3GT and 5R4GY. It has four inputs, a phono input, and a booster jack in the back.And yes, I have a TON of pics, in case anyone is interested in seeing this amp. Also, I’m wondering if this amp is an anomaly or something, because I’m finding very little info on it. I can find plenty on the MA-17, 25, 35, and 50, but next to nothing on the MA-60.

  8. Simply desire to say your article is as surprising. The clarity in your post is simply spectacular and i could assume you’re an expert on this subject. Fine with your permission allow me to grab your feed to keep updated with forthcoming post. Thanks a million and please keep up the rewarding work.

  9. I replying to your watning more info about Multi vox amps. I purchased a lot of vacuum tubes and radios from an old guy that’s giving up the hobby. Within the lot I purchased is a Multi Vox P.A. Single ended push pull with 2 7591 s and 3 12ax7 s (tubes are labeled Multi Vox with what I think is Sylvaina’s yellow stamp). It has a Mic Z adaptor, hum balance and bias adjustment.Outputs are 2,4,8,16 ohm’s and 70 volts, 35 Watts R.M.S. Inputs are Tuner, Phono, P.A. and Mic. Model EA35-6 Made in Montreal, Canada. I have put the bottom cover back on yet there is at least 10 Mullard mustard .022 tuning caps dated 1965 within.Jonathan

  10. This really is the right weblog for any person who wants to find out about this topic. You realize so a lot its just about tough to argue with you (not that I really would want…HaHa). You surely put a brand new spin on a subject thats been written about for years. Good stuff, just wonderful!

  11. Great beat ! I would like to apprentice while you amend your site, how could i subscribe
    for a weblog site? The account helped me a acceptable deal.
    I have been tiny bit acquainted of this your broadcast offered vivid clear idea

  12. The hybrid system must be OFF before any service
    is performed. An alternator charges the battery when your engine
    is running; the battery stores the charge when it is not.

    With the Internet and DIY (do it yourself) instructions within everyone’s reach, consumers are doing more and more activities
    on their own.

  13. In my case it was a non-pilot who managed upwards and brought about a successful outcome.
    These can involve lasers, microwaves or injections
    of chemicals that cause scarring of the palate.

    In any list of the most popular plastic surgery procedures,
    breast augmentation is almost always at the top of the list, along with

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>