Tomcat performance tuning tips

By ITworld tips  Add a new comment

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.

 

ITworld LIVE

NetworkingWhite Papers & Webcasts

White Paper

Building Cloud-Optimized Data Center Networks white paper

Enterprises are turning to the Cloud to improve business agility, reduce expenses and accelerate business innovation. Cloud computing redefines the way IT assets are deployed and consumed and dramatically affects the way data center networks are architected and managed. Conventional hierarchical data center networks built to support traditional IT architectures can't meet the security, agility and price/performance requirements of virtualized cloud computing environments. This white paper reviews the impact of cloud computing on data center networks and describes HP's approach to building simpler, more secure and automated networks that fully meet the stringent performance, security, reliability and agility demands of the new data center in the Cloud.Intel and the Intel logo are trademarks of Intel Corporation in the U.S. and/or other countries.

White Paper

Seven Priorities for Integrated Network Management - How HP Intelligent Management Center Delivers an Enterprise-class Solution

This white paper describes the major requirements for network management solutions to help the organizations become more profitable, efficient and reliable.Intel and the Intel logo are trademarks of Intel Corporation in the U.S. and/or other countries.

White Paper

Top 10 Best Practices of Backup, Replication & Recovery for VMware & Hyper-V

Whether you are new to virtualization or if you have been administering a virtual infrastructure for a while, it's now time to review your virtual infrastructure backup design and backup product features. Determine if you are both optimally protecting your virtual infrastructure as well as taking advantage of the latest virtualization backup features. Read this white paper to learn the 10 best practices for virtual infrastructure backup.

White Paper

Expert Guide on Backing up Windows Server in Hyper-V

Virtualization improves your infrastructure in many ways - it also introduces unfamiliar considerations. Take backup, replication and disaster recovery for example. The right backup and replication solution for Hyper-V can ensure that you'll be able to scale your infrastructure and protect yourself from data and application loss. But there are wrong choices to be made. Download this white paper from Microsoft MVP John Savill, avoid bad choices, and learn how to effectively protect your virtualized data and systems successfully.

White Paper

7 Expert Tips on VMware Backup

Want to create a bulletproof VMware backup infrastructure? Download this guide and learn 7 time-tested VMware infrastructure backup tips from virtualization backup pros:* Understand backup tool limitations* Save time, prevent data-loss* Find the solution that's right for youDownload the guide and save time planning your VMware backup.

See more White Papers | Webcasts

Ask a question

Ask a Question