The maximum number of connections that Apache Tomcat can handle is defined within the Tomcat settings. In essence, this setting determines the number of concurrent connections that can be handled by Apache Tomcat.
Out of the box, Apache Tomcat is configured to handle around 200 simultaneous connections (kind of…), which for most web servers and projects is more than adequate. But when you get into the realms of millions of monthly active users and concurrent connections, you’ll soon need to optimise Tomcat to suit your needs accordingly.
Ultimately though, the number of simultaneous connections that the Apache Tomcat software on your web server is capable is handling is determined by the physical limitations of your web server. The bigger your hardware, the bigger the number of connections that Apache Tomcat is capable of handling. This being said, the 200 maximum connections as a default is merely an arbitrary number, so you’ll need to assess this in relation to the hardware you are running on. The bigger the server hardware you’re running on, the higher the number of maximum connections on Apache Tomcat your server will be able to handle. As always, this can change depending on what you are doing. For example, if you have a process on your really fast server that is calling a third party dog slow server, then this soon becomes the limiting factor in the chain.
Max connections from an Apache Tomcat perspective is calculated based on a number of factors such as the number of simultaneous connections multiplied by the maximum execution time that each thread is capable of handling. Under normal circumstances this can appear to be behaving correctly, so keep in mind exceptional circumstances when things don’t quite perform as you wish. What you’ll soon spot (via the Tomcat Manager) is that the Max Connections soon starts to creep up and eat up all the resources on your server. This can be caused by many factors, from long running processes on your own server, or code on that is calling third party servers which may be running slow and causing problems.
With all this in mind, let’s take a look at how to increase the maximum connections on Apache Tomcat. It’s actually quite simple to do.
Run the command;
sudo nano /usr/share/tomcat8/conf/server.xml
This command will open the editor to edit the file Server.xml which controls many of the settings required for configuring Apache Tomcat. You’ll note that it is Apache Tomcat 8 that is being used in this example, so edit this accordingly for the version of Apache Tomcat that you are using. Again, depending on your individual configuration of your server, this file may live in a different file path. So to easily find the file if the above command doesn’t work, then run the command below;
find / -type f -name server.xml
The above command will search for a File (f) called “server.xml”
What you’re looking for within this file is the setting for “maxConnections” which will have a value set. You may not have this variable set so look out for the core settings for the port that your Tomcat instance is listening on, for example;
<Connector port=”8080” protocol=”HTTP/1.1” maxConnections=”200”/>
The maximum number of connections that the server will accept and process at any given time. When this number has been reached, the server will accept, but not process, one further connection. This additional connection be blocked until the number of connections being processed falls below maxConnections at which point the server will start accepting and processing new connections again.
Depending on your specific requirements, you may also need to increase the maxThreads setting too. Hope that helps getting you started with tuning Apache Tomcat for your specific needs.
Great post Michael, it’s very clear and accurate. Is there a way to change maxConnections value in execution time? I need an application which increases or decreases maxConnections value depending of current connections number. Thanks for your help.
There is always a way – but you’ll have some trade-offs. For any new settings to take effect within this file, you have to restart Tomcat, you could create a script that would change the settings within the file (this would have to be outside of the Servlet Container as and Java applications running there would not be able to access this file). Then this script would have to restart Tomcat too. While this all would be possible, it would be a fair amount of work.
I’d think about the requirements you are trying to build against as there may be a better way. What is the reason you’re looking to dynamically increase/decrease the maxConnections for Tomcat specifically? Could you not throttle this within your application layer?
Regards,
Michael
very very great!!
Hi Michael,
In your context, is connection == request == thread ?
In that case, whats the relation between TCP connection and the connection/thread per request ?
If 100 requests are coming from same client (say a browser), is it going to just one TCP connection opened and 100 tomcat threads working?
Thank you,
Sreenivas.
Hi Sreenivas,
Honestly, I’m not 100% sure. I would have thought that Tomcat runs every new TCP connection in a new thread or a pool of threads….. in fact, let me Google that….. Yes, it does… “Tomcat relies on java.net.Socket and other related classes. It runs on TCP port(Default: 8080) and identifies each request by IP address of host and TCP port used by host to connect to Tomcat. A HTTP request is sent by the browser over this connection. Tomcat contains pool of threads to handle multiple HTTP requests. For each request tomcat assigns a thread from its pool to handle request.When the response has been generated and sent back, this thread gets free and ready to serve another request.” taken from https://stackoverflow.com/questions/28183075/how-does-tomcat-or-any-webserver-handles-requests-and-dispatches-responses
In the example you give, 100 browser windows open on the client side would be 100 independent TCP connections to the server side. So that would be 100 threads (assuming you’ve got Tomcat configured to handle this).
In reality though, things aren’t that simple. Since a connection/request/thread will technically be unavailable for only a small amount of time until the Request/Response cycle of an initiated user action (i.e. load page) takes place, which can take micro-seconds to action. You tend to only find this as an issue in the situation either where you have an extremely high traffic website/application with a high number of concurrent connections and/or a web application that is heavily integrated with various other systems that is pushing/pulling data around a wider application landscape.
As with anything, monitor your environments and increase as/when necessary. Thankfully the Apache Tomcat Manager Application lists the number of active connections at any point in time, and there are various pieces of server/application monitoring software that are available to help with this. Unfortunately virtually all of these are either extremely challenging to configure and/or extremely pricey to purchase. Both options are very good, but you pay either way in varying forms (cash or time).
Hope that helps 🙂
Regards,
Michael