Master Tomcat Performance: Tuning Threads, Connections, and Linux Kernel for Optimal Throughput
This guide explains how to optimize Tomcat by adjusting maxThreads, acceptCount, maxConnections, and connectionTimeout, while also tuning Linux kernel parameters, TCP settings, and JVM options to achieve balanced CPU and I/O performance for high‑concurrency web services.
1. Tomcat Self‑Optimization
1. maxThreads
The
maxThreadsattribute defines the maximum number of request‑processing threads Tomcat can create (default 200). For CPU‑bound workloads, a smaller value reduces thread‑switching overhead; for I/O‑bound workloads, a larger value allows more concurrent requests. Over‑allocating threads (e.g., 3000) can cause severe response‑time degradation because the CPU spends most time switching threads.
2. acceptCount
acceptCountspecifies the maximum number of pending connections queued when all processing threads are busy (default 100). If the queue is full, new connections are refused. The effective queue length is the lesser of the connector’s backlog and the kernel’s
net.core.somaxconnvalue.
3. maxConnections
maxConnectionslimits the total number of simultaneous connections the server will accept. When this limit is reached, additional connections are accepted but not processed until the count drops below the limit. The default varies by connector (e.g., 10000 for NIO, 8192 for APR).
4. connectionTimeout
The
connectionTimeout(default 20000 ms in Tomcat’s
server.xml) defines how long Tomcat waits for the request line after a socket is established. Setting it to
-1disables the timeout.
2. Linux Kernel Parameter Optimization
To support higher TCP concurrency, increase the per‑user file descriptor limits and adjust kernel networking parameters.
<code>ulimit -u 65536 # increase max processes per user
</code>Edit
/etc/security/limits.confand add:
<code>prouser soft nofile 65536
prouser hard nofile 65536
prouser soft nproc 65536
prouser hard nproc 65536
</code>Adjust TCP settings in
/etc/sysctl.conf:
<code>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.core.somaxconn = 8192 # accept queue length
</code>Apply changes with
sudo /sbin/sysctl -p.
3. JVM Tuning
Configure Java options for Tomcat’s JVM to provide sufficient heap and garbage‑collection settings:
<code>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"
</code>Example
Executorconfiguration in
server.xml:
<code><Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="500" minSpareThreads="30" maxIdleTime="60000" prestartminSpareThreads="true" maxQueueSize="100" />
</code>Key parameters:
maxThreads : maximum concurrent threads (recommended 500‑800 based on hardware).
minSpareThreads : initial thread pool size.
maxIdleTime : idle thread timeout (ms).
prestartminSpareThreads : pre‑initialize
minSpareThreadsat startup.
maxQueueSize : maximum request queue length before rejection.
Typical Tomcat architecture consists of a Connector that accepts sockets and places them into an accept queue (length governed by
acceptCount), an Acceptor thread that moves connections to the Executor, and the Executor that dispatches worker threads to process servlet requests.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
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.