• notice
  • Congratulations on the launch of the Sought Tech site

3 ways to optimize Tomcat performance in high concurrency environment

As the most commonly used Java web server, Tomcat's performance will drop sharply as the amount of concurrency increases. Is there any way to optimize the performance of Tomcat in a high-concurrency environment?

written in front

As the most commonly used Java web server, Tomcat's performance will drop sharply as the amount of concurrency increases. Is there any way to optimize the performance of Tomcat in a high-concurrency environment?

Tomcat running mode

There are three operating modes of Tomcat.

1. bio mode

The default mode, the performance is very low, without any optimization and support.

2. nio mode

Use java's asynchronous io care technology, noblocking IO technology. To run in this mode, directly modify the Connector node in server.xml and modify the protocol to the following configuration.

protocol = "org.apache.coyote.http11.Http11NioProtocol"

After restarting Tomcat, it will take effect.

3.apr mode

It is the most difficult to install, but it solves the asynchronous IO problem from the operating system level and greatly improves performance. In this mode, apr and native must be installed, and apr is supported by direct startup. For example, if nio modifies the mode, modify the protocol to org.apache.coyote.http11.Http11AprProtocol, as shown below.

protocol = "org.apache.coyote.http11.Http11AprProtocol"

Tomcat concurrency optimization

Install APR

[root@binghe ~]# yum -y install apr apr-devel openssl-devel
[root@binghe ~]# tar zxvf tomcat-native.tar.gz
[root@binghe ~]# cd tomcat-native-1.1.24-src/jni/native
[root@binghe native]# ./configure --with-apr=/usr/bin/apr-1-config --with-ssl=/usr/include/openssl/
[root@binghe native]# make && make install

After the installation is complete, the following prompt message will appear

Libraries have been installed in : 
/usr/ local /apr/ lib

After the installation is successful, you need to set environment variables for tomcat by adding 1 line to the catalina.sh file:

Below this code add:

# OS specific support.  $var _must_ be set to either true or false.

Modify the configuration of server.xml as shown below.

protocol = ” org.apache.coyote.http11.Http11AprProtocol ”

After starting tomcat, check the log, as shown below.

more TOMCAT_HOME/logs/catalina.out
2020-04-17 22:34:56 org.apache.catalina.core.AprLifecycleListener init
INFO: Loaded APR based Apache Tomcat Native library 1.1.31 using APR version 1.3.9.
2020-04-17 22:34:56 org.apache.catalina.core.AprLifecycleListener init
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2020-04-17 22:34:56 org.apache.catalina.core.AprLifecycleListener initializeSSL
INFO: OpenSSL successfully initialized (OpenSSL 1.0.1e 11 Feb 2013)
2020-04-17 22:34:58 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler [“http-apr-8080”]
2020-04-17 22:34:58 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler [“ajp-apr-8009”]
2020-04-17 22:34:58 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1125 ms

Tomcat optimization

1. JVM tuning

Add the following statement in TOMCAT_HOME/bin/catalina.sh, the specific value depends on the situation.
Just add it after CATALINA_OPTS above, as shown below.

JAVA_OPTS =-Xms512m -Xmx1024m -XX: PermSize =512M -XX: MaxNewSize =1024m -XX: MaxPermSize =1024m

Detailed parameters

  • -Xms: JVM initializes the heap memory size.

  • -Xmx: The maximum memory of the JVM heap.

  • -Xss: thread stack size.

  • -XX:PermSize: The initial memory allocation size of the JVM non-heap area.

  • -XX:MaxPermSize: The maximum memory of the JVM non-heap area.

Advice and Precautions:

The -Xms and -Xmx options are set to the same heap memory allocation to avoid adjusting the size of the heap after each GC. The heap memory is recommended to account for 60%~80% of the memory; non-heap memory is non-recyclable memory, and the size depends on the project ; The thread stack size is recommended to be 256k.

The 32G memory configuration is as follows:

JAVA_OPTS =-Xms20480m -Xmx20480m -Xss1024K -XX: PermSize =512m -XX: MaxPermSize =2048m

2. Turn off DNS reverse lookup

Add the following parameters in <Connector port="8080".

enableLookups = " false "

3. Optimize tomcat parameters

Perform the following configuration in the server.xml file.

<Connector port="8080"
connectionTimeout="20000" //Connection timeout
maxThreads="500"//Set the maximum number of threads for processing client requests, which determines the number of threads that the server can respond to client requests at the same time, the default is 200
minSpareThreads="20"//Initialize the number of threads, the minimum number of idle threads, the default is 10
acceptCount="1000" //When all available threads for processing requests are used, the number of requests that can be placed in the processing queue, requests exceeding this number will not be processed, the default is 100
URIEncoding="UTF-8" />


Technical otaku

Sought technology together

Related Topic


Leave a Reply