Adjusting Tomcat Configurations
Purpose
Adjust Tomcat configurations to improve the server performance.
Procedure
- Scenario of Independent Tomcat
- Modify the parameters in the Tomcat configuration file conf/server.xml in the Tomcat installation directory.
- Open the file.
1vim conf/server.xml - Press i to enter the insert mode and add the following content to the configuration file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<Connector executor="tomcatThreadPool" port="10000" protocol="HTTP/1.1" acceptCount="10000" maxConnections="10000" connectionTimeout="20000" compression="off" compressionMinSize="2048" URIEncoding="utf-8" tcpNoDelay="true" enableLookups="false" useURIValidationHack="false" disableUploadTimeout="false" connectionUploadTimeout="150000" keepAliveTimeout="12000" maxKeepAliveRequests="1000" redirectPort="8443" />
See the result:

- Press Esc, type :wq!, and press Enter to save the file and exit.
- Open the file.
- Bind Tomcat to cores.
1 2
cd /home/apache-tomcat-9.0.69 taskset -c N ./bin/startup.sh
- Modify the parameters in the Tomcat configuration file conf/server.xml in the Tomcat installation directory.
- Scenario of Built-in Tomcat in Spring Boot
- Modify the configuration options in the application.properties file of the Spring Boot project. For details about the parameter names, default values, descriptions, and tuning suggestions, see Table 1.
For example, to set the value of server.tomcat.max-threads to 300, locate the server.tomcat.max-threads parameter in the configuration file and change its value to 300, that is, server.tomcat.max-threads=300.
Table 1 Configuration parameter names, default values, descriptions, and tuning suggestions Parameter
Default Value
Description
Tuning Suggestion
server.tomcat.max-threads
200
Specifies the maximum number of Tomcat threads for processing concurrent requests.
Adjust the parameter based on the number of CPU cores and memory of the server.
Generally, 200 to 300 threads is a proper initial setup.
server.tomcat.min-spare-threads
5
Specifies the minimum number of threads created during Tomcat startup. This function ensures that requests can be responded immediately when the load increases.
Adjust the parameter based on application load and the expected number of concurrent requests.
Generally, set a value ranging from 10 to 20.
server.tomcat.max-connections
8192
Specifies the maximum number of connections allowed by Tomcat.
Adjust the parameter based on the concurrent connection requirements of applications.
For heavy-traffic applications, increase the value of this parameter.
server.tomcat.keep-alive-timeout
60000
Specifies the connection timeout interval, in milliseconds. If no data is transferred within the timeout interval, the connection is closed.
Set a value ranging from 15000 to 30000 (15 to 30 seconds) to balance connection retention and resource release.
server.tomcat.max-keep-alive-requests
100
Specifies the maximum number of requests that can be processed by a connection.
In the scenario where there are frequent short connections, you can downgrade the configuration of Tomcat's persistent connection function (or set the value to 0 or 1 to disable it). The purpose is to reduce the resource occupation caused by connection retention.
On the contrary, in the scenario where there are multiple requests with a single connection, you can increase the value of this parameter to reduce the response latency and resource consumption caused by connection establishment.
server.servlet.session.timeout
30m
Specifies the session timeout interval for inactive sessions.
Generally, the session timeout interval is adjusted based on the user interaction frequency. You can set a value ranging from 30m to 1h (30 minutes to 1 hour).
If you frequently send requests through a single connection, increase the timeout interval. If you do not send requests frequently, decrease the timeout interval.
server.http2.enabled
false
Specifies whether to enable the HTTP/2 protocol.
HTTP/2 is an important network transmission protocol and has higher performance than that of HTTP/1.1. You are advised to enable HTTP/2 to improve web application performance. Set the value to true to enable HTTP/2.
server.max-http-header-size
8KB
Specifies the maximum number of bytes in an HTTP request header. This prevents the client from sending an oversized request header, which may cause out of memory (OOM) or excessive resource occupation.
If services are not affected, you can reduce the value of this configuration item to improve system security and stability.
server.compression.enabled
false
Specifies whether to enable the web page compression function. When the parameter is set to false, the compression function is disabled. If you want to reduce the amount of data sent to the client and improve the transmission efficiency, you can set true to enable the compression function.
You are advised to set the parameter to true to enable the compression function to improve performance.
server.tomcat.compression.min-response-size
2KB
Specifies the minimum response size required for the Tomcat server to perform compression.
Setting min-response-size to 2KB usually achieves good results, which indicates that the Tomcat server compresses a response only when the response size exceeds 2 KB. This setting helps avoid unnecessary compression of very small responses, thereby saving server resources.
- Bind Tomcat to cores.
1 2
cd /home/tomcat-test-01 taskset -c N java -jar target/tomcat-test-01-0.0.1-SNAPSHOT.jar
- Modify the configuration options in the application.properties file of the Spring Boot project. For details about the parameter names, default values, descriptions, and tuning suggestions, see Table 1.