Fastest languages for memcached
Written by Brian Moon, dealnews.com developer, on March 01, 2006
We are working on a new application here at dealnews (more on it soon) and decided to open our minds up to make sure we built the fastest application possible. So, I took a look at PHP (our standard language), Perl and Python. The application will be mostly working with memcached, so, that was the focus of my tests.
What is memcached?
"memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load."
All three languages provide an interface to memcached. The original client for memcached was written in Perl by the folks at Danga Interactive. The Python client is now maintained by a 3rd party group. The PHP client we use at dealnews (and the one recommended by most in the know) is available as a PECL package.
All test used openload with 20 concurrents and were allowed to reach 10,000 request or 90 seconds whichever came first. The server used a PIII-650 with 1GB or ram. It was freshly wiped with a new install of the latest Gentoo Linux on it. All applications were installed from Gentoo's portage if available.
Hello World Benchmarks
I started with a simple hello world script that did not use memcached. This gave me a good base for how each language performed.
Platform Req/s Base Memory -------------------------------------------- Apache+static 866.54 N/A Apache+mod_perl 668.80 3.2MB Apache+Python 458.88 2.8MB Apache+PHP+APC 454.70 4.6MB Apache+PHP 401.88 4.6MB
I used a static file here as a base. As you can see, it is quickest as expected. mod_perl does a great job here. mod_perl integrates at such a low level with Apache that it removes a lot of the overhead of the other other two. The addition of APC helped PHP some, but not really all that much. Its a pretty simple script.
Single page from memcached
The next test was a script that pulled a 212k cached HTML page from memcached and sent it back to the browser.
Platform Req/s ----------------------- Apache+PHP+APC 60.12 Apache+Python 54.83 ( varied wildly from 23 - 72) Apache+Perl 41.69
Well, this came as a bit of a shock. After the first test, I was thinking I would be knee deep in mod_perl for this project. This started to change my mind however. The next test showed more of the same.
Lots of sets and gets
These scripts made 100 sets to different keys and then fetched those same keys from memcached
Platform Req/s ----------------------- Apache+PHP+APC 38.14 Apache+Python 7.78 Apache+Perl 4.95
Just the gets
Lastly, I decided to test just the gets. The application we are making would be doing gets most of the time.
Platform Req/s ---------------------- Apache+PHP+APC 62.40 Apache+Python 13.79 Apache+Perl 6.75
This just widened the gap for PHP.
Reasons for this?
Going in to this, our team thought mod_perl would be the winner hands down. mod_perl is well integrated with Apache and the original memcached client was written in Perl. However, this is not the case.
The only reason we can come up with for these findings is that the PHP client is written in C as a PHP extension. The other two are written in Perl and Python. So, all the complex code has to be interpretted. For PHP however, most of the work happens in the engine, not in PHP code. This makes the PHP client far superior in performance to the other two.
So, after all that, we will still be using PHP for this application. That is good for us as we know PHP very well.
According to a post on the memcached list, the Perl client is being rewritten. I have not tested it. Reportedly, gets are faster with the new Perl client.
Here is the Perl, Python and PHP code I used: