<?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>Pale Purple</title>
	<atom:link href="http://www.palepurple.co.uk/feed" rel="self" type="application/rss+xml" />
	<link>http://www.palepurple.co.uk</link>
	<description>Business Web Application Development and Training in PHP</description>
	<lastBuildDate>Fri, 11 May 2012 13:41:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Varnish for WordPress Performance</title>
		<link>http://www.palepurple.co.uk/varnish-for-wordpress-performance</link>
		<comments>http://www.palepurple.co.uk/varnish-for-wordpress-performance#comments</comments>
		<pubDate>Fri, 11 May 2012 13:41:03 +0000</pubDate>
		<dc:creator>palepurple</dc:creator>
				<category><![CDATA[systems administration]]></category>

		<guid isPermaLink="false">http://www.palepurple.co.uk/?p=436</guid>
		<description><![CDATA[How to improve WordPress/Apache performance on a relatively heavily loaded server using Varnish and more Scenario One server (8 cpu cores, 32gb of memory, RAID5 disks) running WordPress (PHP 5.3.x and MySQL 5.1.x). WordPress site(s) would often grind to a &#8230; <a href="http://www.palepurple.co.uk/varnish-for-wordpress-performance">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>How to improve WordPress/Apache performance on a relatively heavily loaded server using Varnish and more</p>
<p><span id="more-436"></span></p>
<h2>Scenario</h2>
<p>One server (8 cpu cores, 32gb of memory, RAID5 disks) running WordPress (PHP 5.3.x and MySQL 5.1.x). WordPress site(s) would often grind to a halt as the server struggled to respond to requests quickly enough.</p>
<h2>Problems</h2>
<p>Lack of available I/O capacity, identified by :</p>
<ol>
<li>Numerous processes often in a &#8216;waiting for I/O&#8217; state (as shown by &#8216;vmstat&#8217;)</li>
<li>MySQL becoming backed up with queries and frequently overwhelmed (running out of connections; large number of queries waiting in the process list). Problem especially apparent when a new post was made to the site &#8211; as this causes a flushing of the MySQL query cache, any on disk cache&#8217;s from WordPress and any related caches (e.g. the cached copy of the front page would need regenerating).</li>
<li>Performance graphs from &#8216;munin&#8217; plot I/O operations along with other virtual memory subsystem events.</li>
</ol>
<h2>Solutions</h2>
<ol>
<li>Profile WordPress code to identify expensive queries, add additional caching where possible. Using e.g. the <a title="Wordpress plugin - debug objects" href="http://wordpress.org/extend/plugins/debug-objects/">debug-objects</a> plugin to provide timings and help identify the origin of some queries.</li>
<li>Caching of pages using e.g. WPSuperCache or W3 Total Cache was already in place but not sufficient</li>
<li>Use memcached, rather than disk as an object cache for WordPress</li>
<li>Use the <a title="Apache documentation - mod_log" href="http://httpd.apache.org/docs/2.0/mod/mod_log_config.html">BufferedLog</a> directive within Apache to try and bulk up I/O operations</li>
<li>Install <a title="Varnish Cache" href="https://www.varnish-cache.org/">Varnish</a> as a front end cache, using a malloc memory backend to reduce disk I/O (no need to read file(s) from disk)</li>
</ol>
<p>Of the above, use of Varnish has had the biggest impact on disk I/O &#8211; resulting in a significant decrease. It also led to a significant decrease in the number of Apache processes in use (each of which is relatively memory hungry) &#8211; this is possible as a large part of the site is static content (images/stylesheets/javascript) and unchanging.</p>
<h2>Varnish configuration</h2>
<ol>
<li>Use a malloc backend, 10GiB in size. (See /etc/default/varnish on a Debian based system). Configure to use &#8220;-s malloc,10G&#8221; as a startup parameter. Before using the &#8216;malloc&#8217; backend (and using the default file backend) there was no improvement in I/O and performance problems persisted.</li>
<li>Ignore cookies for all javascript, images and stylesheets to improve the cache hit rate</li>
<li>Ignore query parameters on all images, javascript and stylesheets &#8211; again to improve caching</li>
<li>Force a future expiry date on static assets to encourage browser side caching of files</li>
<li>Ignore all cookies for HTML/PHP content, unless they contain &#8220;wordpress_logged_in&#8221; &#8211; which is a suitable indication that the end user is authenticated.</li>
<li>Add a backend health probe &#8211; so Varnish can display a suitable error page to end users &#8211; rather than showing either a Apache/Wordpress error message or timing out.</li>
</ol>
<h2>Pretty Pictures</h2>
<p>Here are some graphs taken from the server, which give some idea of the impact Varnish has had.</p>
<p>Firstly, we have the number of accesses registered by Apache &#8211; before and after Varnish was introduced. Because Varnish sits infront of Apache, when it is introduced the workload of Apache drops.</p>
<div id="attachment_438" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.palepurple.co.uk/wp-content/uploads/2012/05/apache_accesses-week.png"><img class="size-medium wp-image-438" title="Apache - Accesses per week" src="http://www.palepurple.co.uk/wp-content/uploads/2012/05/apache_accesses-week-300x164.png" alt="Note the drop off on the 7th when Varnish was introduced" width="300" height="164" /></a><p class="wp-caption-text">Graph showing the number of Apache Accesses over a week long period</p></div>
<div id="attachment_439" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.palepurple.co.uk/wp-content/uploads/2012/05/apache_processes-week.png"><img class="size-medium wp-image-439" title="Number of Apache Processes " src="http://www.palepurple.co.uk/wp-content/uploads/2012/05/apache_processes-week-300x178.png" alt="Number of Apache Processes " width="300" height="178" /></a><p class="wp-caption-text">Number of Apache Processes</p></div>
<p>This next graph shows the hit rate logged by Varnish &#8211; i.e. the number of hits per second it deals with. As varnish is made &#8216;live&#8217; it&#8217;s hit-rate increases while Apache&#8217;s decreases.</p>
<div id="attachment_441" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.palepurple.co.uk/wp-content/uploads/2012/05/varnish_hitrate-week.png"><img class="size-medium wp-image-441" title="Varnish Hit Rate" src="http://www.palepurple.co.uk/wp-content/uploads/2012/05/varnish_hitrate-week-300x164.png" alt="Varnish Hit Rate" width="300" height="164" /></a><p class="wp-caption-text">Number of HTTP requests received by Varnish</p></div>
<p><a href="http://www.palepurple.co.uk/wp-content/uploads/2012/05/varnish_cachehitratio-week.png"><img class="aligncenter size-medium wp-image-440" title="Varnish Cache Hit Ratio" src="http://www.palepurple.co.uk/wp-content/uploads/2012/05/varnish_cachehitratio-week-300x178.png" alt="Varnish Cache Hit Ratio" width="300" height="178" /></a></p>
<p>Before the 07th, Varnish was only accessible for testing, on/after the 7th the site&#8217;s DNS entries were changed to route traffic through Varnish. Further configuration changes were made to improve the cache hit rate.</p>
<p>Finally, we have the I/O graph &#8211; note how initially varnish doesn&#8217;t help with the I/O load on the server (if anything it makes it worse between the 7th and 10th).</p>
<p>On the 10th Varnish was reconfigured to use the malloc backend &#8211; at which point the I/O load drops down and appears to remain more consistent.</p>
<p style="text-align: center;"><a href="http://www.palepurple.co.uk/wp-content/uploads/2012/05/iostat-week.png"><img class="aligncenter size-medium wp-image-442" title="Disk I/O statistics for the server" src="http://www.palepurple.co.uk/wp-content/uploads/2012/05/iostat-week-300x164.png" alt="Disk I/O graph" width="300" height="164" /></a></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.palepurple.co.uk/varnish-for-wordpress-performance/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Everything&#8217;s turning mobile &#8211; a talk given to BCS Birmingham in April 2012</title>
		<link>http://www.palepurple.co.uk/everything-is-turning-mobile-bcs-birmingham-april</link>
		<comments>http://www.palepurple.co.uk/everything-is-turning-mobile-bcs-birmingham-april#comments</comments>
		<pubDate>Tue, 24 Apr 2012 08:54:27 +0000</pubDate>
		<dc:creator>palepurple</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.palepurple.co.uk/?p=432</guid>
		<description><![CDATA[Last Tuesday I [DG] presented a talk to BCS Birmingham, titled &#8220;Everything is turning mobile&#8221; (See the Birmingham.BCS.org website here). I&#8217;ve also been &#8220;volunteered&#8221; into presenting a similar talk to the Wolverhampton branch of BCS sometime in the next month or &#8230; <a href="http://www.palepurple.co.uk/everything-is-turning-mobile-bcs-birmingham-april">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Last Tuesday I [DG] presented a talk to BCS Birmingham, titled &#8220;Everything is turning mobile&#8221; (See the <a title="Birmingham BCS website - Mobile application development talk" href="http://birmingham.bcs.org/events/2012-04-17.html">Birmingham.BCS.org website here</a>).</p>
<p>I&#8217;ve also been &#8220;volunteered&#8221; into presenting a similar talk to the Wolverhampton branch of BCS sometime in the next month or two.</p>
<p>The talk covered a little bit of history (i.e.. what was happening 5-10 years ago) and where we are today. It then shows how various technologies can be used to create an optimised website or mobile application, gave some examples of what we [Pale Purple] have created and issues we&#8217;ve encountered.</p>
<p>Thanks to BCS for arranging a great meeting which was well attended.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.palepurple.co.uk/everything-is-turning-mobile-bcs-birmingham-april/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Interviewing time again</title>
		<link>http://www.palepurple.co.uk/its-interviewing-time-again-in-2012</link>
		<comments>http://www.palepurple.co.uk/its-interviewing-time-again-in-2012#comments</comments>
		<pubDate>Tue, 06 Mar 2012 12:01:00 +0000</pubDate>
		<dc:creator>palepurple</dc:creator>
				<category><![CDATA[recruitment]]></category>

		<guid isPermaLink="false">http://www.palepurple.co.uk/?p=406</guid>
		<description><![CDATA[It appears to be that time of the year when I arrange to take a trip to Aberystwyth to interview students for a possible Industrial Year placement with Pale Purple. We&#8217;ve had 13 applications this year &#8211; some names I &#8230; <a href="http://www.palepurple.co.uk/its-interviewing-time-again-in-2012">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It appears to be that time of the year when I arrange to take a trip to <a title="Computer Science in Aberystwyth" href="http://www.aber.ac.uk/en/undergrad/courses/compsci/">Aberystwyth</a> to interview students for a possible Industrial Year placement with Pale Purple. We&#8217;ve had 13 applications this year &#8211; some names I remember from the <a title="Gregynog " href="http://www.palepurple.co.uk/gregynog-2011-interviewing-feedback">Gregynog</a> interviews back in Autumn 2011. They all look good &#8211; so it&#8217;s going to be difficult to narrow them down to 5-6 for interviewing purposes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.palepurple.co.uk/its-interviewing-time-again-in-2012/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP UK Conference 2012</title>
		<link>http://www.palepurple.co.uk/php-uk-conference-2012</link>
		<comments>http://www.palepurple.co.uk/php-uk-conference-2012#comments</comments>
		<pubDate>Wed, 29 Feb 2012 15:18:22 +0000</pubDate>
		<dc:creator>palepurple</dc:creator>
				<category><![CDATA[conferences]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[london]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.palepurple.co.uk/?p=426</guid>
		<description><![CDATA[On Friday and Saturday, last week, the annual  London PHP conference took place. We were there on Friday &#8211; and I saw the talks on : &#8220;The Journey towards Continuous Integration&#8221; (which has prompted me to upgrade our internal Jenkins based CI &#8230; <a href="http://www.palepurple.co.uk/php-uk-conference-2012">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>On Friday and Saturday, last week, the annual  London <a title="PHP UK Conference" href="http://www.phpconference.co.uk">PHP conference</a> took place.</p>
<p>We were there on Friday &#8211; and I saw the talks on :</p>
<ul>
<li>&#8220;The Journey towards Continuous Integration&#8221; (which has prompted me to upgrade our internal Jenkins based CI infrastructure and introduce Sonar for long term statistics capture)</li>
<li>&#8220;Security Audits as an Integral part of PHP application development&#8221; (the presenter unfortunately wasn&#8217;t aware of any tools which could be easily integrated with a build environment like Jenkins, so the only approach seems to still be manual review or penetration testing on deliverables).</li>
<li>&#8220;Profiling PHP Applications&#8221; (which was good, but could have been improved if the presenter had a &#8220;real&#8221; application to demo with which had performance problems).</li>
<li>&#8220;Try { getting people to come to a talk about exceptions}&#8221; &#8211; which was an interesting look on exception and error reporting within software.</li>
</ul>
<p>The conference was (as with previous years) well attended and contained a number of useful nuggets of information which made attending worthwhile.</p>
<p>As with previous years, videos of all the talks should be online &#8211; so catch-up should be possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.palepurple.co.uk/php-uk-conference-2012/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fight the bulge &#8211; page load time matters</title>
		<link>http://www.palepurple.co.uk/fight-the-bulge-page-load-time-matters</link>
		<comments>http://www.palepurple.co.uk/fight-the-bulge-page-load-time-matters#comments</comments>
		<pubDate>Tue, 31 Jan 2012 10:49:36 +0000</pubDate>
		<dc:creator>palepurple</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[compression]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[deflate]]></category>
		<category><![CDATA[expiry]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[speed]]></category>

		<guid isPermaLink="false">http://www.palepurple.co.uk/?p=401</guid>
		<description><![CDATA[One often overlooked, area ripe for improvement on websites is that of page weight &#8211; namely how much data needs to be downloaded by the web browser before the page is rendered. Most web pages will be constructed from a &#8230; <a href="http://www.palepurple.co.uk/fight-the-bulge-page-load-time-matters">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One often overlooked, area ripe for improvement on websites is that of page weight &#8211; namely how much data needs to be downloaded by the web browser before the page is rendered. Most web pages will be constructed from a mixture of Javascript, Stylesheets (CSS), Images and HTML. A <a title="BBC News - Girth of webpages increasing" href="http://www.bbc.co.uk/news/technology-16300000">BBC news article</a> states that the average page &#8216;weight&#8217; is approaching 1mb</p>
<p>Why does it matter? Well, although it&#8217;s tempting to think everyone must have pretty quick &gt;4mbit ADSL, it&#8217;s not always the case -</p>
<ol>
<li>Mobile users may be on a GPRS (2G) or 3G connection &#8211; which is normally far less than 1Mbit/s (3g minimum should be 200Kbit). At this speed our average page would take about 40 seconds to load.</li>
<li>Some people (often in rural areas) will be limited to around around 512Kb or 1Mbit &#8211; due to the long distances between themselves and the nearest telephone exchange (so around about 10 seconds to load).</li>
<li>If you&#8217;re lucky enough to have around a 10Mbit ADSL connection, then the download would take around about 1 second.</li>
</ol>
<p>We seem to be seeing mobile users accounting for around about 10-20% of the traffic to websites at the moment &#8211; and this figure is likely to grow as <a title="Apple selling more iPads than the HP sells PCs" href="http://www.reghardware.com/2012/01/31/canalys_q4_2011_figures_put_apple_on_top_over_hp/">tablet computing becomes more popular</a>. Having a mobile theme on your website is likely to help with this &#8211; so resources are loaded via AJAX (see e.g <a title="jQuery Mobile page loading" href="http://jquerymobile.com/demos/1.0.1/docs/pages/page-anatomy.html">jQueryMobile</a>)</p>
<p>From a developers point of view, there are a number of things we can do to help reduce the &#8216;weight&#8217; of a web page &#8211; for example :</p>
<ol>
<li>Enable compression on textual data being sent back by the web server using deflate or gzip (trading off some CPU time against network bandwidth) (Example below)</li>
<li>Resize images so they are not being resized in a stylesheet / by the browser &#8211; when using PHP, something like the <a title="phpimageresize" href="https://github.com/palepurple/phpimageresize">phpimageresize</a> plugin could be used (Example below)</li>
<li>Ensure static assets have appropriate / correct expiry times/headers to encourage client side caching (or caching by upstream proxy servers) (see mod_expires) (Example below)</li>
<li>Try and use &#8216;common&#8217; URLs for JS libraries (e.g. Google&#8217;s API hosting) &#8211; as there&#8217;s a reasonable chance the user will already have the script cached in their browser before visiting your site.</li>
<li>Compact or minimise stylesheets and javascript resources &#8211; removing unnecessary spaces and comments (see e.g. the <a title="Yahoo YUI Compressor" href="http://developer.yahoo.com/yui/compressor/">YUI compressor</a> )</li>
<li>Delay loading/fetching of Javascript (see e.g. the <a title="contentLoader @ jsClasses.org" href="http://www.jsclasses.org/blog/package/24/post/1-Fast-Page-Loading-Tutorial.html">contentLoader script</a> from jsclasses.org) until after the initial page has loaded.</li>
<li>Merge multiple resources together (the Google <a title="Google's mod_pagespeed Apache plugin" href="http://code.google.com/speed/page-speed/docs/module.html">mod_pagespeed</a> Apache plugin can do this) &#8211; so rather than your browser making multiple requests to the server for multiple javascript files, it sees only one.</li>
<li>Appropriate use of AJAX to load content (saving the user from having to download unchanged HTML between &#8216;pages&#8217;).</li>
</ol>
<h2>Server side data compression</h2>
<p>If you&#8217;re using Apache, enabling the &#8216;deflate&#8217; module and then having something like the following in a .htaccess file should work well -</p>
<p><code>AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html</code></p>
<p>PHP can also perform the compression for you, but handling the compression through the web server will give better results (more stuff will be compressed).</p>
<h2>Image Resizing</h2>
<p>If a graphic designer has uploaded an image to the website, and it then gets resized through CSS, your browser is technically downloading more data than it needs to. Instances of this can be easily discovered using the <a title="Google Page Speed" href="https://developers.google.com/pagespeed/">Google PageSpeed tool</a>.</p>
<p>Dynamic resizing of images can be done using the <a title="phpimageresize" href="https://github.com/palepurple/phpimageresize">phpImageResize</a> tool (and I&#8217;m sure there are many other alternatives) as follows &#8211; on the assumption we want to only show a 20px x 20px image:</p>
<p><code> &lt;img src="&lt;?php echo resize('images/whatever.jpg', array('h' =&gt; 20, 'w' =&gt; 20, 'scale' =&gt; true)); ?&gt;"&gt;<br />
</code></p>
<p>For one customer we found correctly resizing the images reduced page &#8216;weight&#8217; from around about 5mb originally to 1mb (it is a very image heavy news site/blog). Such a drastic reduction will make the site feel &#8216;snappier&#8217; and more responsive to all users, save on server resources (bandwidth) and allow the server to handle more traffic at once. This should lead to more page impressions and therefore greater advertising revenue.</p>
<h2>Expiry Times</h2>
<p>Adding something like the following to a &#8216;.htaccess&#8217; file should work well, assuming Apache as your web server:</p>
<p><code> ExpiresByType text/css A7200<br />
ExpiresByType application/x-javascript A7200</code></p>
<p>Which tells the browser to cache the javascript and CSS files for 2 hours after access. Often the expiry value can be far higher &#8211; often days, or even years.</p>
<p>It&#8217;s also normally useful to turn off eTag support at this point (&#8220;FileEtag None&#8221; in .htaccess) &#8211; to try and stop browsers trying to validate whether their cache is up to date on each request.</p>
<h2>Common URLs for JS Libraries</h2>
<p>For example, rather than hosting jQuery from your own server (which is probably identical to jQuery on many other servers) you could use e.g.</p>
<p>&lt;script src=&#8221;https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js&#8221;&gt;&lt;/script&gt;</p>
<p>Which is both minified and there&#8217;s a reasonable chance the end user may already have it cached due to other websites using it. See the<a title="Google Code - jQuery etc apis" href="http://code.google.com/apis/libraries/devguide.html#jquery"> Google code site</a> for more information.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.palepurple.co.uk/fight-the-bulge-page-load-time-matters/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Circuit Breaker Design Pattern</title>
		<link>http://www.palepurple.co.uk/the-circuit-breaker-design-pattern</link>
		<comments>http://www.palepurple.co.uk/the-circuit-breaker-design-pattern#comments</comments>
		<pubDate>Thu, 12 Jan 2012 13:44:30 +0000</pubDate>
		<dc:creator>palepurple</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[training]]></category>
		<category><![CDATA[circuit breaker]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://www.palepurple.co.uk/?p=391</guid>
		<description><![CDATA[This post is based upon some content within our PHP OO training course (which covers design patterns). The circuit breaker design pattern is a fairly simple, and handy approach to dealing with remote services which may be offline. To explain &#8230; <a href="http://www.palepurple.co.uk/the-circuit-breaker-design-pattern">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This post is based upon some content within our <a title="Object Orientation training course" href="http://www.palepurple.co.uk/php-training/object-orientation">PHP OO training course</a> (which covers design patterns).</p>
<p>The <a title="Circuit Breaker Design Pattern" href="http://en.wikipedia.org/wiki/Circuit_breaker_design_pattern">circuit breaker design pattern</a> is a fairly simple, and handy approach to dealing with remote services which may be offline. To explain the pattern, here&#8217;s a semi-true story -</p>
<p>So, imagine the front page of a website includes some output from talking to twitter. However, one day twitter is offline &#8211; and all visitors to the site start to experience a 10-15 second delay in page load time. You investigate the problem and discover it&#8217;s due to Twitter being offline &#8211; in that all requests to it are timing out. What would be nice at this point in time is if your code could have known in advance that Twitter was offline and skipped making a request on it &#8211; therefore saving the end users from experiencing a page load delay.</p>
<p>In order to implement the above, we need some sort of shared state between requests &#8211; within a PHP context, we could use something like memcache or APC (both are fast and pretty simple to use).</p>
<p>So, before your code checks a remote service, it checks the circuit breaker&#8217;s status. If the circuit breaker says &#8220;ok&#8221; then your code talks to the remote service and notifies the circuit breaker of the outcome (success or failure). If more than a set number of failures occur within a set time period then the circuit breaker changes state and your code can therefore avoid experiencing whatever timeouts may have otherwise occurred.</p>
<p>Pseudo code to illustrate the above could look like :</p>
<pre class="brush:php">&lt;?php
$cb = new CircuitBreaker('talking_to_twitter');
if($cb-&gt;isOk()) {
    $url = 'http://search.twitter.com/search.json?q=cake';
    $json = @file_get_contents($url);
    if(false === $json) {
        // immediate problem with twitter -
        // tell the circuit breaker.
        $cb-&gt;fail();
    }
    else {
        // let's try and decode the data - check it's valid.
        $data = @json_decode($json);
        if(false === $data || !isset($data['completed_in'])) {
            $cb-&gt;fail();
        }
        else {
            // everything looks good... tell the circuit breaker
            // and print something trivial out.
            $cb-&gt;success();
            echo $data['results'][0]['text'];
        }
   }
}
else {
   // circuit breaker thinks twitter is ill, so don't bother trying
   echo "Sorry, twitter is down <img src='http://www.palepurple.co.uk/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' />  ";
}</pre>
<p>Unfortunately there don&#8217;t seem to be many PHP CircuitBreakers around &#8211; the best examples I can find (outside of what I think is proprietary code belonging to a customer) is <a title="Blog post" href="http://www.litfuel.net/plush/?postid=181">this post</a> (<a title="PHP Circuit breaker - uses MySQL" href="http://code.google.com/p/plushcode/source/browse/trunk/stability_patterns/circuit_breaker/CircuitBreaker.php">which uses a database</a>) and this<a title="ZF Circuit Breaker Proposal" href="http://artur.ejsmont.org/blog/PHP-Circuit-Breaker-initial-Zend-Framework-proposal"> proposal for one</a> to go into the Zend Framework.</p>
<p>A good circuit breaker would need to :</p>
<ul>
<li>Support multiple instances (e.g. one for Twitter, one for talking to Google or whatever)</li>
<li>Support variable timeouts and thresholds</li>
<li>Support different storage &#8216;backends&#8217; (e.g. memcache, APC, MySQL (perhaps) or fileSystem) &#8211; at which point it might be best for it to just use Zend Cache.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.palepurple.co.uk/the-circuit-breaker-design-pattern/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Help! Developer needed! (PHP/Bromsgrove) (Jan 2012)</title>
		<link>http://www.palepurple.co.uk/developer-needed-php-bromsgrove-jan-2012</link>
		<comments>http://www.palepurple.co.uk/developer-needed-php-bromsgrove-jan-2012#comments</comments>
		<pubDate>Thu, 12 Jan 2012 12:43:56 +0000</pubDate>
		<dc:creator>palepurple</dc:creator>
				<category><![CDATA[palepurple]]></category>
		<category><![CDATA[recruitment]]></category>

		<guid isPermaLink="false">http://www.palepurple.co.uk/?p=389</guid>
		<description><![CDATA[We seem to have too much work at the moment; and will need to either hire a new developer within the next few months &#8211; or use a freelancer/contractor. So if you&#8217;re looking for work (or a change) here are &#8230; <a href="http://www.palepurple.co.uk/developer-needed-php-bromsgrove-jan-2012">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>We seem to have too much work at the moment; and will need to either hire a new developer within the next few months &#8211; or use a freelancer/contractor. So if you&#8217;re looking for work (or a change) here are some details:</p>
<p>Your primary role will involve building and testing PHP based web applications.</p>
<ol>
<li>PHP &#8211; you should know about design patterns (MVC, Factory, CircuitBreaker and so on), be able to talk to a database and be aware of security considerations. You should have knowledge/experience with PHP Frameworks (e.g. Zend).</li>
<li>CSS</li>
<li>Javascript &#8211; AJAX, jQuery &#8230;</li>
<li>Unit testing &#8211; PHPUnit, Selenium and Jenkins.</li>
</ol>
<p>If anyone is interested, please contact david at palepurple.co.uk with a CV and salary expectations.</p>
<p>Please no agencies.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.palepurple.co.uk/developer-needed-php-bromsgrove-jan-2012/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple load checking shell script</title>
		<link>http://www.palepurple.co.uk/simple-load-checking-shell-script</link>
		<comments>http://www.palepurple.co.uk/simple-load-checking-shell-script#comments</comments>
		<pubDate>Thu, 24 Nov 2011 11:13:57 +0000</pubDate>
		<dc:creator>palepurple</dc:creator>
				<category><![CDATA[systems administration]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[load]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[uptime]]></category>

		<guid isPermaLink="false">http://www.palepurple.co.uk/?p=380</guid>
		<description><![CDATA[Below is a simple shell script which can be used to control execution of tasks on a Linux system based on the systems current load value &#8211; with the intention that if the 5 minute load average is greater than &#8230; <a href="http://www.palepurple.co.uk/simple-load-checking-shell-script">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Below is a simple shell script which can be used to control execution of tasks on a Linux system based on the systems current load value &#8211; with the intention that if the 5 minute load average is greater than a given value the script exits with an error return code (1) or completes without error (0).</p>
<p>In this case saved in a file called /usr/local/bin/load_check,</p>
<pre>#!/bin/bash

if [ -z $1 ]; then
    echo "Incorrect usage .... " &gt; /dev/stderr
    exit 1
fi

LOADLIMIT=$1

load_avg=$(uptime | awk -F 'load average:' '{print $2}' | cut -d, -f1)
if [[ $load_avg &lt; $LOADLIMIT ]]; then
    exit 0
fi
exit 1</pre>
<p>And usage would look like :</p>
<pre>/usr/local/bin/load_check 3 &amp;&amp; run/whatever/command</pre>
<p>It&#8217;s now possible to modify non-essential cron jobs (for example /etc/cron.d/munin) so that they do not run if the system is deemed too busy &#8211; so changing :</p>
<pre>*/5 * * * *     munin if [ -x /usr/bin/munin-cron ]; then /usr/bin/munin-cron; fi</pre>
<p>to</p>
<pre>*/5 * * * *     munin if [ -x /usr/bin/munin-cron ]; then /usr/local/bin/load_check 4 &amp;&amp; /usr/bin/munin-cron; fi</pre>
<p>Will result in munin-cron not running if the load is over 4. Rinse and repeat for other cron jobs which aren&#8217;t critical.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.palepurple.co.uk/simple-load-checking-shell-script/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gregynog 2011 &#8211; Interviewing feedback</title>
		<link>http://www.palepurple.co.uk/gregynog-2011-interviewing-feedback</link>
		<comments>http://www.palepurple.co.uk/gregynog-2011-interviewing-feedback#comments</comments>
		<pubDate>Sat, 12 Nov 2011 22:28:52 +0000</pubDate>
		<dc:creator>palepurple</dc:creator>
				<category><![CDATA[recruitment]]></category>
		<category><![CDATA[aberystwyth]]></category>
		<category><![CDATA[gregynog]]></category>
		<category><![CDATA[interviewing]]></category>
		<category><![CDATA[students]]></category>

		<guid isPermaLink="false">http://www.palepurple.co.uk/?p=368</guid>
		<description><![CDATA[Today, I interviewed a number of second year Aberystwyth Computer Science students at Gregynog. The aim of the exercise is to help prepare them for upcoming industrial placement interviews. As a whole, the students were better this year &#8211; their CVs &#8230; <a href="http://www.palepurple.co.uk/gregynog-2011-interviewing-feedback">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today, I interviewed a number of second year <a title="Aberystywyth Computer Science" href="http://www.aber.ac.uk/compsci">Aberystwyth Computer Science</a> students at Gregynog. The aim of the exercise is to help prepare them for upcoming industrial placement interviews.</p>
<p>As a whole, the students were better this year &#8211; their CVs and covering letters had fewer obvious mistakes and appeared better prepared. The majority of students were also smartly dressed which gave a good impression.</p>
<p>However, many students undersell themselves &#8211; CVs were often missing reference to work they&#8217;d done outside their degree scheme &#8211; or the extent of the reference was &#8220;Perl&#8221; or &#8220;Python&#8221;. Yet, in one example a student had written a Python/MySQL GUI application and others had experimented with JQuery, CSS3 or HTML5.</p>
<p>Most students expressed a deep interest or passion in a specific field &#8211; yet they would often lack supporting &#8220;evidence&#8221; of self directed research. Being able to mention a mailing list / google group / relevant website / conference or hot topic within that field would lend credibility to the claim &#8211; employers want employees who are genuinely motivated and interested.</p>
<p>Finally &#8211; it was common to see students saying something like &#8220;It will help me in my degree to work for you as I&#8217;ll learn to do X and Y&#8221;. Unsurprisingly an employer is not likely to be interested in what the student will get from the employment period &#8211; they are interested in what benefits the student will bring to them and their team.</p>
<p>See also - <a title="blog post" href="http://www.palepurple.co.uk/interviewing-students-some-findings">http://www.palepurple.co.uk/interviewing-students-some-findings</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.palepurple.co.uk/gregynog-2011-interviewing-feedback/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP Barcelona conference, November 2011.</title>
		<link>http://www.palepurple.co.uk/php-barcelona-conference-november-2011</link>
		<comments>http://www.palepurple.co.uk/php-barcelona-conference-november-2011#comments</comments>
		<pubDate>Thu, 03 Nov 2011 20:48:39 +0000</pubDate>
		<dc:creator>palepurple</dc:creator>
				<category><![CDATA[conferences]]></category>
		<category><![CDATA[barcelona]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.palepurple.co.uk/?p=364</guid>
		<description><![CDATA[This is a quick summary of the PHP Barcelona conference &#8211; this is the second year one of us attended, and again it was well organised and had stimulating presentations/talks, such as: The Pomodoro Technique for time keeping Cloud hosting (managed &#8230; <a href="http://www.palepurple.co.uk/php-barcelona-conference-november-2011">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This is a quick summary of the <a title="PHP Barcelona" href="http://phpconference.es/">PHP Barcelona</a> conference &#8211; this is the second year one of us attended, and again it was well organised and had stimulating presentations/talks, such as:</p>
<ol>
<li>The <a title="Pomodoro time keeping technique" href="http://www.pomodorotechnique.com/">Pomodoro Technique</a> for time keeping</li>
<li>Cloud hosting (managed vs unmanaged with <a title="Microsoft's Azure cloud platform" href="http://www.microsoft.com/windowsazure/">Microsoft&#8217;s Azure</a>+ and <a title="Orchestra.io cloud hosting" href="http://orchestra.io/">Orchestra.io</a> as examples). This included live demos and a good coverage of the various options available (and why you might pick (for example) EC2 over Orchestra or vice versa).</li>
<li><a title="Solr Document Indexing" href="http://lucene.apache.org/solr/">Solr</a> &#8211; document indexing (which we are already using, but there was some useful content within the marathon 2 hour talk/workshop)</li>
<li>Unit testing as an afterthought &#8211; Marco Tabini (<a title="http://www.phparch.com/" href="http://www.phparch.com/">phparch.com</a>) spoke about his belief that when writing unit tests, code coverage on it&#8217;s own is not enough when it comes testing through unit tests &#8211; like phpunit &#8211; as it&#8217;s also necessary to cover how components interact with each other, and therefore effectively do testing of an entire application and not just isolated components (something we&#8217;d already discovered).</li>
<li><a title="Doctrine 2 documentation" href="http://www.doctrine-project.org/projects/orm/2.0/docs/en">Doctrine2</a> &#8211; an overview of why you should be using an ORM and not lower level SQL everywhere &#8211; followed by a quick overview of why Doctrine2 is better/faster/superior to Doctrine1.</li>
<li>PHP on the CLI &#8211; a few useful titbits where hidden within this talk covering using PHP for command line scripts (options parsing via the getopt library being the most useful).</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.palepurple.co.uk/php-barcelona-conference-november-2011/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Object Caching 579/611 objects using disk: basic

Served from: www.palepurple.co.uk @ 2012-05-18 15:32:57 -->
