Adservio team
January 26, 2022
Digital Quality
Tomcat Performance Best Practices

Digital Quality

10 min

Tomcat Performance Best Practices

Apache Tomcat is an open-source Catalina servlet container and web server. Out of the box, Tomcat provides a Java runtime for running basic servlet container and application server functionality that is suitable for many webapps.

According to Kissmetrics, a one-second delay in page response can result in a 7% reduction in conversions. Depending on your revenue, that could cost millions of dollars per year. Given these numbers, it is easy to see why tuning and maintaining your web server for optimal performance is key.

Most enterprise production websites, however, will need performance capabilities beyond the basic configurations. Tuning Tomcat is just one of many performance engineering strategies. You’ll get optimal performance for your Java applications by following these Tomcat performance best practices.

Tomcat performance best practices for Connectors

Connectors are needed to enable Tomcat servers to receive requests from clients. There is one connector per port on the server, and each listens for a specific type of request.

If different workloads are coming into the Tomcat server that you want to be processed separately, consider having multiple connectors. That will allow one type to be processed on one port and another type on a different port.

This Tomcat performance best practice minimizes the chances that different request types will interfere with each other.

When Tomcat receives requests, it assigns them to threads. The maxThreads attribute of a connector defines the maximum number of simultaneous threads that can be executed for that connector.

Tomcat Performance maxThreads

If maxThreads is set too low, you'll see bottlenecks as requests will have to wait until there are free threads to process their request. This can increase response times which affects performance and negatively impacts the user's experience.

For best performance and to increase throughput, you should set the value of maxThreads high enough so that it always has an available thread when an incoming request arrives.

You can specify a number for the acceptCount setting if you want the operating system to accept a certain number of requests should Tomcat run out of available connections. The server.xml file is where you configure these properties for connectors.

Tomcat Performance Architecture

Connectors can accept both SSL and non-SSL requests. Any request that is sent to a non-SSL connector should be sent to the redirectPort specified in this configuration field.

Configuring threads requires careful evaluation of your system to ensure you select the optimum number of threads. We can help evaluate your system and make recommendations on optimizing this setting.

We can even help implement the changes you need. Get in touch to learn more about how we can help you make the most of your server resources to achieve outstanding performance objectives.

Tomcat performance best bractices for Executors

When using multiple connectors, you can configure an executor. An executor is a thread pool that can be shared by all of the connectors of each request type. This allows better sharing of threads across them.

Not only does this Tomcat performance best practice allow for better use of resources on individual connections, but it also gives administrators more control over how many threads are in the pool at any given time.

Tomcat performance best practices for database connection Pooling

Connection pools are a common technique for optimizing database accesses. by having a pool of open connections so requests can get these connections from the pool and return them to the pool once they are done with their tasks.

This reduces connection overhead on the server, which allows application response to be faster. Ensure that maxActive is large enough to handle the workload. Also, consider tuning the maxIdle, and maxWait attributes to avoid connectionTimeout issues.

Tomcat performance best practices for HTTP, HTTPS, and HTTPD

Generally speaking, using HTTP/1.1 in Tomcat will result in a much better performance. This is because verbose traffic from HTTPS increases the overall server time for users with higher pings.

If you require the security of HTTPS despite its slow speed compared to HTTP requests, you may have to consider adding additional servers closer to your users for increased speed.

HTTPD is a daemon process for serving content over HTTP. There is a performance overhead when running HTTPD and thus should be avoided.

There are many factors that go into deciding which of these options to use. Which one you select depends on your use case.

Our team of professionals can guide you through your options and help choose the best for your environment.

Tomcat performance best practices for the JVM

Tomcat runs on x86_64 architecture as well as most devices running ARM processors. You have the option to select the appropriate architecture or the JVM.

Be sure to select an appropriate JVM for our application. JVMs are available in 32-bit and 64-bit modes.

The difference between the two is the size of the heap each can hold. A 32-bit JVM is limited to 2GB of memory on the server hardware. Apps that require more than 2GB of memory will need to use a 64-bit JVM.

Tomcat performance best practices for garbage Collection

JVM garbage collection can use up valuable CPU power that would be better used to serve web requests.

To reduce the frequency of JVM invoking the garbage collector, start your JVM using an initial heap size that is equal to its allocated memory.

This Tomcat performance best practices will keep the server from having to reallocate and resize its memory. If your web application doesn’t require frequent garbage collection, enable incremental collections with -Xincgc.

Tomcat performance best practices for Memory

Use a profiler to monitor or and detect memory issues. Memory availability in the JVM can also adversely impact performance.

This is because you have to make sure there is sufficient memory available in the heap and non-heap memory pools. If any of these pools run out of available memory then your application may suddenly stop functioning.

To prevent this, use the Xmx and Xms flags to define the heap size or stack size for your needs. Alternatively, use XX flags to set PermSize or MetaspaceSize depending on which version of Tomcat you're using.

Another common problem with memory is memory leaks. This usually happens when an object is no longer being referenced but is being held in memory and is unable to be garbage collected.

The Tomcat performance best practice is to release references to objects when they are no longer needed by the application.

Tomcat performance best practices for TCP port optimization

TCP port exhaustion happens when all available TCP ports are in use, thus preventing new connection requests.

The Tomcat performance for TCP port optimization is to configure the TCP settings to ensure the system can handle a large number of requests at any given time. Configuring TCP ports properly can be challenging without the right technical expertise.

Adservio performance engineering experts can guide you on best practices for TCP ports to ensure your system can handle as many requests as needed.

Tomcat performance best practices for Number of Open Files (Linux Operating System)

Every incoming or outgoing connection needs to open a socket and each of these is an individual file on the system. It's important that you configure the maximum number of sockets for your server — generally, this default setting can be pretty low.

Tomcat performance best practices for the Web.xml file

The web.xml file specifies the application configuration for each web application on the Tomcat Server instance. The default values in this file are for development environments. They need to be reconfigured for production servers.

Specific items to consider are:

  • The JSP compiler setting is set to development mode by default. Change this setting to true to indicate that it is a production app.
  • Compiling JSPs causes overhead on the server. Precompile these pages so they don’t need to be compiled for each request.
  • Set trimSpaces to “true” to remove unnecessary bytes from the response.

If you want help with performance testing your app and applying Tomcat performance best practices, having an expert by your side is the best way to get optimal results.

We can help establish benchmarks and define a strategy to help meet your performance metrics goals. If you are looking to use the Tomcat API to customize your application, we can help with that too.  

Adservio continues to help countless companies achieve scalability, resiliency, and superior performance for web applications. If you're interested in learning more about how our team of professionals can help your organization thrive, reach out to Adservio to learn more.

Industry insights you won’t delete. Delivered to your inbox weekly.

Other posts

Do you have any questions for us?