<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java crumbs &#187; Tomcat</title>
	<atom:link href="http://blog.krecan.net/tag/tomcat/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.krecan.net</link>
	<description>Short remarks from Java world</description>
	<lastBuildDate>Tue, 31 Jan 2012 20:13:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>More than 16,000 connections in Tomcat</title>
		<link>http://blog.krecan.net/2010/05/06/more-than-16000-connections-in-tomcat/</link>
		<comments>http://blog.krecan.net/2010/05/06/more-than-16000-connections-in-tomcat/#comments</comments>
		<pubDate>Thu, 06 May 2010 12:19:27 +0000</pubDate>
		<dc:creator>Lukáš Křečan</dc:creator>
				<category><![CDATA[Articles in English]]></category>
		<category><![CDATA[Threads]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://blog.krecan.net/?p=566</guid>
		<description><![CDATA[This is just a short update. Last time, I have reached around 13,000 concurrent connections in Tomcat. Based on some comments (thanks IA), I have updated the test. The most important change is that HttpClient is used instead of standard HTTP connection. Moreover the servlet address is hard-coded, so a new String is not created [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a short update. <a href="http://blog.krecan.net/2010/05/02/cool-tomcat-is-able-to-handle-more-than-13000-concurrent-connections/">Last</a> time, I have reached around 13,000 concurrent connections in Tomcat. Based on some comments (thanks IA), I <a href="https://java-crumbs.svn.sourceforge.net/svnroot/java-crumbs/trunk/threads/src/main/java/net/javacrumbs/test/ThreadsServlet.java">have updated the test</a>. </p>
<p>The most important change is that <a href="http://hc.apache.org/httpclient-3.x/">HttpClient</a> is used instead of standard HTTP connection.  Moreover the servlet address is hard-coded, so a new String is not created every time. I do not believe that this change has big impact but the code generates less garbage so GC has easier job. </p>
<p>With this small enhancements, I was able to get more than 16,000 connections running</p>
<pre name="code" class="text">
...
Servlet no. 16354 called.
Servlet no. 16355 called.
Servlet no. 16356 called.
Servlet no. 16357 called.
May 6, 2010 1:21:54 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.SocketException) caught when processing request: Too many open files
May 6, 2010 1:21:54 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
</pre>
<p>Again, we get "To many open files". The limit is 32768 and we have two open connections per thread (incoming/outgoing). Theoretically, we could get even higher, but the heap was still filling really fast, so GC was quite slow. And again, threads are not the main issue, the problem is still somewhere else.</p>
<p>The source code is available <a href="https://java-crumbs.svn.sourceforge.net/svnroot/java-crumbs/trunk/threads">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.krecan.net/2010/05/06/more-than-16000-connections-in-tomcat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cool, Tomcat is able to handle more than 13,000 concurrent connections.</title>
		<link>http://blog.krecan.net/2010/05/02/cool-tomcat-is-able-to-handle-more-than-13000-concurrent-connections/</link>
		<comments>http://blog.krecan.net/2010/05/02/cool-tomcat-is-able-to-handle-more-than-13000-concurrent-connections/#comments</comments>
		<pubDate>Sun, 02 May 2010 09:37:35 +0000</pubDate>
		<dc:creator>Lukáš Křečan</dc:creator>
				<category><![CDATA[Articles in English]]></category>
		<category><![CDATA[Threads]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://blog.krecan.net/?p=539</guid>
		<description><![CDATA[Last time I have promised you to take a look at more real life scenario regarding threads. In the last blog entry I have shown that on modern operating system and JVM it's not a problem to create 32,000 threads. Now I want to test how many threads can be handled by a Tomcat instance. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.krecan.net/2010/04/07/how-many-threads-a-jvm-can-handle/">Last time</a> I have promised you to take a look at more real life scenario regarding threads. In the last blog entry I have shown that on modern operating system and JVM it's not a problem to create 32,000 threads. Now I want to test how many threads can be handled by a Tomcat instance. </p>
<p>I just want to remind you the motivation. Some people believe that threads are expensive, that we should not create lot of them. They believe that it's better to use different mechanisms like asynchronous servlets, specialized libraries etc. I just want to find out if we really need such measures or if good old threads are good enough.</p>
<p>If you read <a href="http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html">articles</a> about asynchronous servlets, you find out that the main motivation is AJAX. Mainly the scenario, when a HTTP connection is open for a long time and the data are sent when an event occurs. </p>
<p>OK, let's simulate it. We need to simulate lot of open HTTP connections waiting for an event. The easiest way to achieve it is my precious suicidal servlet.</p>
<pre name="code" class="java">
public class ThreadsServlet extends HttpServlet {
	private static final long serialVersionUID = 7770323867448369047L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int number = Integer.valueOf(req.getParameter("number"));
		try {
			System.out.println("Servlet no. "+number+" called.");
			URL url = new URL(req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getRequestURI()+"?number="+(number+1));
			Object content = url.getContent();
			resp.setContentType("plain/text");
			resp.getWriter().write("OK: "+content);
		} catch (Throwable e) {
			String message = "Reached "+number+" of connections";
			System.out.println(message);
			System.out.println(e);
			resp.getWriter().write(message);
		}
	}
}
</pre>
<p>The servlet is quite simple, it just opens HTTP connection to itself. So it basically tries to create infinite number of connections. Top keep track of the progress, there is a request parameter “number” that is incremented with each call. We can thus observe how many active connections we have.</p>
<h2>Default configuration</h2>
<p>Let's run it. Just open “http://localhost:8080/threads/something?number=1” in your browser and see what happens.</p>
<p>Not much, in console (or logs/catalina.out) you can see </p>
<pre name="code" class="text">
...
Servlet no. 37 called.
Servlet no. 38 called.
Servlet no. 39 called.
Servlet no. 40 called.
</pre>
<p>What? Only 40 concurrent threads served? That's not much. Let's try better. </p>
<h2>Connector configuration</h2>
<p>We can reconfigure Tomcat connector to be able to serve more connections (server.xml)</p>
<pre name="code" class="xml">
    &lt;Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxThreads="32000"/&gt;
</pre>
<p>As we know from the last time, 32K is the OS limit, we can't go over that. If we execute the test, the results are slightly better:</p>
<pre name="code" class="text">
Servlet no. 485 called.
Servlet no. 486 called.
Servlet no. 487 called.
Servlet no. 488 called.
Servlet no. 489 called.
Servlet no. 490 called.
May 1, 2010 5:55:32 PM org.apache.tomcat.util.net.JIoEndpoint$Acceptor run
SEVERE: Socket accept failed
java.net.SocketException: Too many open files
        at java.net.PlainSocketImpl.socketAccept(Native Method)
        at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:358)
        at java.net.ServerSocket.implAccept(ServerSocket.java:470)
        at java.net.ServerSocket.accept(ServerSocket.java:438)
        at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:61)
        at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:310)
        at java.lang.Thread.run(Thread.java:636)
</pre>
<p>Wow, it looks like, that there is some limit on open files. Since I am not Linux guru, the first thing I have tried was to change Tomcat connector to nonblocking.</p>
<h2>Nonblocking Connector</h2>
<p>To use nonblocking connector, you have to set the protocol in server.xml</p>
<pre name="code" class="xml">
  &lt;Connector port="8080"
               connectionTimeout="20000"
               redirectPort="8443"
		protocol="org.apache.coyote.http11.Http11NioProtocol"
		maxThreads="32000"/&gt;
</pre>
<p>Unfortunately the result is almost the same: </p>
<pre name="code" class="text">
Servlet no. 483 called.
Servlet no. 484 called.
Servlet no. 485 called.
Servlet no. 486 called.
May 1, 2010 5:59:24 PM org.apache.tomcat.util.net.NioEndpoint$Acceptor run
SEVERE: Socket accept failed
java.io.IOException: Too many open files
        at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
        at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:163)
        at org.apache.tomcat.util.net.NioEndpoint$Acceptor.run(NioEndpoint.java:1198)
        at java.lang.Thread.run(Thread.java:636)
</pre>
<h2>Increase Open File Limit</h2>
<p>Apparently, the blocking connector was not the issue. After some time spent with Google I have found the answer. Linux is limiting number of open connections. You can execute “ulimit -n” to see what's your limit. Luckily, it's possible to change  the limit. You can either set it by “ulimit -n 32768” if you have permissions or by adding following lines to  /etc/security/limits.conf (lukas is my username)</p>
<pre name="code" class="text">
lukas            hard    nofile          32768
lukas            soft    nofile          32768
</pre>
<p>To apply this change you have to logout and login. After that, you will see this:</p>
<pre name="code" class="text">
Servlet no. 5856 called.
Servlet no. 5857 called.
Servlet no. 5858 called.
Servlet no. 5859 called.
May 1, 2010 6:07:58 PM org.apache.tomcat.util.net.NioEndpoint$SocketProcessor run
SEVERE:
java.lang.OutOfMemoryError: GC overhead limit exceeded
	at java.util.Arrays.copyOf(Arrays.java:2894)
	at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:117)
	at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:407)
	at java.lang.StringBuilder.append(StringBuilder.java:136)
	at java.lang.StringBuilder.append(StringBuilder.java:132)
	at java.lang.Throwable.printStackTrace(Throwable.java:529)
	at java.util.logging.SimpleFormatter.format(SimpleFormatter.java:94)
	at java.util.logging.StreamHandler.publish(StreamHandler.java:196)
	at java.util.logging.ConsoleHandler.publish(ConsoleHandler.java:105)
	at java.util.logging.Logger.log(Logger.java:476)
	at java.util.logging.Logger.doLog(Logger.java:498)
	at java.util.logging.Logger.logp(Logger.java:698)
	at org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:167)
	at org.apache.juli.logging.DirectJDKLog.error(DirectJDKLog.java:135)
	at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:755)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:2080)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
	at java.lang.Thread.run(Thread.java:636)
</pre>
<p>We have reached 5856 threads before we run out of memory. Cool. </p>
<h2>Increase Heap Size</h2>
<p>Let's try to increase the heap size. Just add “-Xmx2048m” to JAVA_OPTS. Before I have started running out of heap, I got to cca 11000 threads! Is it enough? I do not know, but I think it's pretty good. </p>
<p>Moreover, if you do a heap dump, you will see, that most of the memory is consumed by char and byte arrays. (This heap dump has been taken with approximately 5000 connections, screenshot from VisualVM)</p>
<p><img src="/files/heap1.png" alt="Heap Dump" /></p>
<p>It's understandable, we have lot of open buffers on both sides. I assume that we have at least four buffers per servlet. One for sevlet request, one for servlet response, one for URL request and one for URL response. But maybe there will be other buffers as well. To be honest, I have to admit that memory consumed by stacks would not appear here, it's probably handled by OS. But we have run out of the heap, so that's why I am talking about it.</p>
<h2>Smaller buffers</h2>
<p>We can try to make some of the buffers smaller. I was able to find only one setting that had some effect. Again it is connector setting in server.xml config file.</p>
<pre name="code" class="xml">
  &lt;Connector port="8080"
               connectionTimeout="200000"
               redirectPort="8443"
		protocol="org.apache.coyote.http11.Http11NioProtocol"
		maxThreads="32000"
		socket.appReadBufSize="1024"
		socket.appWriteBufSize="1024"
		bufferSize="1024"/&gt;
</pre>
<p>With this setting, I was able to get near to 13000 open connections.</p>
<pre name="code" class="text">
...
Servlet no. 13327 called.
Servlet no. 13328 called.
Servlet no. 13329 called.
Servlet no. 13330 called.
Servlet no. 13331 called.
Servlet no. 13332 called.
</pre>
<p>After that the machine started to run out of physical memory, GC took ages so I had to stop the server. (Just to remind you, my test machine is two year old laptop with Intel Core 2 Duo T8100 2.1GHz with 4GB of RAM. There is 64bit Linux 2.6.32 and OpenJDK (IcedTea6 1.8) running on top of it.) </p>
<p>As we have seen, threads are not the major issue on modern machines. There is probably significant amout of memory consumed by the stack traces too, but I think the biggest problem are the buffers. And the important point is, that we would need the buffers even if we used asynchronous servlets! Of course, there is still some overhead connected with threads, so asynchronous libraries have their place. In fact, it would be nice to try similar experiment with asynchronous servlets. I am afraid, that I will not be able to do it, but I will be glad to help if there is some volunteer.</p>
<p>Please also note that your numbers may vary. After all this has been quite artificial test. I think it's simulates lot of real-life use cases, but you know, the reality is always different.</p>
<p>On the other hand, with more physical memory and better Tomcat configuration, we might got to higher numbers. I have heard <a href="http://www.javalobby.org/java/forums/t92965.html">legends</a> about 16K threads. </p>
<p>I think that I will finish with my favorite message. Do not use complicated constructs unless you are sure you need them. Please remember golden rules of optimization:</p>
<blockquote><p>The First Rule of Program Optimization: Don't do it.<br />
The Second Rule of Program Optimization (for experts only!): Don't do it yet.</p></blockquote>
<p>If you want to verify my results, the source code is <a href="https://java-crumbs.svn.sourceforge.net/svnroot/java-crumbs/trunk/threads/">here</a>.  If you have some comments, different results or advices, do not hesitate to add a comment. </p>
<p>Resources:<br />
<a href="http://people.apache.org/~fhanik/http.html">Tomcat connector config</a></p>
<p><a href="http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html">Why we need asynchronous servlets</a></p>
<p><em>Note: If you wonder why the hell I have started to write in something that looks almost like English when apparently I do even more mistakes than in Czech, the answer is simple. I just need to practice my English (apart from that I want to be world famous, not just known in Czech Republic)</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.krecan.net/2010/05/02/cool-tomcat-is-able-to-handle-more-than-13000-concurrent-connections/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

