The default Tomcat configuration looks powerful and hassle-free to the 1st time users. However, it is not customized for the real work where high server load can be expected. Therefore, it is important to optimize Tomcat performance by setting customization and system tuning.
The post will describe how to optimize Tomcat performance.
Tuning JVM
The proper Tomcat JVM configuration and tuning is essential for Tomcat server optimization.
First, set tomcat mode to be in a server mode, by default tomcat is in client mode (for 32 bit OS), to set the server mode just add -server to your CATALINA_OPTS to be like
CATALINA_OPTS=”-server -Xms3G -Xmx3G $CATALINA_OPTS -XX:+UseConcMarkSweepGC -XX:NewSize=1G -XX:+UseParNewGC”
Second, the JVM processes like garbage collection and memory reallocation, can use up valuable CPU. You can reduce the frequency with which these processes occur by setting Xms and Xmx and XX:MaxPermSize to a suitable values depends on the server. You can edit it in catalina.(sh|bat) files.
- To reduce the frequency with which JVM invokes garbage collection, use the -Xmx switch to start the JVM with a higher maximum heap memory.
- Use the -Xms switch to ensure that the JVM’s initial heap memory size is equal to the maximum allocated memory. It will keep the JVM from having to reallocate and resize its heap memory so Tomcat can serve more requests.
Configuring connectors
In default Tomcat configuration file server.xml, you can see the default value
1 2 3 |
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> |
Normally, there are several connectors available for Tomcat:
- BIO – blocking I/O -> protocol=”HTTP/1.1” (good for low concurrency, or high concurrency without keep-alive from clients)
- NIO – Non-blocking I/O -> protocol=”org.apache.coyote.http11.Http11NioProtocol” (good for high currency with no keep-alive parameter from clients)
- Apache Portable Runtime (APR) -> protocol=”org.apache.coyote.http11.Http11AprProtocol”
For high concurrency scenario, to maintain keep-alive connection, it is better to use NIO connector to replace BIO connector to get higher TPS (transaction per second). If possible, you can try APR. Tomcat can use the Apache Portable Runtime (APR) to provide superior scalability, performance, and better integration with native server technologies. More information of APR can be found https://apr.apache.org/. It requires 3rd library to be installed.
Here are connectors in preference order
1 2 3 4 5 6 7 |
Stability BIO NIO or APR SSL APR NIO BIO Low concurrency BIO APR NIO High concurrency No Keep-Alive BIO APR NIO High concurrency Keep-Alive APR NIO BIO |
Beside the protocols, other values to be changed are: pickup a reasonable max thread value, disable client DNS lookup etc. To get the reasonable values, it is important to simulate the real world scenario, experiment with different values to determine the best settings.
Here is a sample:
1 2 3 4 5 6 7 8 9 10 11 12 |
<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="20000" redirectPort="8443" maxThreads="500" minSpareThreads="20" acceptCount="100" disableUploadTimeout="true" enableLookups="false" URIEncoding="UTF-8" /> |
Clustering
A cluster is a group of servers connected together that can perform similar functionalities. There are many benefits of clustering:
- Increased scalability to accommodate future enhancements
- High availability
- High performance to support more concurrent users
Depends on the deployment topology, Tomcat clustering can be classified to two categories: vertical clustering and horizontal clustering.
Vertical clustering consists of a single hardware with multiple instances running and using the shared resources from the system. For example: Multiple Tomcat servers are deployed in one powerful physical server. They can be used in the production environment or in development and QA environment.
Horizontal clustering consists of a group of individual machines connected via high speed Ethernet. It is widely used in production environment. There are multiple ways to implement horizontal clustering so the request can be routing to the right Tomcat server.
- DNS request distribution
- NAT (TCP Network Address Translation) request distribution
- mod_proxy_balancer load balancing
Cache and compression
Due to network delays and remote client latency, it is a good choice to add a web server in front of Tomcat to serve static content. Remote client latency can be mitigated by compressing content with Apache mod_zip or Tomcat’s compression filter. You also can consider using nginx to achieve cache and compression content, and leave less work to Tomcat
Test those parameters and setting to get the best numbers
To generate a high-quality benchmark, it is important to simulate the real world scenario in which your site operates as closely as possible.
1 2 3 4 |
java -XX:+PrintFlagsFinal tomcat | grep HeapSize java -XX:+PrintFlagsFinal -Xms512m -Xmx1024m -Xss512k -XX:PermSize=64m -XX:MaxPermSize=128m |
“NIO – Non-blocking I/O -> protocol=”org.apache.coyote.http11.Http11NioProtocol” (good for high currency with no keep-alive parameter from clients)”
i assume there are few typos here. currency>concurrency. with no>with