Tomcat Performance Tuning Training Course now on offer!
Tomcat is one of the most widely used servlet containers on the planet but many are unaware of how to tune Tomcat for maximum performance. The best place to get more performance out of your application though, is to look at the application code itself as a request spends around 80% of its time there rather than in Tomcat's code.
Tomcat Performance and Tomcat Connectors
But if you are looking to squeeze some extra performance out of Tomcat one of the places to look is at the connectors and their settings. Connectors are the mean by which external applications gain access to the output of Tomcat's servlet (Catalina) and JSP(Jasper) engines. By default Tomcat comes with two connectors already setup, the HTTP connector on port 8080 and the AJP connector on port 8009.
AJP is a binary protocol that should be faster for front-end web servers to access than verbose text based protocols such as HTTP. It is really intended for binary access to Tomcat by web servers such as Apache HTTP. The AJP connector is different to the HTTP connector in that there are two places to tune the settings. Both the HTTP and AJP connector can be tune in server.xml, but the AJP connector can also be tune in apache.conf for example. These are the "client" setting for the AJP connector such as mod_jk and mod_proxy.
Just to confuse issues, Tomcat has three implementations of connectors that implement the HTTP and AJP protocol. In fact there seems to be multiple uses of the term "connectors" in Tomcat documentation. A connector is both a port and protocol, as well as a code library that implements the connectors. So besides changing the settings for a connector you also need to choose the most appropriate implementation.
Tomcat Connector Implementations
In general the APR (Apache Portable Runtime) library is the most efficient connector, as it is a non-blocking C implementation. The Java based, blocking IO implementation, know as Cototye, is considered more stable, but due to its blocking nature is usually not great for high traffic sites. The NIO (New IO) implementation is generally not used, unless you don't have access to the APR library. Tomcat comes with the blocking IO implementation by default.
Once you have chosen the best library to use you can then start tweaking the timeout and keep alive request settings in sever.xml. The general rule of thumb is that for high concurrency sites without SSL use low time outs and keep-alive requests setting and for low concurrency sites or sites with SSL set the time outs and max requests higher. You should always change the default time out of 20 seconds on the HTTP connector to a lower value.
When using AJP you should set the keep-alive and timeouts to off as usually the web server and Tomcat are on the same machine, making the time to set up and create connections between the two almost irrelevant. Also use Apache to do the SSL in this case and not Tomcat!
Performance Tuning with Timeouts and Thread Settings
The maximum number of threads should also be tweaked to suit the application load profile. If the application has high CPU usage set the max threads lower as Tomcat will accept connections but the CPU will be unable to service them. If CPU utilisation is low this can be set higher. For the blocking io connector, maxThreads is the maximum number of concurrent connections that can be accepted, for the APR connector this is limited by available memory.
It is not possible to give any hard and fast rules when it comes to performance tuning though as it is highly dependent on your load profile, even in the case of a common load profile you will need to see what works in practice.