<?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>Alfa Jango Blog &#187; performance tuning</title>
	<atom:link href="http://www.alfajango.com/blog/tag/performance-tuning/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alfajango.com/blog</link>
	<description>Engineering, Software, and Entrepreneurship</description>
	<lastBuildDate>Fri, 30 Jul 2010 13:47:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Performance Tuning for Phusion Passenger (an Introduction)</title>
		<link>http://www.alfajango.com/blog/performance-tuning-for-phusion-passenger-an-introduction/</link>
		<comments>http://www.alfajango.com/blog/performance-tuning-for-phusion-passenger-an-introduction/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 13:54:07 +0000</pubDate>
		<dc:creator>steve</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[mod_rails]]></category>
		<category><![CDATA[passenger]]></category>
		<category><![CDATA[performance tuning]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[swap]]></category>

		<guid isPermaLink="false">http://www.alfajango.com/blog/?p=142</guid>
		<description><![CDATA[Once you've launched your application and people start actually using it, you may find server experiencing excessive swapping once the traffic begins to pick up. Before getting too deep into scaling, caching, upgrading your server, etc., there are a few Passenger settings you can tune to wring out the best performance your existing stack. I'll address these settings with a few rules of thumb we've gathered from the Passenger documentation and simple trial-and-error.]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://www.alfajango.com/blog/wp-content/uploads/2010/03/Picture-10.png"><img class="size-medium wp-image-151 alignright" title="Picture 10" src="http://www.alfajango.com/blog/wp-content/uploads/2010/03/Picture-10-237x300.png" alt="" width="237" height="300" /></a><a href="http://www.modrails.com/">Phusion Passenger</a> (aka mod_rails) allows for easy and scalable deployment of Ruby on Rails applications on Apache or Nginx servers. Part of what makes it so easy is that it comes with suitable default settings right out of the box, so that you don&#8217;t need to concern yourself with any of the details when deploying your application to production.
</p><p>
However, once you&#8217;ve launched your application and people start actually using it, you may find server experiencing excessive swapping once the traffic begins to pick up. Before getting too deep into scaling, caching, upgrading your server, etc., there are a few Passenger settings you can tune to wring out the best performance your existing stack. I&#8217;ll address these settings with a few rules of thumb we&#8217;ve gathered from the <a href="http://www.modrails.com/documentation.html">Passenger documentation</a> and simple trial-and-error.
</p><p>
Before getting started, we highly recommend employing Munin (<a href="http://www.alfajango.com/blog/how-to-monitor-your-railspassenger-app-with-munin/">instructions for Munin with Passenger here</a>) and NewRelic Performance Monitoring for your Passenger/Rails application, so that you can fine-tune your settings for the best performance.
</p><p>
<em>Note: the following settings are specific for Apache, you will need to modify the syntax/file-names for Nginx.</em>
</p>
<h2>Per-server Settings</h2>
<p>
<span style="font-weight: normal;">These settings are set per-server, meaning if you have multiple applications running on the server, these should be set only once throughout all of your <code class="codecolorer text default"><span class="text">.conf</span></code> files. If you have only one Rails/Passenger app on the server, then you may set these in that application&#8217;s <code class="codecolorer text default"><span class="text">.conf</span></code> file. On Ubuntu, this may be found at <code class="codecolorer text default"><span class="text">/etc/apache2/sites-available/yourapp.conf</span></code>. </span>
</p>
<span id="more-142"></span>
<p>
A more appropriate place for these however would be in Apache&#8217;s <code class="codecolorer text default"><span class="text">.conf</span></code> file.
</p>
<h3>PassengerMaxPoolSize and PassengerPoolIdleTime</h3>
<p>
<code class="codecolorer text default"><span class="text">/etc/apache2/apache2.conf</span></code>:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #adadad; font-style: italic;"># PassengerMaxPoolSize</span><br />
<span style="color: #adadad; font-style: italic;"># Default: 6</span><br />
<span style="color: #adadad; font-style: italic;"># For 2gb RAM: 30</span><br />
<span style="color: #adadad; font-style: italic;"># For 256 slice with MySQL running: 2</span><br />
PassengerMaxPoolSize <span style="color: #ff0000;">2</span><br />
<br />
<span style="color: #adadad; font-style: italic;"># PassengerPoolIdleTime</span><br />
<span style="color: #adadad; font-style: italic;"># Recommended to be avg time per page * 2</span><br />
<span style="color: #adadad; font-style: italic;"># In Google Analytics... (Avg time on site / Avg page views) * 2</span><br />
<span style="color: #adadad; font-style: italic;"># Default: 300</span><br />
PassengerPoolIdleTime <span style="color: #ff0000;">150</span></div></div>

</p><p>
You can essentially copy and paste this into your apache conf file. I&#8217;ve added some notes in there to help you remember how they should be tuned.</p>
<p>
<a href="http://modrails.com/documentation/Users%20guide%20Apache.html#_passengermaxpoolsize_lt_integer_gt"><code class="codecolorer apache default"><span class="apache">PassengerMaxPoolSize</span></code></a>: This is the number of Passenger instances that can be spawned at once for your site. This basically relates directly to the amount of RAM that is available to your application. However, it is not a linear relationship. It&#8217;s more of an exponential relationship. Numbers that we and others have found to work well are:</p>
<table>
<tbody>
<tr>
<th>RAM (MB)</th>
<th>PassengerMaxPoolSize</th>
</tr>
<tr>
<td>256</td>
<td>2</td>
</tr>
<tr>
<td>512</td>
<td>6 (default)</td>
</tr>
<tr>
<td>1024 (1GB)</td>
<td>15</td>
</tr>
<tr>
<td>2048 (2GB)</td>
<td>30</td>
</tr>
</tbody>
</table>
<p>
Since this depends on available RAM, this will vary depending on what other processes are running on your server, such as MySQL, etc. If you have Munin installed with the Passenger plugins, you can monitor the Passenger Memory Stats and Passenger Status graphs to tweak this as necessary.
</p><p>
<a href="http://modrails.com/documentation/Users%20guide%20Apache.html#PassengerPoolIdleTime"><code class="codecolorer apache default"><span class="apache">PassengerPoolIdleTime</span></code></a>: This is essentially the amount of time (in seconds) that each Passenger instance continues to run after it becomes idle (i.e. someone stops clicking around the site). According to the documentation, this should be set to the average time a visitor views a page multiplied by 2. This of course prevents Passenger instances from shutting down (and thus having to re-spawn) while someone is viewing a page.
</p><p>
If your application has been in production long enough to have a few hundred viewers and you have Google Analytics installed, an easy way to get the ideal setting for your application is to take the Average Time on Site, divide by the Average Page Views per Visit, and multiply by 2.
</p><p>

<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">( Average Time on Site / Average Page Views per Visit ) x 2</div></div>

</p>
<h3>PassengerMaxInstancesPerApp (optional)</h3>
<p>
If you do have more than one Rails/Passenger application on the server, you may optionally set <code class="codecolorer text default"><span class="text">PassengerMaxInstancesPerApp</span></code>.
</p><p>
<a href="http://modrails.com/documentation/Users%20guide%20Apache.html#_passengermaxinstancesperapp_lt_integer_gt"><code class="codecolorer apache default"><span class="apache">PassengerMaxInstancesPerApp</span></code></a>: This is the maximum number of instances each application may use of the total <code class="codecolorer text default"><span class="text">PassengerMaxPoolSize</span></code>. Therefore, <code class="codecolorer text default"><span class="text">PassengerMaxInstancesPerApp</span></code> must be smaller than <code class="codecolorer text default"><span class="text">PassengerMaxPoolSize</span></code>.
</p><p>
For example, if you have 2 applications running on one 256MB server, you may want to set <code class="codecolorer text default"><span class="text">PassengerMaxInstancesPerApp</span></code> to 1, so that one app may never hog all of the rails instances at the expense of the other app. If you have 2 applications running on a 512MB server (<code class="codecolorer text default"><span class="text">PassengerMaxPoolSize</span></code> 6), then you may want to set cci]PassengerMaxInstancesPerApp[/cci] to 4 or 5 to allow each app to better handle higher load (hopefully while the other app is under less load).
</p>
<h3>RailsAutoDetect</h3>
<p>
To get a little more performance out of your server, you can also turn the <a href="http://modrails.com/documentation/Users%20guide%20Apache.html#_railsautodetect_lt_on_off_gt"><code class="codecolorer apache default"><span class="apache">RailsAutoDetect</span></code></a> setting of Passenger off. This will make it so that each request through Apache does not check to see if it is a Rails request that requires Passenger. However, this will now require that you explicitly tell Apache which requests are Rails and do require Passenger, using <a href="http://modrails.com/documentation/Users%20guide%20Apache.html#RailsBaseURI"><code class="codecolorer apache default"><span class="apache">RailsBaseURI</span></code></a>. This is easy though.
</p><p>
Again, in your <code class="codecolorer text default"><span class="text">apache2.conf</span></code> file:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #adadad; font-style: italic;"># Must be used with RailsBaseURI in Vhost</span><br />
RailsAutoDetect <span style="color: #0000ff;">off</span></div></div>

</p><p>
Now, within your the Vhost block in your site&#8217;s <code class="codecolorer text default"><span class="text">.conf</span></code> file, you will need to set the <code class="codecolorer text default"><span class="text">RailsBaseURI</span></code>:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:80&gt;<br />
&nbsp; ...<br />
&nbsp; RailsBaseURI /<br />
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</div></div>

</p><p>
Here is a complete example of the Passenger block in your <code class="codecolorer text default"><span class="text">apache2.conf</span></code> file:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #adadad; font-style: italic;"># PassengerMaxPoolSize</span><br />
<span style="color: #adadad; font-style: italic;"># Default: 6</span><br />
<span style="color: #adadad; font-style: italic;"># For 2gb RAM: 30</span><br />
<span style="color: #adadad; font-style: italic;"># For 256 slice with MySQL running: 2</span><br />
PassengerMaxPoolSize <span style="color: #ff0000;">2</span><br />
<br />
<span style="color: #adadad; font-style: italic;"># PassengerPoolIdleTime</span><br />
<span style="color: #adadad; font-style: italic;"># Recommended to be avg time per page * 2</span><br />
<span style="color: #adadad; font-style: italic;"># In Google Analytics... (Avg time on site / Avg page views) * 2</span><br />
<span style="color: #adadad; font-style: italic;"># Default: 300</span><br />
PassengerPoolIdleTime <span style="color: #ff0000;">144</span><br />
<br />
<span style="color: #adadad; font-style: italic;"># PassengerMaxInstancesPerApp &lt; PassengerMaxPoolSize </span><br />
PassengerMaxInstancesPerApp <span style="color: #ff0000;">1</span><br />
<br />
<span style="color: #adadad; font-style: italic;"># Must be used with RailsBaseURI in Vhost</span><br />
RailsAutoDetect <span style="color: #0000ff;">off</span></div></div>

</p></p><p>
And a simple <code class="codecolorer text default"><span class="text">sites-available/yourapp.conf</span></code> may look like this:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:80&gt;<br />
&nbsp; <span style="color: #00007f;">ServerName</span> www.yourapp.com<br />
&nbsp; <span style="color: #00007f;">DocumentRoot</span> /home/deploy/yourapp/current/public<br />
&nbsp; &lt;<span style="color: #000000; font-weight:bold;">Directory</span> <span style="color: #7f007f;">&quot;/home/deploy/yourapp/current/public&quot;</span>&gt;<br />
&nbsp; &nbsp; <span style="color: #00007f;">Options</span> <span style="color: #0000ff;">FollowSymLinks</span><br />
&nbsp; &nbsp; <span style="color: #00007f;">AllowOverride</span> <span style="color: #0000ff;">None</span><br />
&nbsp; &nbsp; <span style="color: #00007f;">Order</span> <span style="color: #00007f;">allow</span>,<span style="color: #00007f;">deny</span><br />
&nbsp; &nbsp; <span style="color: #00007f;">Allow</span> from <span style="color: #0000ff;">all</span><br />
&nbsp; &lt;/<span style="color: #000000; font-weight:bold;">Directory</span>&gt;<br />
&nbsp; RailsBaseURI /<br />
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</div></div>

</p>
<h2>More Passenger Configuration Settings</h2>
<p>
These settings so far only scratch the surface of Passenger configuration. Give the <a href="http://modrails.com/documentation/Users%20guide%20Apache.html#_configuring_phusion_passenger">Passenger documentation</a> a good read-through for more information on configuring Passenger.
</p>
<h2>Further Reading: Apache Settings</h2>
<p>
At this point, if you still need to squeeze more performance out of your server (and you will eventually), you&#8217;ll want to configure Apache to utilize its resources most efficiently with Passenger. I recommend reading through <a href="http://forum.slicehost.com/comments.php?DiscussionID=1776">this thread on the Slicehost forums</a> to get a good idea just how much trial-and-error this will take. <strong>Keep a close eye on your server when you tweak these settings, you may end up making it slower several times before finding that sweet spot.</strong>
</p><p>
You may read the <a href="http://httpd.apache.org/docs/1.3/mod/core.html">documentation for each setting in the Apache docs</a> (note that these are the Apache 1.3 docs, but they still explain purpose of each setting well). For more information using these settings to reduce the RAM consumed by Apache, see <a href="http://forum.slicehost.com/comments.php?DiscussionID=3313">this other Slicehost forum</a> and <a href="http://programming-gone-awry.blogspot.com/2009/06/how-to-save-much-ram-when-running-rails.html">this blog post on saving RAM with Rails and Passenger</a>.
</p><p>
If you&#8217;re impatient and just want to get something better than default, here are some numbers that worked well for us on a recent application running on a 256MB VPS on Slicehost. These settings can be found in your <code class="codecolorer text default"><span class="text">apache2.conf</span></code> file.
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;height:300px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #adadad; font-style: italic;"># prefork MPM</span><br />
<span style="color: #adadad; font-style: italic;"># StartServers: number of server processes to start</span><br />
<span style="color: #adadad; font-style: italic;"># MinSpareServers: minimum number of server processes which are kept spare</span><br />
<span style="color: #adadad; font-style: italic;"># MaxSpareServers: maximum number of server processes which are kept spare</span><br />
<span style="color: #adadad; font-style: italic;"># MaxClients: maximum number of server processes allowed to start</span><br />
<span style="color: #adadad; font-style: italic;"># MaxRequestsPerChild: maximum number of requests a server process serves</span><br />
&lt;<span style="color: #000000; font-weight:bold;">IfModule</span> mpm_prefork_module&gt;<br />
&nbsp; <span style="color: #00007f;">StartServers</span>          2<br />
&nbsp; <span style="color: #00007f;">MinSpareServers</span>       1<br />
&nbsp; <span style="color: #00007f;">MaxSpareServers</span>       5<br />
&nbsp; <span style="color: #00007f;">MaxClients</span>           10<br />
&nbsp; <span style="color: #00007f;">MaxRequestsPerChild</span>   100<br />
&lt;/<span style="color: #000000; font-weight:bold;">IfModule</span>&gt;<br />
<span style="color: #adadad; font-style: italic;"># worker MPM</span><br />
<span style="color: #adadad; font-style: italic;"># StartServers: initial number of server processes to start</span><br />
<span style="color: #adadad; font-style: italic;"># MaxClients: maximum number of simultaneous client connections</span><br />
<span style="color: #adadad; font-style: italic;"># MinSpareThreads: minimum number of worker threads which are kept spare</span><br />
<span style="color: #adadad; font-style: italic;"># MaxSpareThreads: maximum number of worker threads which are kept spare</span><br />
<span style="color: #adadad; font-style: italic;"># ThreadsPerChild: constant number of worker threads in each server process</span><br />
<span style="color: #adadad; font-style: italic;"># MaxRequestsPerChild: maximum number of requests a server process serves</span><br />
&lt;<span style="color: #000000; font-weight:bold;">IfModule</span> mpm_worker_module&gt;<br />
&nbsp; <span style="color: #00007f;">StartServers</span>          2<br />
&nbsp; <span style="color: #00007f;">MaxClients</span>           10<br />
&nbsp; <span style="color: #00007f;">MinSpareThreads</span>      25<br />
&nbsp; <span style="color: #00007f;">MaxSpareThreads</span>      75<br />
&nbsp; <span style="color: #00007f;">ThreadsPerChild</span>      25<br />
&nbsp; <span style="color: #00007f;">MaxRequestsPerChild</span>   100<br />
&lt;/<span style="color: #000000; font-weight:bold;">IfModule</span>&gt;</div></div>

</p>]]></content:encoded>
			<wfw:commentRss>http://www.alfajango.com/blog/performance-tuning-for-phusion-passenger-an-introduction/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
