Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
»  Contact HP
HP.com home

Tomcat tuning notes

» 

Web Servers Home

» HP software index home
» HP-UX 11i home
» HP-UX 11i internet software
» HP-UX 11i internet express
» Development software home
» Development tools
» Technical support
» Buy online from HP
Content starts here

** Exerpted from: http://jakarta.apache.org/tomcat/tomcat-3.2-doc/uguide/tomcat_ug.html. Please see this for the complete Tomcat User Guide.

Real World configuration tips

By default the Tomcat distribution comes with a naive configuration whose main goal is to promote first time user experience and an "out of the box" operation... This configuration however is not the best way to deploy Tomcat on real sites. For example, real sites may require some performance tuning and site-specific settings (additional path elements for example). This section will try to get you started by directing you to the first steps that should be taken before publishing a Tomcat based site.

Modify and customize the batch files

As stated in the previous sections, the startup scripts are here for your convenient. Yet, sometimes the scripts that are needed for deployment should be modified:

  • To set resource limits such as maximum number of descriptors.
  • To add new CLASSPATH entries (for example, JDBC drivers).
  • To add new PATH/LD_LIBRARY_PATH entries (for example, JDBC drivers DLLs).
  • To modify the JVM command line settings.
  • Make sure that you are using a specific JVM (out of the two or three JVMs installed on your machine).
  • To switch user from root to some other user using the "su" UNIX® command.
  • Your pet reason.

Some of these changes can be done without explicit changes to the basic scripts; for example, the tomcat script can use an environment variable named TOMCAT_OPTS to set extra command line parameters to the JVM (such as memory setting etc.). On UNIX® you can also create a file named ".tomcatrc" in your home directory and Tomcat will take environment information such as PATH, JAVA_HOME, TOMCAT_HOME and CLASSPATH from this file. On NT however (and also on UNIX® when the modifications are for something such as the JVM command line) you are forced to rewrite some of the startup script...

Do not hesitate, just do it.

Modify the default JVM settings

The default JVM settings in the tomcat script are very naïve; everything is left for defaults. There are a few things that you should consider to improve your Tomcat performance:

  1. Modify your JVM memory configuration. Normally the JVM allocates an initial size for the Java heap and that's it, if you need more then this amount of memory you will not get it.
    Nevertheless, in loaded sites, giving more memory to the JVM improves Tomcat's performance. You should use command line parameters such as -Xms/-Xmx/-ms/-mx to set the minimum/maximum size of the Java heap (and check to see if the performance was improved).
  2. Modify your JVM threading configuration. The SUN JDK1.2.2 for Linux comes with support for both, green and native threads. In general native threads are known to provide improved performance for I/O bound applications, green threads on the other hand put less stress on the machine. You should experiment with these two threading models and see which model is better for your site (in general, native threads are better).
  3. Select the best JVM for the task. There are several JVM vendors, for example on Linux there are today (21/03/2000) two product level JVMs: the SUN JDK1.2.2 and the IBM JDK1.1.8. If your application does not require a specific JDK functionality, you should benchmark the two JVMs and select the better one. In my (Gal Shachor) internal tests I found the IBM JVM significantly faster than the one created by SUN, you should check that for yourself and make a calculated decision.
Modify your connectors

The Connectors, as configured in Tomcat's default server.xml contains two Connectors configured as in the next server.xml fragment:

The two default connectors in server.xml

<!-- (1) HTTP Connector for stand-alone operation -->
<Connector className="org.apache.tomcat.service.PoolTcpConnector">
    <Parameter name="handler"
        value="org.apache.tomcat.service.http.HttpConnectionHandler"/>
    <Parameter name="port"
        value="8080"/>
</Connector>

<!-- (2) AJPV12 Connector for out-of-process operation -->
<Connector className="org.apache.tomcat.service.PoolTcpConnector">
    <Parameter name="handler"
        value="org.apache.tomcat.service.connector.Ajp12ConnectionHandler"/>
    <Parameter name="port"
        value="8007"/>
</Connector>
         
  1. Is a Connector that listens on port 8080 for incoming HTTP requests. This connector is needed for stand-alone operation.
  2. Is a Connector that listens on port 8007 for incoming AJPV12 requests. This connector is needed for web-server integration (out-of-process servlet integration).

The AJPV12 Connector is required for Tomcat shutdown. However, the HTTP Connector may be removed if stand-alone operation is not needed.

Use a thread pool in your connectors

Tomcat is a multi-threaded servlet container this means that each request needs to be executed by some thread. Prior to Tomcat 3.2, the default was to create a new thread to serve each request that arrives. This behavior is problematic for loaded sites because:

  • Starting and stopping a thread for every request puts a needless burden on the operating system and the JVM.
  • It is hard to limit the resource consumption. If 300 requests arrive concurrently Tomcat will open 300 threads to serve them and allocate all the resources needed to serve all the 300 requests at the same time. This causes Tomcat to allocate much more resources (CPU, Memory, Descriptors...) than it should and it can lead to low performance and even crashes if resources are exhausted.

The solution for these problems is to use a thread pool, which is the default for Tomcat 3.2. Servlet containers that are using a thread pool relieve themselves from directly managing their threads. Instead of allocating new threads; whenever they need a thread they ask for it from the pool, and when they are done, the thread is returned to the pool. The thread pool can now be used to implement sophisticated thread management techniques, such as:

  1. Keeping threads "open" and reusing them over and over again. This saves the trouble associated with creating and destroying threads continuously.
    • Usually the administrator can instruct the pool not to keep too many idle threads, freeing them if needed.
  2. Setting an upper bound on the number of threads used concurrently. This prevents the resources allocation problem associated with unlimited thread allocation.
    • If the container maxed out to the threads upper limit, and a new request arrives, the new request will have to wait for some other (previous) request to finish and free the thread used to service it.

You can refine the techniques described above in various ways, but these are only refinements. The main contribution of thread pools is thread-reuse and having a concurrency upper bound that limits resource usage.

Using a thread pool in Tomcat is a simple move; all you need to do is to use a PoolTcpConnector in your <Connector> configuration. For example the following server.xml fragment defines ajpv12, pooled Connector:

Pooled ajpv12 connector

<!-- A pooled AJPV12 Connector for out-of-process operation -->
<Connector className="org.apache.tomcat.service.PoolTcpConnector">
    <Parameter
        name="handler"
        value="org.apache.tomcat.service.connector.Ajp12ConnectionHandler"/>
    <Parameter
        name="port"
        value="8007"/>
</Connector>
        

This fragment is very simple and the (default) pool behaviour instructed by it is:

  • Upper bound for concurrency of 50 threads.
  • When the pool has more then 25 threads standing idle it will start to kill them.
  • The pool will start 10 threads on creation, and it will try to keep 10 vacant threads (as long as the upper bound is kept).

The default configuration is suitable for medium load sites with an average of 10-40 concurrent requests. If your site differs you should modify this configuration (for example reduce the upper limit). Configuring the pool can be done through the <Connector> element in server.xml as demonstrated in the next fragment:

Configuring the thread pool

<!-- A pooled AJPV12 Connector for out-of-process operation -->
<Connector className="org.apache.tomcat.service.PoolTcpConnector">
    <Parameter
        name="handler"
        value="org.apache.tomcat.service.connector.Ajp12ConnectionHandler"/>
    <Parameter
        name="port"
        value="8007"/>
    <Parameter
        name="max_threads"
        value="30"/>
    <Parameter
        name="max_spare_threads"
        value="20"/>
    <Parameter
        name="min_spare_threads"
        value="5" />
</Connector>
        

As can be seen the pool has 3 configuration parameters:

  • max_threads - defines the upper bound to the for the concurrency, the pool will not create more then this number of threads.
  • max_spare_threads - defines the maximum number of threads that the pool will keep idle. If the number of idle threads passes the value of max_spare_threads the pool will kill these threads.
  • min_spare_threads - the pool will try to make sure that at any time there is at least this number of idle threads waiting for new requests to arrive. min_spare_threads must be bigger then 0.

You should use the above parameters to adjust the pool behavior to your needs.

Disable Servlet auto-reloading

Servlet auto-reloading is really useful for development time. However it is very expensive (in performance degradation terms) and may put your application in strange conflicts when classes that were loaded by a certain classloader cannot co-operate with classes loaded by the current classloader.

So, unless you have a real need for class reloading during your deployment you should turn off the reloadable flag in your contexts.

Authors

This document was created by:

Gal Shachor

With help from (alphabetical ordered):

Jonathan Bnayahu
Fiona Czuczman
Costin Manolache

Copyright ©1999 The Apache Software Foundation
Legal Stuff They Make Us Say
Contact Information
** About PDF files: The PDF files on this Web site can be read online or printed using Adobe® Acrobat® Reader. If you do not have this software installed on your system, you may download it from the Adobe Web site.
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 2009 Hewlett-Packard Development Company, L.P.