120

In Tomcat's server.xml what is maxThreads versus maxConnections?

I understand that maxConnections is the number of connections open to the server.

And maxThreads is the maximum number of request processing threads .

But how do these two configuration parameters work together? -- Obviously you will not set maxConnections to 1000 and maxThreads to 10.

What is the relationship between the two configuration parameters?

<Connector 
    port="8443" 
    protocol="org.apache.coyote.http11.Http11Protocol"
    maxThreads="250" 
    SSLEnabled="true" 
    scheme="https" secure="true"
    clientAuth="false" 
    sslProtocol="TLS" 
    connectiontimeout="20000"
/>
1

2 Answers 2

156

Tomcat can work in 2 modes:

  • BIO – blocking I/O (one thread per connection)
  • NIOnon-blocking I/O (many more connections than threads)

Tomcat 7 is BIO by default, although consensus seems to be "don't use BIO because NIO is better in every way". (And BIO has been completely thrown out of 8.5.0 and later versions.) You set this using the protocol parameter in the server.xml file.

  • BIO will be HTTP/1.1 or org.apache.coyote.http11.Http11Protocol
  • NIO will be org.apache.coyote.http11.Http11NioProtocol

If you're using BIO then I believe they should be more or less the same.

If you're using NIO then actually "maxConnections=1000" and "maxThreads=10" might even be reasonable. The defaults are maxConnections=10,000 and maxThreads=200. With NIO, each thread can serve any number of connections, switching back and forth but retaining the connection so you don't need to do all the usual handshaking which is especially time-consuming with HTTPS but even an issue with HTTP. You can adjust the "keepAlive" parameter to keep connections around for longer and this should speed everything up.

3
  • 1
    Default protocol appears to be "HTTP/1.1" which uses an auto-switching mechanism (tomcat.apache.org/tomcat-7.0-doc/config/http.html) "...to select either a blocking Java based connector or an APR/native based connector. If the PATH (Windows) or LD_LIBRARY_PATH (on most unix systems) environment variables contain the Tomcat native library, the APR/native connector will be used. If the native library cannot be found, the blocking Java based connector will be used"
    – alpa
    Commented Dec 19, 2020 at 11:11
  • 7
    If maxThreads=200, and the server receives 201 simultaneous requests, what happens?
    – payne
    Commented Dec 15, 2021 at 0:46
  • 3
    When requests exceed available threads, new threads are created up to maxThreads. If more requests arrive, Tomcat accepts them until maxConnections. Connections are queued until a thread is free. If maxConnections is reached, OS queues additional connections. OS queue size is controlled by acceptCount. If OS queue fills, requests may be refused or time out.
    – talabes
    Commented Jun 22, 2023 at 10:35
17

From Tomcat documentation, For blocking I/O (BIO), the default value of maxConnections is the value of maxThreads unless Executor (thread pool) is used in which case, the value of 'maxThreads' from Executor will be used instead. For Non-blocking IO, it doesn't seem to be dependent on maxThreads.

3
  • 8
    I'm not the downvoter but I think you have not answered the question. You've merely explained what the defaults are, without teaching the asker the concepts, and the question is about the concepts.
    – Tim Cooper
    Commented Sep 10, 2014 at 12:47
  • 2
    Got it. I took the question as asking the relation between them rather than what they actually mean.
    – Swapnil
    Commented Sep 10, 2014 at 13:30
  • 2
    This answer was also usefull, since it clarifies that even with BIO, maxConnections can be greater than maxThreads (I think that this is new in Tomcat 7)
    – Ivan
    Commented Apr 2, 2015 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.