3

I have created simple Spring Boot MVC application and noticed (using JVisualVM) that all my threads have prefix nio. That mean that Tomcat use java.nio package. We can add the following params to Tomcat configuration : maxThreads,maxConnections. As I know that mean: For example we have maxThreads = 2 , maxConnections = 10000, then Selector of first thread (from java.nio) can handle 10000 concurrent requests, but execute each sequentially according to selectorKeys and the same behavior for the second thread.

Does it work in this way , if yes how do you usually choose the best option for maxConnections

Thank in advance

(BTW I use Tomcat 8 )

Almas Abdrazak
  • 3,209
  • 5
  • 36
  • 80

3 Answers3

9

The maximum number of client connections is acceptCount + maxConnections. Too low and requests may be rejected unnecessarily. Too high and clients may be starved if throughput can't keep up.

acceptCount: - 100 by default - This is used as the backlog parameter for ServerSocket.bind. The OS's TCP stack will queue at most acceptCount pending socket connections. This occurs before Tomcat starts processing the connection.

maxConnections: - 10,000 by default - The maximum number of connections that Tomcat allows to be in progress internally.

maxThreads: - 200 by default under NIO and NIO2 - Maximum number of concurrent request processing threads.

The goal is to optimize service availability and performance and utilize resources efficiently. The settings will depend on the latency and load distribution of your request processors.

There are many different approaches to capacity planning depending on the goals. A simple option is to increase maxThreads to the point where the server wouldn't be able to safely process any additional requests within a reasonable period of time. At this point of saturation, accepting more connections would be inadvisable since it would take too long to clear them out. So consider how much CPU time and RAM each processing thread requires and how many can safely run at the same time.

jspcal
  • 50,847
  • 7
  • 72
  • 76
  • From docs maxConnections = 10000, maxThreads =200 by default Maybe I don't understand, if maxConnections = 1000 so each tomcat thread can handle 1000 connections or all tomcat threads can handle 1000 connections together? – Almas Abdrazak May 29 '18 at 05:35
  • The maximum number of pending connections is `acceptCount`. The maximum number of *active* connections is `maxConnections`. – user207421 May 29 '18 at 05:58
  • @AlmasAbdrazak Each request processing thread handles one request at a time, serially. When a thread is done with a request, it picks up another pending request from the queue. `maxConnections` is the number of socket connections that are active and monitored at any given time. (This is entirely different from the number of NIO threads handling IO events on the sockets.) – jspcal May 29 '18 at 06:15
3

For example we have maxThreads = 2, maxConnections = 10000, then Selector of first thread (from java.nio) can handle 10000 concurrent requests, but execute each sequentially according to selectorKeys and the same behavior for the second thread.

No. They can handle 10,000 between them, in this case 5,000 each, sequentially. The maximum is global, not per-thread.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

I think what you've asked is answered here: Tomcat - maxThreads vs maxConnections

I believe it really depends on the nature of your application/service. It's best to do stress test the system and find the optimal values. Too many threads in a IO heavy application might cause significant delays because of high number of context switches.

regulus
  • 1,572
  • 12
  • 18