<?xml version="1.0" encoding="UTF-8"?><rss version="0.92">
<channel>
	<title>Common Sense Code</title>
	<link>http://www.commonsensecode.com</link>
	<description>Applying Common Sense to Development</description>
	<lastBuildDate>Sun, 04 Jul 2010 20:12:53 +0000</lastBuildDate>
	<docs>http://backend.userland.com/rss092</docs>
	<language>en</language>
	<!-- generator="WordPress/3.0.1" -->

	<item>
		<title>ActiveMQ Supporting Thousands of Concurrent Connections</title>
		<description><![CDATA[<p>On one of my projects, I&#8217;ve been using ActiveMQ to support an infrastructure of thousands of connections.  At the very least, this has been a real challenge, so I&#8217;m going to throw out some additional information that I haven&#8217;t found anywhere else.  First things first, though.  If you haven&#8217;t read <a href="http://www.pepperdust.org">Dave Stanley&#8217;s blog</a> about the <a href="http://http://www.pepperdust.org/?p=150">subject</a>, he has most of the practical details on how to configure ActiveMQ to support thousands of connections.</p>
<p>As Dave says in his blog, the good news is that it can handle thousands of connections.  However, the bad news is that there are clearly some tradeoffs.  To understand these, it&#8217;s important to understand what ActiveMQ does under the hood. </p>
<p>Here is an example to show how ActiveMQ works.  Say you are going to build out a system capable of handling 4000 concurrent connections.  For now, we won&#8217;t assume anything in relation to number of queues, transactions per second, etc.  (Don&#8217;t worry, we&#8217;ll get there).  In order to get the best performance from ActiveMQ, it&#8217;s best if the total processing threads for the JVM stays below 1500 threads.</p>
<p>The thread model for ActiveMQ can be tweaked in several ways, but I&#8217;m going to focus on the configuration in Dave&#8217;s blog.  The nio connector is extremely important.  The tcp and ssl connectors by default will create a single thread per connection.  The nio connector uses a considerably smaller thread pool, which reduces the thread counts roughly by a factor of 4-5. </p>
<blockquote>
<p><strong>Tradeoff:</strong>  You have to put encryption on the producer consumer, or if you can, have your servers fronted by a load balancer, preferably with SSL acceleration.  (I haven&#8217;t found much information about ActiveMQ, working behind a load balancer, but in my own tests, I&#8217;ve never had a problem with it).</p>
</blockquote>
<p>Back to our example.  We have 4000 connections, which means we should have approximately 800 &#8211; 1000 threads allocated to processing connection data.  This can go larger depending on load, but we&#8217;ll consider it there for the sake of argument. Considering we want to keep the thread count below 1500, we have roughly 500 left to work with.</p>
<p>The next thing to consider is how threads are used in ActiveMQ.  The details are <a href="http://http://fusesource.com/wiki/display/ProdInfo/Understanding+the+Threads+Allocated+in+ActiveMQ">here</a>. Suffice to say, if you make the recommended configuration changes, you&#8217;ll reduce the number of threads required to process messages.  The most important change you can make is to set the <span style="font-family:monospace">org.apache.activemq.UseDedicatedTaskRunner</span> variable to <span style="font-family:monospace">false.</span>The ActiveMQ documentation says this:</p>
<blockquote>
<p><span style="font-style: normal; text-align: auto; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); border-collapse: separate; font-weight: normal; line-height: normal; text-indent: 0px; font-variant: normal; white-space: normal; text-transform: none; word-spacing: 0px; widows: 2" class="Apple-style-span"><span style="line-height: 20px" class="Apple-style-span">ActiveMQ can optionally use internally a thread pool to control dispatching of messages &#8211; but as a lot of deployment operating systems are good at handling a large number of threads, this is off by default.</span></span></p>
</blockquote>
<p>Although, there are varying thoughts on the subject, I still believe that ActiveMQ should ship with this configuration.  The dedicated task runner basically tells ActiveMQ not to use thread pooling, and to create a single thread for every queue. </p>
<blockquote>
<p><strong>Tradeoff:</strong> You can&#8217;t use a large number of destinations.  In a connection heavy configuration, plan on having no more than 500 queues, preferably less than 200.</p>
</blockquote>
<p>The reasoning behind this is straightforward.  The more queues, the more time ActiveMQ spends on context switching, and the less time it spends on message delivery.  Back to our example, figure that 500 queues is approximately 250 threads under load.  Now we are down to 850 threads available.</p>
<blockquote>
<p><strong>Tradeoff:</strong> Broker networks will degrade your message performance dramatically.  If you can use another high availability strategy, then do so. If you have to use a broker network, then plan on having a sharded environment and only having broker network pairs.</p>
</blockquote>
<p>Broker networks do a number of interesting things.  First, broker networks use advisory topics to get notifications of when consumers are available so that they can determine which server to deliver messages to.  This means that for every queue created, an advisory topic is created and a consumer for that advisory topic.  Figure you just created another 250 threads for those messages if you are in a pair.  Another side effect of advisory topics is producer flow control. For standard queues and topics, producer flow control can be shut off, which is a very good thing, since it will create a flow control thread for every queue.  Unfortunately, producer flow control cannot be shut off on advisory topics.  That means while the delivery threads work in the same thread pool, flow control will automatically add another 500 threads to your thread count.  If you are keeping track that means we are already at 2000 threads.</p>
<p>This means you have to shard your environment and reduce connections.  If a network of brokers is your only option, then plan on sharding your environment into server pairs and plan on manually balancing connections on those pairs.  This makes for some interesting message delivery issues, but it will allow ActiveMQ to process your threads.  If you can keep a single machine down to 1000 connections, you will have sufficient headroom on your broker network pair to failover all 1000 connections to the second machine in case of a failure. </p>
<p>Best of luck with your configuration.</p>
]]></description>
		<link>http://www.commonsensecode.com/2010/07/02/activemq-supporting-thousands-of-concurrent-connections/</link>
			</item>
</channel>
</rss>
