<?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>UNIXy &#187; cache</title>
	<atom:link href="http://blog.unixy.net/tag/cache/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.unixy.net</link>
	<description>Fully Managed Dedicated Servers</description>
	<lastBuildDate>Thu, 09 Sep 2010 07:08:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to build your own CDN using BIND, GeoIP, Nginx, and Varnish</title>
		<link>http://blog.unixy.net/2010/07/how-to-build-your-own-cdn-using-bind-geoip-nginx-and-varnish/</link>
		<comments>http://blog.unixy.net/2010/07/how-to-build-your-own-cdn-using-bind-geoip-nginx-and-varnish/#comments</comments>
		<pubDate>Sun, 18 Jul 2010 18:40:51 +0000</pubDate>
		<dc:creator>UNIXy</dc:creator>
				<category><![CDATA[Challenge]]></category>
		<category><![CDATA[akamai]]></category>
		<category><![CDATA[bind]]></category>
		<category><![CDATA[build own cdn]]></category>
		<category><![CDATA[build your own cdn]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[CDN]]></category>
		<category><![CDATA[geodns]]></category>
		<category><![CDATA[GeoIP]]></category>
		<category><![CDATA[global CDN]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[own CDN]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[private CDN]]></category>
		<category><![CDATA[varnish]]></category>

		<guid isPermaLink="false">http://blog.unixy.net/?p=342</guid>
		<description><![CDATA[In this article, we shall outline the steps required to build a private Content Delivery or Distribution Network (CDN) using Varnish Cache, Nginx, BIND, and GeoIP. The goal is to build a CDN using free, readily available software but most importantly spend the least amount of funds possible. ]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.unixy.net%2F2010%2F07%2Fhow-to-build-your-own-cdn-using-bind-geoip-nginx-and-varnish%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.unixy.net%2F2010%2F07%2Fhow-to-build-your-own-cdn-using-bind-geoip-nginx-and-varnish%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>In this article, we shall outline the steps required to build a private Content Delivery or Distribution Network (CDN) using <a title="Varnish Cache" href="http://www.varnish-cache.org" target="_blank">Varnish</a> Cache and <a title="Nginx" href="http://www.nginx.org" target="_blank">Nginx</a>. The goal is to build a CDN using free, readily available software but most importantly spend the least amount of funds possible. To this end, all nodes participating in this network are going to be virtual machines (Xen, Virtuozzo, OpenVZ, etc). Should you have any questions or comments on the configuration of this CDN, please post them in this forum: <a title="Varnish Cache Forum" href="http://www.varnish-cache.info/" target="_blank">http://www.varnish-cache.info/</a></p>
<p>Our global CDN can not only keep the latest copy of static files closer to our global visitors but can also cache the most used pages (dynamic or not) in memory on the edge nodes! This means less trips to the geographically distant and slower Dynamic Node (see below). This is similar to what <a title="Akamai" href="http://www.akamai.com" target="_blank">Akamai</a> and other well known firms do, only at a fraction of the cost. However, in this article, and to keep things simple, we will only be caching static files.</p>
<p>Some of you might be surprised to learn that we built this global CDN free of charge for one of our beloved customers. UNIXY offers truly fully managed dedicated servers and clusters with <strong><a title="Managed server" href="http://www.unixy.net/dedicated-servers" target="_self">managed servers</a></strong><strong> starting as low as $45</strong>. Our motto is simple: what you cannot do with a few mouse clicks, we will gladly do it for you! Please visit us online when you have a chance: <a title="Truly Fully Managed Dedicated Servers" href="http://www.unixy.net" target="_self">http://www.unixy.net</a>. Please do ask if you have any question or comment. No question is minor!</p>
<ul>
<li><strong>The Big Picture</strong></li>
</ul>
<p>The illustration below presents a logical layout of our CDN. Edge nodes can be located just about anywhere in the world. One could also add more nodes at any location should there be a capacity need. The Dynamic Content Node will typically run a mixture of MySQL, Apache, and server-side software built using PHP, Ruby, Python, .Net, or any language for that matter.</p>
<p><strong> </strong></p>
<div id="attachment_344" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.unixy.net/wp-content/uploads/2010/07/CDN_design.png"><img class="size-medium wp-image-344" title="Global CDN Logical Design" src="http://blog.unixy.net/wp-content/uploads/2010/07/CDN_design-300x273.png" alt="Global CDN" width="300" height="273" /></a><p class="wp-caption-text">Global CDN</p></div>
<p><strong> </strong></p>
<p><strong> </strong></p>
<ol><strong><span style="font-weight: normal;"><br />
</span></strong></ol>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong></p>
<ul>
<li>Role of Each Software Component</li>
</ul>
<p></strong></p>
<p>Nginx is a lightweight high-performance Web server that is able to handle large traffic consistently. We are leveraging its proxy and caching capabilities. We shall compile Nginx and leverage the proxy module. This module allows us to cache data on the local disks of the remote edge locations.</p>
<p>As its name implies, Varnish Cache is a high-performance caching engine used to keep recently accessed content in memory for fastest access. Varnish is not a Web server. Hence our need to bundle it with Nginx, which is acting as a Web server at the edge nodes. We will cover Varnish in detail in our next installment.</p>
<p>And finally the glue that holds all of these components together: BIND. BIND is the DNS software used to map Internet host names to IP addresses. We shall patch Bind to add geographical filters support. In other words, BIND will serve each client the IP of closest edge node in the CDN. For example, an vistor from Africa will receive the edge node IP of South Africa or Morocco depending on the filters. We will touch on this later.</p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong></p>
<ul>
<li>Node Layout</li>
</ul>
<p></strong></p>
<p>At a minimum, we will need two nodes to demo and build our private CDN. That&#8217;s one Dynamic Content Node and one Edge Location node. The Dynamic Content Node will run the full LAMP stack along with BIND and the geographical filters patch. The Edge Location node will run Nginx and Varnish. One could always run BIND+GeoIP on a separate node as it is good practice. We will assign the hostname dynamic_content to the Dynamic Content Node and edge_node to the Edge Location.</p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong></p>
<ul>
<li>Installation and configuration</li>
</ul>
<p></strong></p>
<p>Download BIND from ISC: <a href="http://www.bind9.net/download">http://www.bind9.net/download</a></p>
<p>Download MaxMind&#8217;s C API: <a href="http://geolite.maxmind.com/download/geoip/api/c/">http://geolite.maxmind.com/download/geoip/api/c/</a></p>
<blockquote>
<div id="_mcePaste">[root@dynamic_node /]# cd /usr/src/</div>
<div id="_mcePaste">[root@dynamic_node src]# wget http://mirrors.24-7-solutions.net/pub/isc/bind9/9.2.4/bind-9.2.4.tar.gz</div>
<div>[root@dynamic_node src]# wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.6.tar.gz</div>
<div>[root@dynamic_node src]# tar -xzvf bind-9.2.4.tar.gz</div>
<div>[root@dynamic_node src]# tar -xzvf GeoIP-1.4.6.tar.gz</div>
<div>[root@dynamic_node src]# cd GeoIP-1.4.6</div>
<div>[root@dynamic_node GeoIP-1.4.6]# ./configure &#8211;prefix=/usr/local/geoip</div>
<div>[root@dynamic_node GeoIP-1.4.6]# make</div>
<div>[root@dynamic_node GeoIP-1.4.6]# make install</div>
<div>[root@dynamic_node GeoIP-1.4.6]# cd ..</div>
<div>
<div>[root@dynamic_node src]# patch -p0 &lt; bind-9.2.4-geodns-patch/patch.diff</div>
<div>[root@dynamic_node src]# cd bind-9.2.4</div>
<div>[root@dynamic_node bind-9.2.4]# CFLAGS=&#8221;-I/usr/local/geoip/include&#8221; LDFLAGS=&#8221;-L/usr/local/geoip/lib -lGeoIP&#8221; ./configure &#8211;prefix=/usr/local/bind</div>
<div>[root@dynamic_node bind-9.2.4]# make</div>
<div>[root@dynamic_node bind-9.2.4]# make install</div>
</div>
</blockquote>
<div>Bind-GeoIP comes with a named.conf file with examples on how to use filtering. Setup your zone files and test them accordingly. The GeoIP patch official page has instructions and examples. Be sure to read over it should you need help: <a href="http://www.caraytech.com/geodns/">http://www.caraytech.com/geodns/</a>. If you do not have access to nodes in the different geo locations around the world to test your BIND configuration, http://traceroute.org is a good resource to leverage. It allows one to test DNS resolution using a looking glass (ping).</div>
<div>Here is how the filters should look inside named.conf:</div>
<div>
<blockquote>
<div>view &#8220;us&#8221; {</div>
<div>// Match clients from US &amp; Canada</div>
<div>match-clients { country_US; country_CA; };</div>
<div>// Provide recursive service to internal clients only.</div>
<div>recursion no;</div>
<div>zone &#8220;cdn.unixy.net&#8221; {</div>
<div>type master;</div>
<div>file &#8220;pri/unixy-us.db&#8221;;</div>
<div>};</div>
<div>zone &#8220;.&#8221; IN {</div>
<div>type hint;</div>
<div>file &#8220;named.ca&#8221;;</div>
<div>};</div>
<div>};</div>
<div>view &#8220;latin&#8221; {</div>
<div>// Match from Argentina, Chile and Brazil</div>
<div>match-clients { country_AR; country_CL; country_BR; };</div>
<div>// Provide recursive service to internal clients only.</div>
<div>recursion no;</div>
<div>zone &#8220;cdn.unixy.net&#8221; {</div>
<div>type master;</div>
<div>file &#8220;pri/unixy-latin.db&#8221;;</div>
<div>};</div>
<div>zone &#8220;.&#8221; IN {</div>
<div>type hint;</div>
<div>file &#8220;named.ca&#8221;;</div>
<div>};</div>
<div>};</div>
</blockquote>
</div>
<div>Let us move on now and install Nginx and Varnish.</div>
<blockquote>
<div>[root@edge_node src]# wget http://nginx.org/download/nginx-0.8.45.tar.gz</div>
<div>[root@edge_node src]# tar -xzvf nginx-0.8.45.tar.gz</div>
<div>[root@edge_node src]# cd nginx-0.8.45</div>
<div>[root@edge_node nginx-0.8.45]# ./configure &#8211;prefix=/usr/local/nginx &#8211;with-http_realip_module</div>
<div>[root@edge_node nginx-0.8.45]# make</div>
<div>[root@edge_node nginx-0.8.45]# make install</div>
</blockquote>
<div>Here is our nginx.conf file with relevant lines only. All other configuration options are stock Nginx:</div>
<p><code> </code></p>
<div>
<p><code> </code></p>
</div>
<blockquote>
<div>http {</div>
<div>include       mime.types;</div>
<div>default_type  application/octet-stream;</div>
<div>sendfile        on;</div>
<div>keepalive_timeout  65;</div>
<div>upstream dynamic_node {</div>
<div style="padding-left: 30px;">server 1.1.1.1:80; # 1.1.1.1 is the IP of the Dynamic Node</div>
<div>}</div>
<div>server {</div>
<div>listen       81;</div>
<div>server_name  cdn.unixy.net;</div>
<div>location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css|mp3|swf|ico|flv)$ {</div>
<div>proxy_set_header  X-Real-IP  $remote_addr;</div>
<div><strong>proxy_pass http://dynamic_node;</strong></div>
<div><strong>proxy_store /var/www/cache$uri;</strong></div>
<div><strong>proxy_store_access user:rw group:rw all:r;</strong></div>
<div>}</div>
</blockquote>
<div>In bold above are configuration lines that are key and define our private CDN. The upstream is essentially going to be our Dynamic Node to which we pass requests that cannot be served from cache. Also, Nginx will only be caching static files like GIF, PNG, and JS. Varnish on the other hand will be caching dynamic pages. Notice how Nginx listens on port 81. This is because Varnish will listen on port 80 and will forward requests to Nginx on port 80. More on Varnish later.</div>
<div>Notice how we are using cdn.unixy.net as the handle for our virtual host name. It can be just about anything depending on your configuration. Once the cache builds up, you should start seeing files and directories being populated under /var/www/ as instructed above.</div>
<div>
<p>A few seconds of browsing and the disk cache is already populating:</p>
</div>
<div>
<blockquote><p>[root@edge_node /]# ls -al /var/www/cache<br />
contact-unixy  css  images  index.html  javascript  js<br />
[root@edge_node /]#</p></blockquote>
</div>
<div>Next we will proceed with installing Varnish. Varnish will act an in-memory cache. While it is not necessary, it can improve response time greatly. Nonetheless, installing Varnish does add a level of complexity to our configuration.</div>
<blockquote>
<div>[root@edge_node src]# wget http://downloads.sourceforge.net/project/varnish/varnish/2.1.2/varnish-2.1.2.tar.gz?use_mirror=cdnetworks-us-1&amp;ts=1279434397</div>
<div>[root@edge_node src]# tar -xzvf varnish-2.1.2.tar.gz</div>
<div>[root@edge_node src]# tar -xzvf varnish-2.1.2.tar.gz</div>
<div>[root@edge_node varnish-2.1.2]# ./configure &#8211;prefix=/usr/local/varnish</div>
<div>[root@edge_node varnish-2.1.2]# make</div>
<div>[root@edge_node varnish-2.1.2]# make install</div>
</blockquote>
<div>Be sure to follow guides online on the initial setup of Varnish. This article only covers the configuration of the CDN. There are certainly additional Varnish options that need tuning but those are most likely peculiar to your application.</div>
<div>
<blockquote>
<div>backend default {</div>
<div style="padding-left: 30px;">.host = &#8220;127.0.0.1&#8243;;</div>
<div style="padding-left: 30px;">.port = &#8220;81&#8243;;</div>
<div>}</div>
</blockquote>
</div>
<div>
<blockquote>
<div>sub vcl_recv {</div>
<div style="padding-left: 30px;">.<br />
.<br />
.<br />
if (req.url ~ &#8220;\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)$&#8221;) {</div>
<div style="padding-left: 60px;">return (lookup);</div>
<div style="padding-left: 30px;">}<br />
.<br />
.<br />
.</div>
<div>}</div>
</blockquote>
<blockquote>
<div>sub vcl_fetch {</div>
<div style="padding-left: 30px;">.<br />
.<br />
.<br />
if (req.url ~ &#8220;\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)$&#8221;) {</div>
<div style="padding-left: 60px;">unset obj.http.set-cookie;</div>
<div style="padding-left: 30px;">}<br />
.<br />
.<br />
.</div>
<div>}</div>
</blockquote>
<div>Go ahead and startup Varnish and browse around you portal a bit to build the cache. Monitor the command varnishstat on the edge node and you will be able to see the cache hits and misses. There should be more hits as the cache builds up over time and more objects are accessed.</div>
</div>
<div>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong></p>
<ul>
<li>Wrap up</li>
</ul>
<p></strong><strong><span style="font-weight: normal;">The instructions above can replicated across however many additional Edge Nodes you want to add. One could also add redundancy to the BIND+GeoIP setup by configuring secondary nodes. The illustration below shows the flow of a request from top to bottom.</span><br />
</strong></p>
<div>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong></p>
<div id="attachment_389" class="wp-caption aligncenter" style="width: 437px"><a href="http://blog.unixy.net/wp-content/uploads/2010/07/text3648.png"><img class="size-medium wp-image-389 " title="CDN Request and Response Flow" src="http://blog.unixy.net/wp-content/uploads/2010/07/text3648-300x237.png" alt="CDN Built Using Nginx and Varnish" width="427" height="338" /></a><p class="wp-caption-text">CDN Request and Response Flow</p></div>
<p></strong>That&#8217;s all folks! I hope you enjoyed this article.</p>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.unixy.net/2010/07/how-to-build-your-own-cdn-using-bind-geoip-nginx-and-varnish/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Improve Performance With MySQL Query Caching</title>
		<link>http://blog.unixy.net/2009/06/improve-performance-with-mysql-query-caching/</link>
		<comments>http://blog.unixy.net/2009/06/improve-performance-with-mysql-query-caching/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 05:38:00 +0000</pubDate>
		<dc:creator>UNIXy</dc:creator>
				<category><![CDATA[Crash Course]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[coalesce]]></category>
		<category><![CDATA[high CPU]]></category>
		<category><![CDATA[memory usage]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[performance improvement]]></category>
		<category><![CDATA[query cache]]></category>
		<category><![CDATA[query_cache_size]]></category>
		<category><![CDATA[speed up mysql]]></category>

		<guid isPermaLink="false">http://blog.unixy.net/?p=13</guid>
		<description><![CDATA[The MySQL query cache is used to store data retrieved using SELECT statements. The advantage of the query cache is that subsequent and identical SELECT statement results are retrieved directly from the query cache, which resides in memory. So instead of executing the SELECT statements again and again, the stream of data is pulled from [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.unixy.net%2F2009%2F06%2Fimprove-performance-with-mysql-query-caching%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.unixy.net%2F2009%2F06%2Fimprove-performance-with-mysql-query-caching%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>The MySQL query cache is used to store data retrieved using SELECT statements. The advantage of the query cache is that subsequent and identical SELECT statement results are retrieved directly from the query cache, which resides in memory. So instead of executing the SELECT statements again and again, the stream of data is pulled from memory. The end result is lower CPU and disk hits, which can improve server load average and most importantly responsiveness.</p>
<p>The query cache functionality has enough smartness that once configured, it runs itself. MySQL will make sure to flush cached data if there&#8217;s an update that takes place. This ensures stale data is flushed out and replaced with fresh entries. There is, however, an extra step that is required. The cache tends to become fragmented over time. But nothing that cannot be addressed with a simple script (see below).</p>
<p>Stock MySQL configurations set the query cache to 32MB. This means that 32MB worth of server memory is pre-allocated to MySQL. If you have a relatively large database, you will most certainly gain from increasing that value. After configuring the query cache, it takes a while for MySQL to build the cache depending on how busy your MySQL server is.</p>
<p>Let&#8217;s get the query cache configured. Inside the <em>[mysqld]</em> section in the MySQL configuration file (ex: /etc/my.cnf), configure the options below. For a server with 8GB memory and a <em>very busy</em> database, set the cache to 1GB:</p>
<blockquote><p>[mysqld]</p>
<p>query_cache_size = 1024M</p></blockquote>
<p>Then restart MySQL for the new option to take effect. Run this command to verify that the new query cache value is in place and that the query cache feature is indeed enabled:</p>
<blockquote><p># mysql -u root -e &#8220;show variables like &#8216;%query%&#8217;&#8221;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+<br />
| Variable_name                | Value      |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+<br />
| ft_query_expansion_limit     | 20         |<br />
<strong>| have_query_cache             | YES        |</strong><br />
| long_query_time              | 10         |<br />
| query_alloc_block_size       | 8192       |<br />
| query_cache_limit            | 1048576    |<br />
| query_cache_min_res_unit     | 4096       |<br />
<strong>| query_cache_size             | 1073741824 |</strong><br />
| query_cache_type             | ON         |<br />
| query_cache_wlock_invalidate | OFF        |<br />
| query_prealloc_size          | 8192       |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+</p></blockquote>
<p>If the <strong>have_query_cache</strong> option is set to YES, it means that the query cache is indeed up and running. <strong>query_cache_size</strong> represents the cache size in bytes. You can also monitor the <em>mysqld</em> process using top and you will see its <em>RES</em> value increase over time to above 1gb. This would be a good sign asb MySQL fills up the query cache with result sets from SELECT. There is another way to check on cache usage and health. Run the following command:</p>
<blockquote><p># mysql -u root -e &#8220;show status like &#8216;qc%&#8217;&#8221;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+<br />
| Variable_name           | Value     |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+<br />
<strong>| Qcache_free_blocks      | 69776     | </strong><br />
<strong> | Qcache_free_memory      | 756433256 |</strong><br />
| Qcache_hits             | 9518567   |<br />
| Qcache_inserts          | 3878394   |<br />
| Qcache_lowmem_prunes    | 49447     |<br />
| Qcache_not_cached       | 173233    |<br />
| Qcache_queries_in_cache | 122754    |<br />
| Qcache_total_blocks     | 315609    |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+</p></blockquote>
<p>If <strong>Qcache_free_memory</strong> stays high over a few days means that the <em>query_cache_size</em> value is set too high. A high <strong>Qcache_free_blocks</strong> means that memory is fragmented. So it is time to coalesce (make memory blocks contiguous) the region. This is done with the following command:</p>
<blockquote><p># mysql -u root -p -e &#8220;flush query cache&#8221;</p></blockquote>
<p>Contrary to the command name, this does not flush the cache in the sense that all entries become invalid. It simply defragments the memory region that is assigned to the query cache. The above command should be put in a script that run every 6 hours or so. Create a root crontab entry as such:</p>
<blockquote><p>* 6/* * * * /root/coalesce.sh</p></blockquote>
<p>Where <em>coalesce.sh</em> is a shell script:</p>
<blockquote><p>#!/bin/bash</p>
<p>mysql -u root -pyour_root_password -e <code>"flush query cache"</code>;</p></blockquote>
<p>The query cache can be further configured to match your database&#8217;s usage pattern. Be sure to read the MySQL documentation for the details. Hopefully, this post will come in handy for those that have unused memory and need to make the most out of their server. As our customer, we configure the query cache  on your server from the get-go. Be sure to ask us how we can help with your online project!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.unixy.net/2009/06/improve-performance-with-mysql-query-caching/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
