Mastering Tomcat Performance: Tuning Threads, Connections, and Linux Kernel Settings
This guide explains how to tune Tomcat 8.5 by adjusting server.xml parameters such as maxThreads, acceptCount, maxConnections, and connectionTimeout, while also covering related Linux kernel settings, file‑descriptor limits, and JVM options to achieve optimal concurrency and response times.
Tomcat Self‑Optimization
Tomcat tuning focuses on several server.xml attributes. Understanding each parameter’s purpose is essential.
1. maxThreads
Defines the maximum number of request‑processing threads. The default is 200. For CPU‑bound workloads, a smaller value reduces thread‑switching overhead; for I/O‑bound workloads, a larger value increases concurrency. Setting it too high (e.g., 3000) can cause response times to spike because the CPU spends most of its time switching threads.
2. acceptCount
Specifies the maximum number of queued requests when all maxThreads are busy. Default is 100. If the queue is full, new connections are refused. The effective queue length is the smaller of acceptCount and the Linux kernel parameter net.core.somaxconn (default 128).
Experiment:
Set acceptCount=3000 and net.core.somaxconn=8192; ss -lt shows send_q=3000.
Set acceptCount=10000 with the same net.core.somaxconn; send_q=8192, confirming the kernel limit dominates.
3. maxConnections
Maximum simultaneous connections the server will accept. When reached, the server accepts one extra connection but does not process it until the count drops below maxConnections. Default varies: NIO/NIO2 → 10000, APR/native → 8192. The value can be disabled with -1.
4. connectionTimeout
Time (ms) Tomcat waits for the request URI after a socket is accepted. Default in Tomcat’s server.xml is 20000 ms (20 s). Setting -1 disables the timeout.
Typical server.xml modifications:
vi server.xml
# Replace the commented Executor block
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="1500" minSpareThreads="50" prestartminSpareThreads="true"/>
# Update the Connector
<Connector executor="tomcatThreadPool" port="8009" protocol="org.apache.coyote.http11.Http11Nio2Protocol" connectionTimeout="20000" maxConnections="10000" redirectPort="8443" acceptCount="1500"/>Linux Kernel Parameter Optimization
Linux limits affect Tomcat’s ability to handle many concurrent connections.
1. File descriptor limits
Check the per‑user limit with ulimit -u (default 1024). Increase both soft and hard limits in /etc/security/limits.conf:
prouser soft nofile 65536
prouser hard nofile 65536
prouser soft nproc 65536
prouser hard nproc 655362. TCP‑related kernel settings
Edit /etc/sysctl.conf and add:
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 10000
net.core.somaxconn = 8192Apply changes with sudo /sbin/sysctl -p.
JVM Tuning for Tomcat
Typical JVM options for a high‑performance Tomcat instance:
JAVA_OPTS="$JAVA_OPTS -server -Xmn2000m -Xms4000m -Xmx4000m -XX:PermSize=128m -XX:+UseConcMarkSweepGC -XX:MaxPermSize=512m -Djuli-logback.configurationFile=file:$CATALINA_HOME/conf/logback.xml"Executor configuration example:
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="500" minSpareThreads="30" maxIdleTime="60000" prestartminSpareThreads="true" maxQueueSize="100"/>Key parameters:
maxThreads : recommended 500‑800 based on hardware and workload.
minSpareThreads : initial thread count, default 25.
maxIdleTime : idle thread lifespan in ms (default 60000).
prestartminSpareThreads : pre‑creates the minimum spare threads at startup.
maxQueueSize : maximum waiting queue length before rejecting requests.
Combining proper server.xml settings, Linux kernel tuning, and JVM options yields a balanced Tomcat deployment capable of handling high concurrency with stable response times.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
