Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /share/CACHEDEV1_DATA/Web/www/libraries/UBBcode/text_parser.class.php on line 228
Memcache: the solution to all performance problems

Comments Blog About Development Research Sites

The solution to all performance problems

Jan 5, 2008
Well, at least webserver-wise it would seem. Currently I am, as many might know, working on a huge international travel site. The point is that the site should be available in 7 languages.

The implication for this might take a bit to sink in: there is this HUGE site with literally thousends of little texts (every link, every button, everything has text) and all of those need to be translateable. On the fly.

Initially I wrote a system that parsed all templates recursively and extracted the translatable parts, placed them in a nice list and added a nifty ajax interface to edit them. Worked fine, apart from this minor problem: texts were only available on the page you defined them and many, many texts occured at dozens of locations.

So a second system came to be, which simply lets you define a component and a key for each textlabel. The component can be the same troughout pages so all texts are no longer grouped by location but instead by catagory. Disadventage: there is no way of knowing what text was required for each page, so we ended up by loading all text labels and values for a certain language each pageview (alternative was a query for each textlabel and with labelcounts per page reaching into several dozens that did not seem like a good idea to us either).

Long story short, we now had a query that took almost 30% of the entire parsetime to complete that loaded over a thousend textkeys. Not good.

So, after a discussion on GoT I decided to look at alternatives - one in particular, the ability to store all textlabels and values directly into the servers RAM and read only those we needed every pageview.

The results were nothing short of amazing! Instead of wasting 30% of our parsetime on a singlequery the new system only took 2% parstime in total (including the parsing of textvalues after they were loaded). Page generation time decreased from 0.97s to 0.66 seconds - which may not sound like a lot, but memcache is a nonblocking system, its performance does not noticibly decrease with load like the SQL server does. Already under testing loads we saw huge increases in page generation time to the point were even with only 10 visitors online the page would feel sluggish, a situation likely to deteriorate under the increasing loads we were expecting.

Now, on to the more technical part of this post: how does that all work? It's quite simple really: Memcache uses a memory-cache server, very much like an SQL server, that you can connect to and use to store information. In this case I opted to store key-value combinations, with each key concatenated to its locale so we'd get combinations like backButton_en_US / 'Back' - stored directly into the servers' ram! And because the cache server is a daemon that runs in the background, all we needed to do was get all the translations (much like we already did anyway) and instead of assigning them to a local array that gets destroyed each time we store them persistantly in memory so they stay available forever (or until their expiration time is met of course, a value easily set).

Codewise it's even simpler then using an SQL database:
Code (php) (nieuw venster):
    $this -> memcache  = new Memcache;
    $this -> memcache -> connect('localhost', 11211);

     // Setting a variable:
    $this -> memcache -> set($id . '_' . $locale, $value);

    // Retrieving a variable:
    $this -> memcache -> get($id . '_' . $locale);

As I said, it's basicly one huge array of key-value pairs you can put data in and retrieve data from at any time. Even on other pages, or other servers if you fancy it!

For more information, including setting up a memcache server read the PHP manual for it - it's quite self explanatory really. Sources can be downloaded from The official memcache site, binaries for windows can be found here altough the last versions there are quite out of date.

FragFrog out!

New comment

Your name: