<?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>webr3.org &#187; mysql</title>
	<atom:link href="http://webr3.org/blog/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://webr3.org/blog</link>
	<description>brain&#039;s on fire!</description>
	<lastBuildDate>Mon, 30 Aug 2010 00:11:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Speeding up Apache2 and MySQL5 the easy way.</title>
		<link>http://webr3.org/blog/optimization/speeding-up-apache2-and-mysql5-the-easy-way/</link>
		<comments>http://webr3.org/blog/optimization/speeding-up-apache2-and-mysql5-the-easy-way/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 22:28:14 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[apache]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[backend db server]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[HTTP persistent connection]]></category>
		<category><![CDATA[Hypertext Transfer Protocol]]></category>
		<category><![CDATA[Keepalive]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ram]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[Technology/Internet]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=85</guid>
		<description><![CDATA[One of the sites I manage is what I'd would call a high traffic website, traffic sits around 900 requests a second all day and peaks around 3500 a second in rush hour (when the new stories for the day are published each morning). Nearer 6000 per second at peak.
Skippable Details
All this traffic is round [...]]]></description>
			<content:encoded><![CDATA[<p>One of the sites I manage is what I'd would call a high traffic website, traffic sits around 900 requests a second all day and peaks around 3500 a second in rush hour (when the new stories for the day are published each morning). Nearer 6000 per second at peak.</p>
<h3>Skippable Details</h3>
<p>All this traffic is round robin'd to 2 apache2 servers (each is 2x 2.86 quad xeons w/ 4GB ram) and one backend db server of similar spec.</p>
<p>Over the years familiar problems rear there heads, the mysql "Too Many Connections" error, Apache running like a dog under stress etc.</p>
<p>This weekend I got the chance to rebuild the servers from scratch, and a chance to optimize everything - works very very well now!</p>
<h3>Easy Optimization - Apache2</h3>
<p>KeepAliveTimeout: Number of seconds to wait for the next request from the same client on the same connection.</p>
<p>Translate this to "Number of seconds to lock up a worker thread just incase another request comes in from the same client on the same connection" - in short a lot of browsers will request the page, keep the connection alive and use it to request images, css etc.</p>
<p>The default is 15 seconds; in my experience about 40-50% of connections will use KeepAlive, so if you have enough ram to support 500 requests before going in to swap memory, and you get 500 requests a second, by the time 3 seconds are past you've got a load of workers with RAM assigned doing nothing and all the hard work is getting done using swap memory instead, from this poitn on basically all the hard work is getting done using swap rather than ram, ya?</p>
<p>Thus, the simple fix is to knock KeepAliveTimeout right down - some people recommend 2 seconds, my personal preference is 4 seconds - works a charm for me :)</p>
<p>To analyse this yourself just check the server-status provided by apache (normally only viewable from localhost, easy way around this is to &lt;?php echo file_get_contents('http://localhost/server-status'); ?&gt;</p>
<h3>Easy Optimization - MySQL Server</h3>
<p>Other than the normal cnf values you should be optimizing [ key_buffer, sort_buffer_size, max_allowed_packet, thread_stack, thread_cache_size, max_connections, table_cache, query_cache_limit, query_cache_size ] - there is one more that is very important; namely wait_timeout.</p>
<p>wait_timeout - The number of seconds the server waits for activity on a noninteractive connection before closing it.</p>
<p>By default this value is very high, but often you get connections that just aren't closed properly, especially in the PHP apache world - if you ever hit that "Too many connections" error or see a lot of connections open for prolonged periods of time then this could / should be addressed.</p>
<p>Often you will see connections sitting idle for 10-15 seconds, for no good reason. Best to address it I'd say; and thus we lower wait_timeout to 5 seconds.</p>
<p>Connections are cleared faster, more resources free for other connections and all round its good. Remember one of mysql's strongest features is it's ability to open and close connections ultra-fast.</p>
<h3>Summary</h3>
<p>Honestly just check the your apache server status, see how many connections are open, how many of them are flagged K for keep alive then drop the keep alive time, take a note of processor usage / memory usage, drop the KeepAliveTimeout to 2, 4, 6, 8 and check it all again, you may be very suprised, it sure beats weeks of manual code optimizatios to make those scripts run faster.</p>
<p>Regards</p>
<p><img class="alignnone size-full wp-image-86" title="top" src="http://webr3.org/blog/wp-content/uploads/2009/07/top.jpg" alt="top" width="600" height="250" /></p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/optimization/speeding-up-apache2-and-mysql5-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
