<?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/"
	>

<channel>
	<title>echolibre blog &#187; APC</title>
	<atom:link href="http://blog.echolibre.com/category/apc/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.echolibre.com</link>
	<description></description>
	<pubDate>Tue, 25 May 2010 13:52:16 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>HipHop for PHP, Facebook unveils it&#8217;s magic</title>
		<link>http://blog.echolibre.com/2010/02/hiphop-for-php-facebook-unveils-its-magic/</link>
		<comments>http://blog.echolibre.com/2010/02/hiphop-for-php-facebook-unveils-its-magic/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 18:38:26 +0000</pubDate>
		<dc:creator>David</dc:creator>
		
		<category><![CDATA[APC]]></category>

		<category><![CDATA[Facebook]]></category>

		<category><![CDATA[HipHopPHP]]></category>

		<category><![CDATA[LLVM]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[RoadsendPHP]]></category>

		<category><![CDATA[community]]></category>

		<category><![CDATA[echolibre]]></category>

		<category><![CDATA[industry]]></category>

		<category><![CDATA[innovation]]></category>

		<category><![CDATA[performance]]></category>

		<category><![CDATA[phc]]></category>

		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://blog.echolibre.com/?p=791</guid>
		<description><![CDATA[Facebook Releases HipHop for PHP]]></description>
			<content:encoded><![CDATA[<p>After many days of speculations all around the web about Facebook&#8217;s rewrite of PHP, today Haiping Zhao from the Facebook team has announced &#8220;HipHop for PHP&#8221;. The basic idea of HipHop for PHP is that it turns the code you write in PHP into C++ which then can be turned into machine code.</p>
<p>Even though there are others idea that have tried accomplishing the same goal as HipHop for PHP, I believe it is quite safe to assume that Facebook has a large enough user-base to produce code that is solid enough to run and can run well.</p>
<p>The announcement has been made on the <a title="Facebook HipHop For PHP" href="http://developers.facebook.com/news.php?blog=1&amp;story=358">Facebook blog earlier today</a>, and tonight there is going to be the video tech talk that everybody can watch:</p>
<blockquote><p>This evening we&#8217;re hosting a small group of developers to dive deeper into HipHop for PHP and will be streaming this tech talk live. Check back <a title="Facebook HipHop For PHP" href="http://developers.facebook.com/news.php?blog=1&amp;story=358">here</a> around 7:30pm Pacific time if you&#8217;d like to watch.</p></blockquote>
<p>A few questions come to mind even though we haven&#8217;t seen the code just yet. My main concern though is the one of buffer overflows and the security implications of turning PHP code into C++. As they say on the blog, it took nearly 18 months before having a relatively stable version and 3 developers. This is a very short lapse of time to develop a solution used by so many.</p>
<p>Another interest of mine related to this release is how does it compete with the likes of <a title="phc php compiler" href="http://phpcompiler.org">phc</a> or <a title="Roadsend PHP (raven)" href="http://code.roadsend.com/rphp">roadsend php</a>. If it does at all.</p>
<p>However I have noticed on their blog that Facebook has also developed HPHPi which seems to let you use HipHop but without having to actually compile your code before running it (The concept seems a bit like <a title="PHP APC stat" href="http://ie.php.net/manual/en/apc.configuration.php#ini.apc.stat">APC&#8217;s stat</a> on and off switch from the few lines of description), which seems like a quite interesting idea for the development stages.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.echolibre.com/2010/02/hiphop-for-php-facebook-unveils-its-magic/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Surviving the Dragon&#8217;s Den: Vertical Scaling</title>
		<link>http://blog.echolibre.com/2009/04/surviving-the-dragons-den-vertical-scaling/</link>
		<comments>http://blog.echolibre.com/2009/04/surviving-the-dragons-den-vertical-scaling/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 13:39:02 +0000</pubDate>
		<dc:creator>David</dc:creator>
		
		<category><![CDATA[APC]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[echolibre]]></category>

		<category><![CDATA[industry]]></category>

		<category><![CDATA[performance]]></category>

		<category><![CDATA[apache]]></category>

		<category><![CDATA[Helgi]]></category>

		<category><![CDATA[mysql]]></category>

		<category><![CDATA[Open Source]]></category>

		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://blog.echolibre.com/?p=378</guid>
		<description><![CDATA[According to wikipedia, the Dragon&#8217;s Den is:
a venture-capitalist television programme that originated in Japan where the format is owned by Sony. The format, which now airs internationally, consists of entrepreneurs pitching their ideas in order to secure investment finance from business experts — the &#8220;Dragons&#8221;.
As some may already know, in 2009 the television show began [...]]]></description>
			<content:encoded><![CDATA[<p>According to <a title="Dragon's Den Wikipedia" href="http://short.ie/wikipedia-dragons-den" target="_blank">wikipedia, the Dragon&#8217;s Den</a> is:</p>
<blockquote><p>a venture-capitalist television programme that originated in Japan where the format is owned by Sony. The format, which now airs internationally, consists of entrepreneurs pitching their ideas in order to secure investment finance from business experts — the &#8220;Dragons&#8221;.</p></blockquote>
<p>As some may already know, in 2009 the television show began in <a title="Dragon's Den on RTÉ ONE" href="http://rte.ie/dragonsden" target="_blank">Ireland on RTÉ ONE</a>. This post covers the technical considerations encountered when a web site / application appears on national television.<span id="more-378"></span></p>
<h2>Some background</h2>
<p>[<a title="Skip to tech tips" href="#tech">Just want the tech tips without the intro?</a>]</p>
<p>One of our clients, <a title="Rent Collectors" href="http://www.rentcollectors.ie" target="_blank">Rent Collectors.ie</a> , appeared on the Dragon&#8217;s Den (<a title="Dragon's Den on Twitter" href="http://search.twitter.com/search?q=%23ddire">#ddire</a>) on 2 April 2009. This site is hosted on a single virtual private server (VPS) so it has limited memory, CPU and disk space. It runs a version of <a title="Ubuntu Linux Distribution" href="http://ubuntu.com" target="_blank">Ubuntu</a>, <a title="The PHP Project" href="http://php.net" target="_blank">PHP 5.2.3</a>, <a title="Apache HTTP Server" href="http://apache.org" target="_blank">Apache 2.2.4</a> and a <a title="MySQL Open Source Database server" href="http://mysql.com" target="_blank">MySQL 5.0.45</a> (The famous LAMP stack). Those are the stable versions with the operating system version we are running and in order to keep consistency with dependency management, we stuck to it but you should always use the latest stable versions.</p>
<p>RentCollectors is a rather simple service with a good few database interactions and a complex but well designed backend for multiple agents from anywhere in the country to work with. Prior to that day, extreme performances weren&#8217;t a concern. Developing with caching in mind, making the site scalable if it has to be scaled, making sure it could hold high loads was more like a theory we applied to the architecture when developing it.</p>
<p>So what happened? Well a few days before the television show, the client warned us that that website would be under high pressure as it was going to be on national television later that week. I decided to make sure that the server, considering it&#8217;s hardware capabilities, would hold the load of being publicised on TV.</p>
<p>I started by benchmarking the website with Apache Benchmark to find out how much it could take. Turned out to be handling about 35 pages per second. That made my <a title="How to build your own Bat Phone" href="http://www.millionaireplayboy.com/toys/batphone.php">bat phone ring</a>. 35 requests per second is all but acceptable for a website - that is going to be on national television. So knowing that we hadn&#8217;t turned on different levels of caching I realized that in a rather short amount of time - meaning that no rewrite was needed, I managed to bump the server to up to 185 requests per seconds without hogging the server (and still being able to access the website from other computers from other networks). 185 requests per second is not extreme, far from it, but for a single vps server, being to handle the load was the most important things. We could have simply plugged in a few other webservers and scale horizontally/out but I was convinced that we could hold the load with that single server. It was a challenge and a test.</p>
<h2>Vertically Scaling a single VPS</h2>
<p><a name="tech"></a>So instead of scaling out, we decided to scale vertically/up. That basically involves adding CPU or RAM (memory) to a single system. What we did is we created more daemon processes to Apache and added more reserved(spare) child threads. This gave us the buffer we needed as the load was going up.</p>
<p>Interesting, but then we decided to change the MySQL settings to be able to cache more database results. That way, the MySQL query cache wouldn&#8217;t have to do all the work when the exact same queries would be executed. Before you go on your server and increase the query cache on your MySQL servers, please make sure that you know what you are doing. Having the query cache enabled and running is good when the tables do no change too often. This is good for webpages that generate content pages, navigations, footers, etc. If a table gets modified, the cache gets flushed. One way or the other, before you start using the MySQL query cache, I suggest you read <a title="MySQL Query Cache Documentation" href="http://short.ie/mysql-query-cache" target="_blank">the mysql documentation about it</a> and in more general terms you should also read <a title="MySQL Performance Blog High Performance MySQL" href="http://www.mysqlperformanceblog.com" target="_blank">the MySQL Performance Blog</a>, you will most likely learn new things about MySQL and performance.</p>
<p>So after optimizing the MySQL settings we realized that each public page were highly unlikely to be modified during the television show. So we took a  different direction and as well as caching at the MySQL level, we thought that if we could rule out the MySQL server completely, it could be even better.</p>
<p>So we setup <a title="Advanced PHP Cache" href="http://php.net/apc" target="_blank">APC</a> and used our EcholibreApc class. We first used APC to cache the bytecode and then we set the apc.stat setting to &#8220;0&#8243;. <a title="APC Stat" href="http://short.ie/php-apc-stat" target="_blank">apc.stat</a> is set to &#8220;1&#8243; by default and this means that each time a cached script is requested, it verifies if the cache has changed then returns the cached version. By making apc.stat set to &#8220;0&#8243; we skip that step meaning that it doesn&#8217;t try to see if the cached entry has been modified. Beware! It&#8217;s much faster but requires you to restart your webserver if you make a change to a PHP file and you want it to take effect. APC runs by default with the &#8220;apc.shm_size&#8221; set to &#8220;30&#8243;. This means that APC will be using 30 megs of your shared memory. Just remember that memory is cheap and it&#8217;s easy to add, in our case we made the shared memory size bigger in order to make sure that we could store all what we needed. So after all this, we only had the database working every 30 minutes. Which was reducing the response time and server load/memory usage considerably.</p>
<p>Here&#8217;s the very simple EcholibreApc class &#8212; APC utility &#8212; in case you are interested or want to use it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Echolibre Apc Class
 *
 * A very very simplistic class that acts as a layer to the
 * advanced PHP Cache.
 *
 * One of the reasons of this is that APC doesn't store arrays
 * that are more than &quot;1&quot; level deep. This class wraps around
 * json_encode and json_decode. If you want to know why we use
 * json* instead of serialize well you should run your own micro-
 * benchmarks. You could be surprised ;-)
 *
 * @author  David Coallier
 * @see     http://php.net/apc
 * @version 0.1.2
 * @license New BSD
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> EcholibreApc
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * Fetch a variable
     *
     * This static method will fetch a value
     * from the apc cache and either return
     * false if the variable doesn't exist in the
     * cache or a json_decoded array/object.
     *
     * @param  string $name   The name of the variable to fetch from the cache
     * @return mixed  Either bool false or a json_decoded object.
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> fetch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> apc_fetch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> json_decode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Store a variable
     *
     * This static method will store a json_encoded string
     * in the apc cache for the amount of time supplied by
     * the third argument.
     *
     * @param string $name  The name of the variable to store
     * @param mixed  $value The mixed variable to store in the cache
     * @param string $time  The time length to store the variable in cache.
     * @return void
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> store<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3600</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        apc_store<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> json_encode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Delete a variable
     *
     * This static method will delete a variable
     * from the apc cache.
     *
     * @param string $name  The name of the variable to delete from the cache.
     * @return void
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> delete<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        apc_delete<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So after doing the Apache tweaks, MySQL tweaks and the APC/PHP tweaks, we simply made sure the sessions were not read and wrote to disk by making sessions saved to /dev/shm (shared memory) and that was it for backend changes.</p>
<h2>Final Precautions</h2>
<p>We looked at what else could be done in order to improve the user experience. Helgi Þormar Þorbjörnsson and his expertise on frontend caching came in very handy as we decided to cache more data on the user&#8217;s browser, GZIP encode the content being sent to the browser, reduce the footprint of the javascripts and CSS stylesheets. So after compacting the multiple javascript files we had and running a few stress tests, we ended up with a website and a server that was in very good conditions and most likely ready to be on television.</p>
<p>The decisive night arrived, the Dragon&#8217;s Den was on television, our turn came, we started seeing connections to port 80 (netstat &#8211;nat | grep :80 | wc -l) going up, and from a few servers around the world I was still able to access the site as if no-one was online. about 15-20 minutes later, the connection count was gone back to the usual and no sign of downtime whatsoever. We had made it, without a single glitch. Dragon&#8217;s Den is on at 22h00 and after this test, it was time to sleep. A very peaceful night, much more peacefully than the number of connections rising on the webserver <img src='http://blog.echolibre.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.echolibre.com/2009/04/surviving-the-dragons-den-vertical-scaling/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
