Mastering Tomcat Performance: Key Tuning Parameters and Best Practices
This article explains how to optimize Tomcat 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 balanced performance for both CPU‑bound and I/O‑bound workloads.
Tomcat Self‑Optimization
1. maxThreads
maxThreads is the maximum number of request‑processing threads that Tomcat can create, determining the maximum concurrent requests. The default value is 200. If an executor is associated, the attribute is ignored and reported as -1.
When the workload is CPU‑bound, a smaller maxThreads reduces thread‑context switching and improves throughput. When the workload is I/O‑bound, a larger maxThreads allows more concurrent requests, but requires sufficient JVM memory and Linux open‑file limits.
Testing showed that setting maxThreads to a very high value (e.g., 3000) caused response times to spike once the active thread count exceeded about 2000, because the CPU spent most of its time switching threads rather than executing application code.
2. acceptCount
acceptCount defines the maximum number of queued requests when all request‑processing threads are busy. The default is 100. If the queue is full, new connections are refused.
Three scenarios illustrate its behavior:
If the current thread count is below maxThreads, Tomcat creates a new thread for the request.
If the thread count has reached maxThreads, the request is placed in the waiting queue.
If the waiting queue size reaches acceptCount, the request is rejected with a connection‑refused error.
Experiments comparing acceptCount with the Linux kernel parameter net.core.somaxconn showed that acceptCount only takes effect when it is less than or equal to net.core.somaxconn (default 128). Setting acceptCount larger than net.core.somaxconn results in the kernel limiting the queue size to net.core.somaxconn.
3. maxConnections
maxConnections is the maximum number of simultaneous connections the server will accept and process. When the limit is reached, additional connections are accepted but not processed until the number drops below maxConnections. The default varies by connector type (e.g., 10000 for NIO/NIO2, 8192 for APR/native).
4. connectionTimeout
connectionTimeout (default 20000 ms in Tomcat’s server.xml) is the time Tomcat waits after accepting a socket for the request URI line. A value of –1 disables the timeout. If disableUploadTimeout is false, the same timeout applies to reading the request body.
Configuration Example
vi server.xml
# Replace the commented Executor with:
<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
1. File Descriptor Limits
Check the current soft limit with ulimit -u (default 1024). Increase the soft and hard limits for open files and processes by editing /etc/security/limits.conf:
prouser soft nofile 65536
prouser hard nofile 65536
prouser soft nproc 65536
prouser hard nproc 655362. TCP Kernel Limits
Modify /etc/sysctl.conf to tune TCP behavior:
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 = 8192
sudo /sbin/sysctl -pJVM Tuning
Typical JVM options for Tomcat:
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"Default Executor configuration (commented out): <!-- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/> --> Modified Executor configuration: <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="500" minSpareThreads="30" maxIdleTime="60000" prestartminSpareThreads="true" maxQueueSize="100"/> Key parameters: maxThreads : maximum concurrent threads (default 200, often set 500‑800). minSpareThreads : initial thread pool size (default 25). maxIdleTime : idle thread timeout in milliseconds (default 60000). prestartminSpareThreads : whether to pre‑create minSpareThreads at startup. maxQueueSize : maximum request queue size before rejecting new requests.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
