<?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/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
xmlns:rawvoice="http://www.rawvoice.com/rawvoiceRssModule/"
>

<channel>
	<title>Solution Hacker &#187; Tomcat</title>
	<atom:link href="http://www.solutionhacker.com/tag/tomcat/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.solutionhacker.com</link>
	<description>This blog provides solutions for enterpreneurs!</description>
	<lastBuildDate>Mon, 06 Feb 2012 07:19:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=226</generator>
<!-- podcast_generator="Blubrry PowerPress/2.0.4" -->
	<itunes:summary>This blog provides solutions for enterpreneurs!</itunes:summary>
	<itunes:author>Solution Hacker</itunes:author>
	<itunes:explicit>no</itunes:explicit>
	<itunes:image href="http://www.solutionhacker.com/wp-content/plugins/powerpress/itunes_default.jpg" />
	<itunes:subtitle>This blog provides solutions for enterpreneurs!</itunes:subtitle>
	<image>
		<title>Solution Hacker &#187; Tomcat</title>
		<url>http://www.solutionhacker.com/wp-content/plugins/powerpress/rss_default.jpg</url>
		<link>http://www.solutionhacker.com</link>
	</image>
		<item>
		<title>Streaming data to your grid</title>
		<link>http://www.solutionhacker.com/uncategorized/streaming-data-to-your-grid/</link>
		<comments>http://www.solutionhacker.com/uncategorized/streaming-data-to-your-grid/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 00:00:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[6. Uncategorized]]></category>
		<category><![CDATA[Scale]]></category>
		<category><![CDATA[Comet]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[liberator]]></category>
		<category><![CDATA[NIO]]></category>
		<category><![CDATA[push]]></category>
		<category><![CDATA[streaming]]></category>
		<category><![CDATA[thread]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=206</guid>
		<description><![CDATA[<h2>Push data to client</h2>
<p>Traditional web application is based on <strong>request and response model</strong> that information is delivered as a single payload and then immediately close the connection to the client. To keep the client in sync, we normally <strong>pull </strong>the server periodically. This approach may generate unacceptable load to the server. To solve this problem, we want to have a <strong>push </strong>mechanism from server to client. This is why <strong>Comet </strong>is defined. Comet is a generic term describing various approaches to send data asynchronously from a Web server to a client without the need for the client to explicitly request the data. It is an essential technique for any real-time event-driven web applications, where the majority of events occur on the server and data must be "pushed" frequently to the client. To achieve this, Comet servers must maintain <strong>a continuous connection</strong> to each client for the duration of the session.</p>
<div style="page-break-after: always;"><span style="display: none;">&#160;</span></div>
<p>OK. How to maintain <strong>a continuous connection</strong> to each client for the duration of the session?</p>
<blockquote>
<p>If you try to adapt traditional server to the Comet methodology, it may not scale and often fails after a few thousand simultaneously open connections. A true Comet implementation requires a very different kind of server architecture to be efficient and scalable - <a href="http://www.freeliberator.com/comet/">Liberator</a> <em>(a solid Comet server that are used by the financial industries. However, it is written in C and not open source although it has FREE edition distributed). </em></p>
</blockquote>
<p>To understand this statement a little bit more, we need to know how traditional web containers handle the request. They are under <strong>one request per thread </strong>model.</p>
<ol>
    <li>The client , typically , a browser sends request for resource to a web server.</li>
    <li>The server has <strong>a listening thread</strong> that keeps track of incoming connections.</li>
    <li>When a request arrives , the server uses one process or <strong>thread</strong> to process the request.</li>
    <li>The resource is returned to the client and the connection is closed.</li>
</ol>
<p>In this model, the number of requests that can be served in a second would depend on two things</p>
<ol>
    <li>How many threads are there to handle the client requests</li>
    <li>How long it takes to serve one request.</li>
</ol>
<p>If all threads of server are busy, then the incoming requests are <strong>put in a queue</strong>. The server would return to the requests in queue when server threads become free. The number of requests handled per second is always greater than the number of allowed simultaneous connections. All this is made possible because the time required to process a request is very short. In other words you can server more requests in a second than you have threads.</p>
<p>However, there are one breed of applications that need to <strong>hold onto the connections</strong>. Think of applications that require real time data coming to clients (stock tickers)&#160; or think of applications where low-latency is required. In the above traditional web model, the browser has to re-connect to get the new data. (Polling). If the new updates "can"&#160; happen with high frequency (e.g. a chat application) then the polling frequency also has to increase .&#160; An alternative to high frequency polling is to use <strong>push based applications</strong>. For push based application, once the browser connects to server, the server will maintain the connection till the browser time-out (<strong>server response stream is not closed</strong>) and keeps flushing data down the connection as and when they become available. In servlet container, to hold the connection, your thread in the <strong>service </strong>method cannot exit the method. Otherwise, the response stream will be closed. So what you do is, you block the thread on some condition within the service method. So the thread will block for your condition. When push data becomes available , this thread writes to response stream and again enters a blocked state. So as long as you hold onto the connection, you can not return this thread to the thread pool. And as more and more "push" connections are established you would run out of threads! To remedy the problem, the possible solutions are:</p>
<ol>
    <li>Increase # of server threads.</li>
</ol>
<h2>Flex Push</h2>
<p>There is confusion that whether <strong>BlazeDS</strong> supports real time messaging. Yes it does <img onclick="grin(':wink:');" alt=":wink:" src="../../../../../wp-includes/images/smilies/icon_wink.gif" />. In fact, BlazeDS has a full spectrum of channel types ranging from <strong>simple polling</strong>, to <strong>near-real-time polling,</strong> to <strong>real-time streaming</strong>.</p>
<ol>
    <li><strong>Simple polling</strong> - ping the server from Flex client using the traditional request and response model</li>
    <li><strong>Near-real-time polling</strong> (long polling) - Instead of acknowledging right away, the server could hold the polling request until there’s a message for the client. This ensure the messages are delivered to the client as soon as they become available. The caveat for using long-polling is the <strong>thread limitation</strong> in most application servers. At this moment, BlazeDS could not support more than a few hundred long-polling clients on most application servers. However, this problem could be resolved once servers like Tomcat start to support asynchronos, non-blocking connection threads. <span style="color: rgb(255, 0, 0);">Update: Now Tomcat 6 supports NIO.</span></li>
    <li><strong>Real-time streaming</strong> - BlazeDS supports real-time message streaming over AMF and HTTP. Unlike long polling, which closes and reopens the connection upon receiving a message, streaming keep the connection open at all times. Streaming suffers from the same thread blocking issue as long polling. A cap must be set so the server is not hang by idle threads.</li>
</ol>
<p>The reason why people are confused is that Adobe doesn't release its proprietary push solution <a href="http://en.wikipedia.org/wiki/Real_Time_Messaging_Protocol">RTMP</a> to BlazeDS. So, RTMP isn't available as a channel in the BlazeDS configuration files. BlazeDS lives in a Servlet container and hence constrained by <strong>one-thread-per-connection</strong> limit whereas <strong>LCDS </strong>has NIO-based channels that can scale up to 1000s of requests. On the other hand, BlazeDS has the advantage that it'll work over port 80/443, whereas LCDS will use some port for persistent connections that would require a firewall configuration. Once the servlet that implements BlazeDS is revved to support Comet Events under Tomcat 6, and then <strong>Jetty Continuations</strong>, then the long polling technique will be fine.</p>
<p><em>UPDATE: We are waiting for a solution that supports Comet Events under Tomcat 6. Then BlazeDS can be coupled to the Tomcat NIO HTTP listener and be able to scale as well as any NIO based server software.</em></p>
<p>I have learnt from this <a href="http://www.flexlive.net/?p=102">article </a>that you can create a <strong>channel set </strong>in client side. So Flex can fail-over to other channels until it gets connected or the list is exhausted.</p>
<p>Marc has put an effort to build a better data grid like a spreadsheet in Flex. (check <a href="http://rockonflash.wordpress.com/2007/11/26/flex-datagrid-replacement/">this </a>out)</p>
<h2>Reference</h2>
<p>Here are the references I used for this article</p>
<ol>
    <li><a href="http://jha.rajeev.googlepages.com/web2push">Tuning Apache and Tomcat for Web 2.0 comet application</a></li>
    <li><a title="Performance of Grids for Streaming Data" rel="bookmark" href="http://cometdaily.com/2007/12/21/performance-of-grids-for-streaming-data/">Performance of Grids for Streaming Data</a> - <em>This shows you the performance numbers on various frontend technologies. Again, Flex shows us a good result.</em></li>
    <li><a href="http://cometdaily.com/2008/11/21/are-raining-comets-and-threads/">Are raining comets and threads? - Comet Daily</a></li>
    <li><a href="http://iobound.com/2008/11/comet-nio/">Comet &#38; Java: Threaded Vs Nonblocking IO </a></li>
    <li><a href="http://pero.blogs.aprilmayjune.org/2009/01/22/hadoop-and-linux-kernel-2627-epoll-limits/">JDK 1.6 uses epoll to implement NIO</a></li>
    <li><a href="http://www.scribd.com/doc/2742051/blazeds-devguide">BlazeDS dev guide</a></li>
    <li><a href="http://www.yachtchartersmagazine.com/node/720304">Achieve performance breakthrough using BlazeDS</a> - <em>Farata System put an effort to write its NIO channel that runs on Jetty 7 and receive promising result.</em></li>
</ol>
<p>&#160;</p>]]></description>
			<content:encoded><![CDATA[<h2>Push data to client</h2>
<p>Traditional web application is based on <strong>request and response model</strong> that information is delivered as a single payload and then immediately close the connection to the client. To keep the client in sync, we normally <strong>pull </strong>the server periodically. This approach may generate unacceptable load to the server. To solve this problem, we want to have a <strong>push </strong>mechanism from server to client. This is why <strong>Comet </strong>is defined. Comet is a generic term describing various approaches to send data asynchronously from a Web server to a client without the need for the client to explicitly request the data. It is an essential technique for any real-time event-driven web applications, where the majority of events occur on the server and data must be &#8220;pushed&#8221; frequently to the client. To achieve this, Comet servers must maintain <strong>a continuous connection</strong> to each client for the duration of the session.</p>
<div style="page-break-after: always;"><span style="display: none;">&#160;</span></div>
<p>OK. How to maintain <strong>a continuous connection</strong> to each client for the duration of the session?</p>
<blockquote>
<p>If you try to adapt traditional server to the Comet methodology, it may not scale and often fails after a few thousand simultaneously open connections. A true Comet implementation requires a very different kind of server architecture to be efficient and scalable &#8211; <a href="http://www.freeliberator.com/comet/">Liberator</a> <em>(a solid Comet server that are used by the financial industries. However, it is written in C and not open source although it has FREE edition distributed). </em></p>
</blockquote>
<p>To understand this statement a little bit more, we need to know how traditional web containers handle the request. They are under <strong>one request per thread </strong>model.</p>
<ol>
<li>The client , typically , a browser sends request for resource to a web server.</li>
<li>The server has <strong>a listening thread</strong> that keeps track of incoming connections.</li>
<li>When a request arrives , the server uses one process or <strong>thread</strong> to process the request.</li>
<li>The resource is returned to the client and the connection is closed.</li>
</ol>
<p>In this model, the number of requests that can be served in a second would depend on two things</p>
<ol>
<li>How many threads are there to handle the client requests</li>
<li>How long it takes to serve one request.</li>
</ol>
<p>If all threads of server are busy, then the incoming requests are <strong>put in a queue</strong>. The server would return to the requests in queue when server threads become free. The number of requests handled per second is always greater than the number of allowed simultaneous connections. All this is made possible because the time required to process a request is very short. In other words you can server more requests in a second than you have threads.</p>
<p>However, there are one breed of applications that need to <strong>hold onto the connections</strong>. Think of applications that require real time data coming to clients (stock tickers)&#160; or think of applications where low-latency is required. In the above traditional web model, the browser has to re-connect to get the new data. (Polling). If the new updates &#8220;can&#8221;&#160; happen with high frequency (e.g. a chat application) then the polling frequency also has to increase .&#160; An alternative to high frequency polling is to use <strong>push based applications</strong>. For push based application, once the browser connects to server, the server will maintain the connection till the browser time-out (<strong>server response stream is not closed</strong>) and keeps flushing data down the connection as and when they become available. In servlet container, to hold the connection, your thread in the <strong>service </strong>method cannot exit the method. Otherwise, the response stream will be closed. So what you do is, you block the thread on some condition within the service method. So the thread will block for your condition. When push data becomes available , this thread writes to response stream and again enters a blocked state. So as long as you hold onto the connection, you can not return this thread to the thread pool. And as more and more &#8220;push&#8221; connections are established you would run out of threads! To remedy the problem, the possible solutions are:</p>
<ol>
<li>Increase # of server threads.</li>
</ol>
<h2>Flex Push</h2>
<p>There is confusion that whether <strong>BlazeDS</strong> supports real time messaging. Yes it does <img onclick="grin(':wink:');" alt=":wink:" src="../../../../../wp-includes/images/smilies/icon_wink.gif" />. In fact, BlazeDS has a full spectrum of channel types ranging from <strong>simple polling</strong>, to <strong>near-real-time polling,</strong> to <strong>real-time streaming</strong>.</p>
<ol>
<li><strong>Simple polling</strong> &#8211; ping the server from Flex client using the traditional request and response model</li>
<li><strong>Near-real-time polling</strong> (long polling) &#8211; Instead of acknowledging right away, the server could hold the polling request until there’s a message for the client. This ensure the messages are delivered to the client as soon as they become available. The caveat for using long-polling is the <strong>thread limitation</strong> in most application servers. At this moment, BlazeDS could not support more than a few hundred long-polling clients on most application servers. However, this problem could be resolved once servers like Tomcat start to support asynchronos, non-blocking connection threads. <span style="color: rgb(255, 0, 0);">Update: Now Tomcat 6 supports NIO.</span></li>
<li><strong>Real-time streaming</strong> &#8211; BlazeDS supports real-time message streaming over AMF and HTTP. Unlike long polling, which closes and reopens the connection upon receiving a message, streaming keep the connection open at all times. Streaming suffers from the same thread blocking issue as long polling. A cap must be set so the server is not hang by idle threads.</li>
</ol>
<p>The reason why people are confused is that Adobe doesn&#8217;t release its proprietary push solution <a href="http://en.wikipedia.org/wiki/Real_Time_Messaging_Protocol">RTMP</a> to BlazeDS. So, RTMP isn&#8217;t available as a channel in the BlazeDS configuration files. BlazeDS lives in a Servlet container and hence constrained by <strong>one-thread-per-connection</strong> limit whereas <strong>LCDS </strong>has NIO-based channels that can scale up to 1000s of requests. On the other hand, BlazeDS has the advantage that it&#8217;ll work over port 80/443, whereas LCDS will use some port for persistent connections that would require a firewall configuration. Once the servlet that implements BlazeDS is revved to support Comet Events under Tomcat 6, and then <strong>Jetty Continuations</strong>, then the long polling technique will be fine.</p>
<p><em>UPDATE: We are waiting for a solution that supports Comet Events under Tomcat 6. Then BlazeDS can be coupled to the Tomcat NIO HTTP listener and be able to scale as well as any NIO based server software.</em></p>
<p>I have learnt from this <a href="http://www.flexlive.net/?p=102">article </a>that you can create a <strong>channel set </strong>in client side. So Flex can fail-over to other channels until it gets connected or the list is exhausted.</p>
<p>Marc has put an effort to build a better data grid like a spreadsheet in Flex. (check <a href="http://rockonflash.wordpress.com/2007/11/26/flex-datagrid-replacement/">this </a>out)</p>
<h2>Reference</h2>
<p>Here are the references I used for this article</p>
<ol>
<li><a href="http://jha.rajeev.googlepages.com/web2push">Tuning Apache and Tomcat for Web 2.0 comet application</a></li>
<li><a title="Performance of Grids for Streaming Data" rel="bookmark" href="http://cometdaily.com/2007/12/21/performance-of-grids-for-streaming-data/">Performance of Grids for Streaming Data</a> &#8211; <em>This shows you the performance numbers on various frontend technologies. Again, Flex shows us a good result.</em></li>
<li><a href="http://cometdaily.com/2008/11/21/are-raining-comets-and-threads/">Are raining comets and threads? &#8211; Comet Daily</a></li>
<li><a href="http://iobound.com/2008/11/comet-nio/">Comet &amp; Java: Threaded Vs Nonblocking IO </a></li>
<li><a href="http://pero.blogs.aprilmayjune.org/2009/01/22/hadoop-and-linux-kernel-2627-epoll-limits/">JDK 1.6 uses epoll to implement NIO</a></li>
<li><a href="http://www.scribd.com/doc/2742051/blazeds-devguide">BlazeDS dev guide</a></li>
<li><a href="http://www.yachtchartersmagazine.com/node/720304">Achieve performance breakthrough using BlazeDS</a> &#8211; <em>Farata System put an effort to write its NIO channel that runs on Jetty 7 and receive promising result.</em></li>
</ol>
<p>&#160;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/uncategorized/streaming-data-to-your-grid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tomcat Performance Tuning</title>
		<link>http://www.solutionhacker.com/implement-your-idea/scale-your-website/tomcat-performance-tuning/</link>
		<comments>http://www.solutionhacker.com/implement-your-idea/scale-your-website/tomcat-performance-tuning/#comments</comments>
		<pubDate>Thu, 29 May 2008 20:15:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scale]]></category>
		<category><![CDATA[System]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[connection pool]]></category>
		<category><![CDATA[garbage collection]]></category>
		<category><![CDATA[HotSpot]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JVM]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[system]]></category>
		<category><![CDATA[Tomcat]]></category>
		<category><![CDATA[tuning]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=147</guid>
		<description><![CDATA[<p>Most companies I have worked for use Tomcat as Servlet Container. It is de facto standard just like how Apache been used as Web Server. However, most of us just drag our war file to the webapp folder and use Tomcat with all the settings as default out of the box. It works fine in development environment but may not in production. This article will give you advice in several areas:</p>
<ol>
    <li>Production Tomcat Architecture</li>
    <li>Tuning tomcat for performance</li>
    <li>Resolving problems which affect availability</li>
</ol>
<!--more-->
<h2>&#160;<!--more-->Production Tomcat Architecture</h2>
<p>In production Tomcat relies on a number of resources which can impact its overall performance. Understanding the overall system architecture is key to tuning performance and troubleshooting problems.</p>
<p style="text-align: left;"><img height="220" width="471" alt="" src="http://www.solutionhacker.com/wp-content/uploads/tomcatSystemArch(1).jpg" /></p>
<ol>
    <li>Hardware: CPU(s), memory, network IO and file IO</li>
    <li>OS: SMP (symmetric multiprocessing) and thread support</li>
    <li>JVM: version, tuning memory usage, and tuning GC</li>
    <li>Tomcat: version (example, Tomcat 6 supports NIO)</li>
    <li>Application: Application design can have the largest impact on overall performance</li>
    <li>Database: concurrent db connection is allowed (pooling and object caching)</li>
    <li>Web Server: Apache can sit in front of Tomcat and serves the static content. It also can do load balancing across multiple Tomcat instances.</li>
    <li>Network: Network delays.</li>
    <li>Remote Client: How fast is the communication protocol? Content can be compressed.&#160;</li>
</ol>
<h2>Performance Tuning</h2>
<p><u>How to measure and test performance</u></p>
<ul>
    <li><strong>Request latency</strong> is key b/c it reflects the responsiveness of your site for visitors.</li>
    <li>Test environment should match production as closely as possible.</li>
    <li>The <strong>data volume </strong>is important to simulate in database side.</li>
    <li>Test HTTP requests with different request parameters (test corner cases)</li>
    <li>Use <strong>load test</strong> to simulate the traffics (ex. JMeter)</li>
    <li>Final tests should be <strong>over longer periods like days</strong> because JVM performance changes over time and can actually improve if using HotSpot. Memory leaks, db temporary unavailable, etc can only be found when running longer tests.</li>
</ul>
<p><u>JVM version, memory usage and GC</u></p>
<ul>
    <li>Sun Java 1.3 and later releases inlcude <strong>HotSpot </strong>profiling optimizer customized for long running server application.</li>
    <li><img src="file:///C:/WINDOWS/TEMP/moz-screenshot-1.jpg" alt="" />Tomcat will freeze processing of all requests while the JVM is performing <a target="_blank" href="http://www.javaworld.com/javaworld/jw-01-2002/jw-0111-hotspotgc.html">GC</a>. On a poorly tuned JVM this can last 10's of seconds. Most GC's should take &#60; 1 second and never exceed 10 seconds</li>
    <li>Tune the -Xms (min) and -Xmx (max) java<strong> stack memory </strong>(set them to the same value can improve GC performance)</li>
    <li>Make sure the java process always keeps the memory it uses resident in physical memory and not swapped out to virtual memory.</li>
    <li>Use -Xincgc to enable<strong> incremental garbage collection</strong></li>
    <li>Try reducing -Xss thread stack memory usage</li>
</ul>
<p><u>Tomcat version and configuration</u></p>
<ul>
    <li>Tomcat 6 supports NIO.</li>
    <li>Set "reloadable" false - remove unnecessary detection overhead</li>
    <li>Set "liveDeploy" to false - liveDeploy controls whether your webapps directory is periodically checked for new war files. This is done using background thread.</li>
    <li>Set "debug" to 0</li>
    <li>Set "swallowOutput" to true - This makes sure all output to stdout or stderr for a web application gets directed to the web application log rather than the console or catalina.out. This make it easier to troubleshoot problems.</li>
    <li>Connector configuration - minProcessor, maxProcessor, acceptCount, enableLookups. Don't set the acceptCount too high b/c this sets the number of pending requests awaiting processing. It is better deny few requests than overload Tomcat and cause problems for all requests. Set "enableLookups" to false b/c DNS lookups can add significant delays.</li>
</ul>
<p><u>Database connection pool</u></p>
<ul>
    <li>We use connection pool provided by Spring instead</li>
    <li>Using middleware to persist and cache objects from your database can significantly improve performance b/c of fewer db calls, less thrashing of the JVM for creation and subsequent GC of object craeted for resultset.</li>
</ul>
<p><u>Application design and profiling</u></p>
<ul>
    <li>If the data used to generate a dynamic page rarely changes, modify it to a static page which you regenerate periodically.</li>
    <li>Cache dynamic page</li>
    <li>Use tool like JProble to profle your web applications during development phase</li>
    <li>Look for possible thread synchronization bottlenecks</li>
    <li>Date and Time thread synchronization bottleneck&#160;</li>
</ul>
<h2>Troubleshooting</h2>
<p><u>Collecting and analyzing log data</u></p>
<p><u>Common problems</u></p>
<ul>
    <li><strong>Broken pipe</strong> - For HTTP Connector indicates that the remote client aborted the request. For web server JK Connector indicates that the web server process or thread was terminated. These are normal and rarely due to a problem with Tomcat. However, if you have long request, the connectionTimeout may close the connection before you send your response back.</li>
    <li><strong>Tomcat freezes or pauses </strong>with no request being processed - usually due to a long pause of JVM GC. A long pause can cause a cascading effect and high load once Tomcat starts handling requests again. Don't set the "acceptCount" too high and use java -verbose:gc startup argument to collect GC data.</li>
    <li><strong>Out of Memory Exception</strong> - look into application code to fix the leak (profile tool can help). Increase available memory on the system via -Xmx. Restart tomcat!</li>
    <li>Database connection failure - connection used up when traffic is spike.</li>
    <li><strong>Random connection close exception </strong>- when you close your connection twice. First close(), the connection returns to the pool. It may be picked up by another thread. Now, second close() may close a connection that is being used by other thread. Don't close connection twice, use JDBC Template from Spring to avoid this problem.&#160;</li>
</ul>
<h2>Reference</h2>
<ol>
    <li><a target="_blank" href="http://www.javaworld.com/javaworld/jw-01-2002/jw-0111-hotspotgc.html">JavaWorld GC Article</a></li>
    <li><a target="_blank" href="http://java.sun.com/docs/hotspot/index.html">Sun HotSpot Performance Document</a></li>
    <li><a target="_blank" href="http://uuu.teetzen.net/uniweb/tomcat_performance.pdf">Tomcat Performance Slides</a></li>
</ol>
<p>&#160;&#160;</p>]]></description>
			<content:encoded><![CDATA[<p>Most companies I have worked for use Tomcat as Servlet Container. It is de facto standard just like how Apache been used as Web Server. However, most of us just drag our war file to the webapp folder and use Tomcat with all the settings as default out of the box. It works fine in development environment but may not in production. This article will give you advice in several areas:</p>
<ol>
<li>Production Tomcat Architecture</li>
<li>Tuning tomcat for performance</li>
<li>Resolving problems which affect availability</li>
</ol>
<p><span id="more-147"></span></p>
<h2>&#160;<!--more-->Production Tomcat Architecture</h2>
<p>In production Tomcat relies on a number of resources which can impact its overall performance. Understanding the overall system architecture is key to tuning performance and troubleshooting problems.</p>
<p style="text-align: left;"><img height="220" width="471" alt="" src="http://www.solutionhacker.com/wp-content/uploads/tomcatSystemArch(1).jpg" /></p>
<ol>
<li>Hardware: CPU(s), memory, network IO and file IO</li>
<li>OS: SMP (symmetric multiprocessing) and thread support</li>
<li>JVM: version, tuning memory usage, and tuning GC</li>
<li>Tomcat: version (example, Tomcat 6 supports NIO)</li>
<li>Application: Application design can have the largest impact on overall performance</li>
<li>Database: concurrent db connection is allowed (pooling and object caching)</li>
<li>Web Server: Apache can sit in front of Tomcat and serves the static content. It also can do load balancing across multiple Tomcat instances.</li>
<li>Network: Network delays.</li>
<li>Remote Client: How fast is the communication protocol? Content can be compressed.&#160;</li>
</ol>
<h2>Performance Tuning</h2>
<p><u>How to measure and test performance</u></p>
<ul>
<li><strong>Request latency</strong> is key b/c it reflects the responsiveness of your site for visitors.</li>
<li>Test environment should match production as closely as possible.</li>
<li>The <strong>data volume </strong>is important to simulate in database side.</li>
<li>Test HTTP requests with different request parameters (test corner cases)</li>
<li>Use <strong>load test</strong> to simulate the traffics (ex. JMeter)</li>
<li>Final tests should be <strong>over longer periods like days</strong> because JVM performance changes over time and can actually improve if using HotSpot. Memory leaks, db temporary unavailable, etc can only be found when running longer tests.</li>
</ul>
<p><u>JVM version, memory usage and GC</u></p>
<ul>
<li>Sun Java 1.3 and later releases inlcude <strong>HotSpot </strong>profiling optimizer customized for long running server application.</li>
<li><img src="file:///C:/WINDOWS/TEMP/moz-screenshot-1.jpg" alt="" />Tomcat will freeze processing of all requests while the JVM is performing <a target="_blank" href="http://www.javaworld.com/javaworld/jw-01-2002/jw-0111-hotspotgc.html">GC</a>. On a poorly tuned JVM this can last 10&#8242;s of seconds. Most GC&#8217;s should take &lt; 1 second and never exceed 10 seconds</li>
<li>Tune the -Xms (min) and -Xmx (max) java<strong> stack memory </strong>(set them to the same value can improve GC performance)</li>
<li>Make sure the java process always keeps the memory it uses resident in physical memory and not swapped out to virtual memory.</li>
<li>Use -Xincgc to enable<strong> incremental garbage collection</strong></li>
<li>Try reducing -Xss thread stack memory usage</li>
</ul>
<p><u>Tomcat version and configuration</u></p>
<ul>
<li>Tomcat 6 supports NIO.</li>
<li>Set &#8220;reloadable&#8221; false &#8211; remove unnecessary detection overhead</li>
<li>Set &#8220;liveDeploy&#8221; to false &#8211; liveDeploy controls whether your webapps directory is periodically checked for new war files. This is done using background thread.</li>
<li>Set &#8220;debug&#8221; to 0</li>
<li>Set &#8220;swallowOutput&#8221; to true &#8211; This makes sure all output to stdout or stderr for a web application gets directed to the web application log rather than the console or catalina.out. This make it easier to troubleshoot problems.</li>
<li>Connector configuration &#8211; minProcessor, maxProcessor, acceptCount, enableLookups. Don&#8217;t set the acceptCount too high b/c this sets the number of pending requests awaiting processing. It is better deny few requests than overload Tomcat and cause problems for all requests. Set &#8220;enableLookups&#8221; to false b/c DNS lookups can add significant delays.</li>
</ul>
<p><u>Database connection pool</u></p>
<ul>
<li>We use connection pool provided by Spring instead</li>
<li>Using middleware to persist and cache objects from your database can significantly improve performance b/c of fewer db calls, less thrashing of the JVM for creation and subsequent GC of object craeted for resultset.</li>
</ul>
<p><u>Application design and profiling</u></p>
<ul>
<li>If the data used to generate a dynamic page rarely changes, modify it to a static page which you regenerate periodically.</li>
<li>Cache dynamic page</li>
<li>Use tool like JProble to profle your web applications during development phase</li>
<li>Look for possible thread synchronization bottlenecks</li>
<li>Date and Time thread synchronization bottleneck&#160;</li>
</ul>
<h2>Troubleshooting</h2>
<p><u>Collecting and analyzing log data</u></p>
<p><u>Common problems</u></p>
<ul>
<li><strong>Broken pipe</strong> &#8211; For HTTP Connector indicates that the remote client aborted the request. For web server JK Connector indicates that the web server process or thread was terminated. These are normal and rarely due to a problem with Tomcat. However, if you have long request, the connectionTimeout may close the connection before you send your response back.</li>
<li><strong>Tomcat freezes or pauses </strong>with no request being processed &#8211; usually due to a long pause of JVM GC. A long pause can cause a cascading effect and high load once Tomcat starts handling requests again. Don&#8217;t set the &#8220;acceptCount&#8221; too high and use java -verbose:gc startup argument to collect GC data.</li>
<li><strong>Out of Memory Exception</strong> &#8211; look into application code to fix the leak (profile tool can help). Increase available memory on the system via -Xmx. Restart tomcat!</li>
<li>Database connection failure &#8211; connection used up when traffic is spike.</li>
<li><strong>Random connection close exception </strong>- when you close your connection twice. First close(), the connection returns to the pool. It may be picked up by another thread. Now, second close() may close a connection that is being used by other thread. Don&#8217;t close connection twice, use JDBC Template from Spring to avoid this problem.&#160;</li>
</ul>
<h2>Reference</h2>
<ol>
<li><a target="_blank" href="http://www.javaworld.com/javaworld/jw-01-2002/jw-0111-hotspotgc.html">JavaWorld GC Article</a></li>
<li><a target="_blank" href="http://java.sun.com/docs/hotspot/index.html">Sun HotSpot Performance Document</a></li>
<li><a target="_blank" href="http://uuu.teetzen.net/uniweb/tomcat_performance.pdf">Tomcat Performance Slides</a></li>
</ol>
<p>&#160;&#160;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/implement-your-idea/scale-your-website/tomcat-performance-tuning/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

