<?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>bitblogr.com &#187; mod_expire</title>
	<atom:link href="http://www.bitblogr.com/tag/mod_expire/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bitblogr.com</link>
	<description>design, tutorials, inspiration, creativity</description>
	<lastBuildDate>Sat, 05 Jun 2010 17:20:23 +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>VPS, CDN and advanced server techniques: 2</title>
		<link>http://www.bitblogr.com/how-to/vps-cdn-and-advanced-server-techniques-2/</link>
		<comments>http://www.bitblogr.com/how-to/vps-cdn-and-advanced-server-techniques-2/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 03:18:10 +0000</pubDate>
		<dc:creator>Opeyemi Obembe</dc:creator>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[PC]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[mod_deflate]]></category>
		<category><![CDATA[mod_expire]]></category>
		<category><![CDATA[scalability]]></category>

		<guid isPermaLink="false">http://www.bitblogr.com/?p=104</guid>
		<description><![CDATA[In this piece, we will focus more on scalability &#8211; which I simply define as the ability of a server to efficiently adapt to more load. It is more complex as that sounds and to know more, you can check out scalability @ wikipedia. As a server administrator, optimizing your site and server for scalability [...]]]></description>
			<content:encoded><![CDATA[<div style="float:right;margin:0px 0px 0px 0px;"></div><div class="tweetmeme_button" style="float: right; margin-left: 10px;margin-right:10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.bitblogr.com%2Fhow-to%2Fvps-cdn-and-advanced-server-techniques-2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.bitblogr.com%2Fhow-to%2Fvps-cdn-and-advanced-server-techniques-2%2F&amp;source=bitblogr&amp;style=normal&amp;service_api=R_2574ec2cdf93a9dc24a80c02f8d96a25&amp;hashtags=caching,mod_deflate,mod_expire,scalability" height="61" width="50" /><br />
			</a>
		</div>
<p>In this piece, we will focus more on scalability &#8211; which I simply define as the ability of a server to efficiently adapt to more load. It is more complex as that sounds and to know more, you can check out <a href="http://en.wikipedia.org/wiki/Scalability">scalability @ wikipedia</a>.<span id="more-104"></span></p>
<p>As a server administrator, optimizing your site and server for scalability is a task you will face occasionally, if nor more often than that. At first, with just a couple of site visitors and members, all may seem well. But as users and visits grow, you will start finding the need.</p>
<p>There are two types of scalability actually &#8211; scaling out which is about adding more system nodes, like buying more servers and scaling up which is about effectively maximizing the potential of what you have on ground already. Scaling up is cheaper than scaling out, in fact, in some instance doesn&#8217;t even cost any dime &#8211; and this is what we are looking at here. Besides, if you start with scaling out, without scaling up, there is the every posiblity you will continue scaling out as just a little wrong thing with your app or server configuration may be causing excessive load and weighing down the server &#8211; a problem scaling out won&#8217;t solve. Scaling up on the other hand will let you detect such. After properly scaling up, you can then scale out.</p>
<p>So the big question &#8211; how do you scale up? And to that is a lot of answers. Yah, there are many ways to optimize your app/server &#8211; every single way you think can save the server memory and number of processes. Some of this include caching, code optimization, profiling and even good configuration of your server and database engine. Caching alone as a big parent, has a lot of children &#8211; setting proxy servers, database caching to mention just two.</p>
<p>In the <a href="http://www.bitblogr.com/how-to/vps-cdn-and-advanced-server-techniques-part-1/">last piece</a> I mentioned CDN (Content Delivery Network), and even this is a great scalability technique. I guess I do not need to recap the advantages here but having your static files served from a different server entirely saves your deployment server (your app server) more memory and processes, leaving it to take care of more important files (dynamic content for example). Mind you, your static files include your CSS, javascript, images, flash and co. At ngbot (<a href="http://mobile.ngbot.com">mobile</a>, <a href="http://ads.ngbot.com">ads</a>, <a href="http://answers.ngbot.com">answers</a>) though, we don&#8217;t store our CSS, JS and all images in our CDN but there is a simple caching technique we use to reduce the effect of serving it from the deployment server, which I will talk about in a gifi.</p>
<p>Continuing from where I stopped on CDN in the last piece, how do you get content in and out of the CDN? It is simple &#8211; APIs. CDNs provide APIs you can interface directly from your application. Most providers even offer code libraries in different programming language to help out. <a href="http://rackspace.com">Rackspace</a> (which we use) for example has a great php class implementing the api. It is simple. You simply authenticate with some already provided details and you are good to go.</p>
<p>Below is an example code excerpt</p>
<pre>include 'cloud/cloudfiles.php';
# Authenticate. CLOUDUSER and CLOUDAPI predefined with define()
$auth = new CF_Authentication(CLOUDUSER, CLOUDAPI);
$auth-&gt;ssl_use_cabundle();
$auth-&gt;authenticate();
# Get Container obj
$conn = new CF_Connection($auth);
# Get container named 'static'. NB: container = folder
$container = $conn-&gt;get_container('static');
# Public?
if(!$container-&gt;is_public())
{
	# make public and cache content for a day
	echo $container-&gt;make_public(86400).' : '.$v.'
';
}</pre>
<p>Note the cache part where you can set the content to be cached on the user&#8217;s system for a number of seconds (8400 in the example = a day). By such, the content is stored on the user&#8217;s system for the specified duration and won&#8217;t be requested again from the CDN during that time. This consequently saves your CDN traffic and saves you money.</p>
<p>Ok, CDN settled, lets see other ways to serve static content from your server, as we do for our css, js and some of our image files. To start with this is specific to the Apache web server. There are ways to do it with other servers though.</p>
<p>The technique is based on caching. If you serve just static pages in your site (which I doubt) you can actually set your server, Apache in this case, to cache the site content for a specified duration. For people like me who have issues with caching all content and setting up proxy servers however, there are ways to cache some specific content by content-type. It is called <strong>Mod Expire</strong> in Apache and you can read more about it from the <a href="http://httpd.apache.org/docs/2.0/mod/mod_expires.html">Apache docs</a></p>
<p>In general, here is what it looks like (in htaccess).</p>
<pre>ExpiresActive on
# Images and flash
ExpiresByType image/png "access plus 2 month"
ExpiresByType image/gif "access plus 2 month"
ExpiresByType image/jpeg "access plus 2 month"
ExpiresByType application/x-shockwave-flash "access plus 2 month"
# css may change a bit sometimes, so dont cache too long
ExpiresByType text/css "access plus 5 days"
# special MIME type for icons in case not set yet
AddType image/vnd.microsoft.icon .ico
# now we have icon MIME type, we can use it
# But my favicon doesn't change much, so say 3 mnths
ExpiresByType image/vnd.microsoft.icon "access plus 3 months"
ExpiresByType image/x-icon "access plus 3 months"
# JS? A month will do
ExpiresByType application/x-javascript "access plus 1 month"</pre>
<p>I&#8217;d love to go on and on, but again, let&#8217;s leave it at here. Don&#8217;t forget however that scaling is about sniffing and optimizing every little thing that will weigh down your server, starting from checking your server (e.g Apache) and database (e.g MySQL) configuration. If you are using a framework, especially for your server side, you should be sure you set things up properly. Even at that, <a href="http://carsonified.com/blog/dev/databases/speed-up-your-web-app-by-1000-with-1-line-of-sql/">be sensitive to page load speeds</a> and memory consumption. You can check out your server memory consumption and other vital details with the Linux command <strong>free</strong> from shell. If you are not much of the shell/console type, there is <a href="http://phpsysinfo.sourceforge.net/">phpSysinfo</a> which displays vital system info in a web page format.</p>
<p>And finally, talking page load speed, output compression is a life saver. By compressing content sent to the http client (say browser) from the server, you can drastically reduce your page load time. <a href="http://devedgelabs.com">We</a> use <a href="http://prototypejs.org">prototype</a> as our parent js framework wich is quite heavy &#8211; around 74KB minified. Think of how long t will take this alone to load not to talk of other scripts. However, by output compression, the file is reduced to just around 21KB.</p>
<p>Output compression is a very easy thing. Note though, it is an extra work for the server so be sure of the trade off. In Apache (2.x), it is as smple as something like this: (<a href="http://httpd.apache.org/docs/2.0/mod/mod_deflate.html">Read more here</a>)</p>
<pre># Compress javascript and css files
AddOutputFilterByType DEFLATE application/x-javascript text/css</pre>
<p>Compressing a PHP file on the other hand is as simple as adding the line below just before any output buffering (any content is displayed/echoed)</p>
<pre>ob_start("ob_gzhandler");</pre>
<p>Ok, that&#8217;s all for now. Hope you&#8217;ve been able to learn a thing or two. Catch you later ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitblogr.com/how-to/vps-cdn-and-advanced-server-techniques-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
