Which connection states must be counted when sizing Tomcat maxConnections for production traffic?
0 reputation · 18 Apr 2024, 05:46 UTC
0 reputation · 18 Apr 2024, 05:46 UTC
In a Tomcat 8.5+ NIO or NIO2 deployment, the connector attribute maxConnections limits the total number of sockets the connector will accept and process. Because idle keep-alive connections remain counted against that ceiling, a configuration that behaves correctly under light local traffic can exhaust the pool in production when clients maintain persistent connections between sparse requests. The uncertainty is whether the attribute should be sized only for expected concurrent active requests, or whether it must also reserve capacity for idle keep-alive sockets; published guidance often disagrees on this point.
The decision is complicated by version-specific defaults and connector differences. Tomcat 10.x changed the NIO2 default maxConnections from Integer.MAX_VALUE to 8192, which can surprise teams upgrading from earlier releases. Raising the limit also interacts with operating-system file-descriptor caps, and the APR/native connector does not necessarily treat idle connections the same way as NIO or NIO2. Without a clear rule, operators must guess whether to match maxThreads, sum active and idle peaks, or leave the default and adjust connectionTimeout instead.
Which sizing model should govern maxConnections for a production NIO2 connector: peak active requests only, or peak active plus idle keep-alive sockets? How does the correct calculation change when upgrading from a pre-10.x Tomcat with an effectively unlimited default to the 8192 default? Is there a documented, connector-specific rule that reconciles the NIO/NIO2 counting behavior with the different treatment in APR/native?
32275 reputation · 18 Apr 2024, 17:20 UTC
For a Tomcat 8.5+ NIO or NIO2 connector, maxConnections counts every socket that the connector has accepted. That includes sockets that are actively processing a request and sockets that are idle but kept alive by the HTTP keep‑alive mechanism. In other words, the peak number of concurrent active requests plus the peak number of idle keep‑alive connections must both fit within the maxConnections ceiling.
maxConnections count. They remain counted until the keep‑alive timeout expires or the client closes the socket.maxConnections value changed to 8192 in Tomcat 10.x (previous releases defaulted to Integer.MAX_VALUE).connectionTimeout only changes how long an idle socket is kept; it does not reduce the number of sockets that are counted against maxConnections.ulimit -n) will prevent new sockets from being accepted regardless of maxConnections.org.apache.coyote:type=Connector,port=… for currentConnections and currentThreadsBusy to capture the peak number of sockets and busy threads.lsof -iTCP -sTCP:LISTEN -P -n | grep java) to verify that the reported currentConnections matches the actual open TCP sockets for the Tomcat process.currentThreadsBusy value gives the number of sockets actively handling requests.currentConnections to infer the count of idle keep‑alive sockets at that moment.maxConnections:
peakActive + peakIdleKeepAlive.ulimit -n or sysctl.
currentConnections never approaches the new maxConnections value.To fine‑tune the recommendation, it would help to know:
connectionTimeout and keepAliveTimeout)?With those details, you can refine the margin and confirm that the chosen maxConnections will accommodate both active and idle traffic in production.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.