I was facing this issue with my test clients where HTTP status code 501 was thrown intermittently. It turned out to be an issue with the client. That's when my dear friend suggested this could be due to performance issues in Tomcat. Here are some things I found out.
Memory related changes:
Normally the JVM allocates an initial size for the Java heap and that's it, if you need more than this amount of memory you will not get it. For this, we need to set the minimum/maximum size of the Java heap. This can be altered using the -Xms/-Xmx parameters in the Java arguments. This can be set in the Java tab of the Tomcat Monitor.
-Xms1024m
-Xmx8192m
Connector alterations:
There are 2 default connectors configured in Tomcat server.xml viz.
- HTTP Connector - listens on port 8080 for incoming HTTP requests. Needed for stand-alone operation.
- AJP Connector- listens on port 8007 for incoming AJP requests. Needed for web-server integration (out-of-process servlet integration).
A good option is to use Thread Pools in Connectors. Tomcat is a multi-threaded servlet container. This means that each request needs to be executed by some thread. Prior to Tomcat 3.2, the default was to create a new thread to serve each request that arrives. This behavior is problematic for loaded sites because:
- Starting and stopping a thread for every request puts a needless burden on the operating system and the JVM.
- It is hard to limit the resource consumption. If 300 requests arrive concurrently Tomcat will open 300 threads to serve them and allocate all the resources needed to serve all the 300 requests at the same time. This causes Tomcat to allocate much more resources (CPU, Memory, Descriptors) than it should and it can lead to low performance and even crashes if resources are exhausted.
The solution for these problems is to use a thread pool, which is the default for Tomcat 3.2. Servlet containers that are using a thread pool relieve themselves from directly managing their threads. Instead of allocating new threads; whenever they need a thread they ask for it from the pool, and when they are done, the thread is returned to the pool.
Example configuration:
<Connector executor="tomcatThreadPool" maxThreads="1000"
minSpareThreads="50"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"
executor="tomcatThreadPool" maxThreads="1000"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"
executor="tomcatThreadPool" maxThreads="1000"
minSpareThreads="50" />
No comments:
Post a Comment