PHP Benchmark – Memcached with pecl-memcache & php-memcached , redis with Predis & Rediska
2010 April 27
Part 2: View Part 2
Update: 04/27/2010 – Scroll to bottom for benchmark which includes the almost unknown Libredis library for PHP.
I am new to using key-value memory caches, so today I decided to run some tests. I’ve searched the web quite a bit today (ok.. a LOT) and found some old benchmarks, but I wanted to run some modern benchmarks on the latest versions and see how it worked for my needs.
Test System:
Linux Kernel 2.6.27 x64
AMD Opteron(tm) Processor 246 HE
4GB RAM
PHP 5.3.0
I tested php-memcached using binary mode for protocol communication, but the “get” benchmark ended up taking twice as long as using ASCII mode. “set” was about the same with or without binary mode.
All commands sent to Predis and Rediska were streamlined, since adding a key-value entry and setting the expiration are 2 different commands, it sends them together in one packet to the server to speed it up.
pecl-memcached does not have the binary mode ability yet, and I am not certain if Redis uses binary mode or ASCII for communication, and found no information when I searched.
My rough and simple benchmark function is below. As you can see, this is only using a single client connection to the caching server. A concurrent benchmark post may follow this.
$this->_server is a class instance of the memcached / redis server. $value is a igbinary serialized MySQL row object, via mysql_fetch_object. For those who are interested, the row is from a user table with 11 fields.
———————————————————————————–
$query = “SELECT * FROM `user_table` WHERE `id`=1 LIMIT 1″;
$res = mysql_query($query);
$value = mysql_fetch_object($res);
$value = igbinary_serialize($value);
$expire = 120;
$key = md5($query);
$begin = microtime(true);
for ($i=0;$i<10000;$i++)
{
$this->_server->set($key.$i,$value,$expire); // different for different libraries, but you get the idea
}
$taken = microtime(true) - $begin;
$taken = number_format($taken,4);
print "set: $taken(s)<br />";
$begin = microtime(true);
for ($i=0;$i<10000;$i++)
{
$this->_server->get($key.$i); // different for different libraries, but you get the idea
}
$taken = microtime(true) - $begin; $taken = number_format($taken,4); print "get: $taken(s)<br />";
———————————————————————————–
Results (set/get time is an average of 3 runs):
Memcached 1.4.5 / pecl-memcache 3.0.4
set: 0.6888(s)
get: 0.6785(s)
get: 0.6785(s)
——————-
Memcached 1.4.5 / php-memcached 1.0.1 (using libmemcached-0.38)
set: 0.7047(s)
get: 0.7613(s)
get: 0.7613(s)
——————-
redis 1.2.6 / Predis v0.5.1
set: 2.2776(s)
get: 1.1201(s)
——————-
redis 1.2.6 / Rediska v0.4.2
set: 4.7263(s)
get: 3.1871(s)
——————-
redis 1.2.6 / libredis (http://github.com/toymachine/libredis) v2010-04-26
set: 0.8478(s)
get: 0.7880(s)
set: 0.8478(s)
get: 0.7880(s)
There you have it.. It looks like Memcached 1.4.5 / pecl-memcache 3.0.4 is the winner in this case. I should also note that I am looking at using key-value memory caching for sessions, and both Memcached modules allow this natively.
I was actually very disappointed to see the poor results from Redis. All of the benchmarks I have seen have blew away Memcached, but I guess that is why it is always good to test it yourself.
I would love to hear your thoughts, and your own experiences. If any of you want to try the test, the code above should be easy enough to work with.. All that needs to be changed is the get and set calls to work with the different modules and classes.
Update 04/27/2010 – As you can see above, I have added the benchmark for Libredis, which is quite comparable to Memcached, especially for a PHP extension that is brand new — good job to the author for that.
EZ Website Monitoring
7670 E Broadway Blvd Tucson, AZ, 85710 USA
info@ezwebsitemonitoring.com • 520-664-2324
7670 E Broadway Blvd Tucson, AZ, 85710 USA
info@ezwebsitemonitoring.com • 520-664-2324
3 Responses
leave one →




Hi Aaron,
unlike pecl-memcache and php-memcached (which are native C extensions for PHP), Predis and Rediska are pure-PHP libraries that do not depend on a single line of C code (well, aside from the core PHP functions), so IMHO they should not even be used to assess any performance difference between Redis and Memcached due to the obvious overhead of the PHP code. Furthermore Redis has a lot more to offer than Memcached, Redis is more like a data-structure server than a plain KV store.
PS: btw I’m the author of Predis
Thank you for pointing that out. I actually had come to that conclusion, but forgot to mention that PHP implementation without an underlying C extension will be slower due to the overhead with sockets and other factors. I really do like Redis, and wouldn’t mind using it instead of MySQL if it does scale well. The question I have is, if I am using it for a real database, and am using consistent hashing, how do I ensure fault tolerance if one server goes down. Is there a way to force it to always make 1 copy on 2 different servers? Good job on Predis btw.. I really do like the PHP implementation, just wish PHP was faster at using it.