Networking

Tomcat performance tuning tips

November 2, 2009, 07:06 AM — 

by jasonb, MuleSoft -- I often get questions about how to tune Tomcat for better performance. It is usually best to answer this only after first spending some time understanding the installation of Tomcat, the web site's traffic level, and the web applications that it runs. But, there are some general performance tips that apply regardless of these important details. In general, Tomcat performs better when you:

  • Start the JVM with a higher heap memory maximum using the -Xmx switch. The higher you make this, the more memory is available for Tomcat to use. This means that the JVM won't need to invoke the garbage collector as often, which means that the server can focus a higher percent of its CPU time to serving web requests. That, in turn, means that requests can be completed faster. If your webapp runs on dedicated server hardware then just give the JVM as much of the machine's physical memory as you can reasonably give it. If it is running on shared or virtual hardware, hook up JConsole or another console (such as MuleSoft's Tcat Server console) to see how much memory your webapp tends to use under high load circumstances so that you can set a more reasonable maximum heap size setting.
  • Start the JVM with its initial heap memory size (the -Xms switch) set the same value as its maximum memory size. This means the JVM never needs to resize and reallocate the heap memory while Tomcat is trying to serve requests.
  • Tune the Connector (web server) thread pool settings to more closely match the web request load you have. This is difficult to do, but is very important to get right for best performance. The important Connector attribute for this is maxThreads. Setting this too low means that you may not have enough threads to handle all of the requests, in which case requests have to sit idle for some time without being handled until another request thread is freed up. Too low of a value also often means that Tomcat will be unable to take advantage of your server machine's hardware. Setting maxThreads too high means that Tomcat startup time will take longer than necessary, and that during peak load times your server could slow down to a crawl trying to run that many request threads concurrently. You want to set it just right, based on your peak traffic pattern, and setting it just right is a matter of testing different values at that volume of traffic.
  • Tune some additional Connector attribute settings:
    • compression: Try turning this "on" and "off" and see which way performs best for your webapp. You may be surprised at the difference. By default it is "off".
    • compressableMimeTypes: If you enable compression, you should also set this attribute so that Tomcat knows what you want it to compress. Try setting it to something like "text/html,text/xml,text/javascript,text/css,text/plain".

Other important factors in performance tuning include:

  • Database performance: Many webapps use a relational database nowadays. The database connection pool settings are very important in that case. Mainly, the maxActive, maxIdle, and maxWait attributes of the Resource element where you define your database connection pool. Also, what about performance testing your database? Tomcat's performance may be significantly slowed waiting on database queries to complete.
  • HTTP caching headers: Enabling the user's web browser to cache the static content of your webapps can greatly increase the user's perceived performance. This is time consuming because tuning this optimally is almost always webapp specific and requires some studying of the webapp. But, making the caching work is all about setting the right set of response headers and their values when Tomcat serves the webapp's static files.
  • HTTPS is slower than HTTP, but this is a tradeoff. HTTPS is secure and HTTP is not. HTTPS is more chatty over the network during every request. Usually, when the user's web browser is geographically distant from Tomcat, HTTPS makes requests take quite a bit longer. It may only be fixed by bringing Tomcat geographically closer to the users.

Also, the way people usually tend to lose about half of their Tomcat performance is to proxy all requests to Tomcat from Apache HTTPD. That configuration is not what you want if you need the best Tomcat performance. See my benchmark (with nice performance graphs) in the sample chapter Tomcat Performance Tuning of our book Tomcat: The Definitive Guide, Second Edition for the performance details of these configurations.

Today's tip was provided by sdozen12 on behalf of MuleSoft

Do you have a question on installing, configuring, or deploying Apache Tomcat? Submit it here.

_______________

Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.

 

Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
peer-to-peer

Esther Schindler
If the comments are ugly, the code is ugly

claird
SVG a graphics format for 21st century

pasmith
Take Chrome OS for a test spin

Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?

sjvn
64-bits of protection?

jfruh
Android fragments vs. the iPhone monolith

mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive

 

Where Google Chrome security fails: the password
I heard mention that the Chrome OS will have some sort of encryption available a la bitlocker. If it's possible to encrypt personal data using another password or key, then it may have potential for very secure data.... And Ubuntu has an 'encrypt home directory' option, perhaps google should follow suit.
- Dann

Join the conversation here

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace