Home > memcached, oss > Ever wished for more Cache per Node?

Ever wished for more Cache per Node?

December 31st, 2008

Ever wished that you could pack more data into a certain memcached instance without scaling up? AKA, reduce the need to scale-out, therefore save money on your infrastructure cost and electricity?

How about saving 8 bytes for every item that you cache? meaning if you cache 10 million small items, you save around 76 megabytes of RAM. Guess what? you can with the upcoming 1.3 series! and the code that achieves this by Trond Norbye is now merged into the 1.3 development tree.

I must mention though that this saving is only available to those that do not use the CAS feature. Keep reading to understand why…

The Magic

In the current release of memcached, an 8 byte value (uint64_t) is hardwired to the item structure which is now optional by specifying it at startup (the new ‘-C’ option). In the code level, the easiest way to describe it is that the CAS value is moved/aligned to the tail of the structure on memory. Needless to say, we know whether that 8 byte value exists or not from the startup option.

Observation

So, here is a very modest test that sets one-hundred-thousand items (unless there is a key clash…).

Firstly the Perl script:

#!/usr/bin/perl
 
use strict;
use warnings;
 
use Cache::Memcached;
use constant {
    NITEMS => 100000,
    KLEN   => 8,
    VLEN   => 16,
};
 
sub random_key {
    my @chars = ('a'..'z', 'A'..'Z', '0'..'9');
    my $buf = "";
 
    foreach (1..KLEN) {
        $buf .= $chars[rand @chars];
    }
    return $buf;
}
 
my $memc = Cache::Memcached->new ({
    servers => ['localhost:11211'],
    debug   => 0,
});
 
my $val = 'a' x VLEN;
 
for (1..NITEMS) {
    $memc->set(random_key, $val);
}

Running this script against an instance with standard growth factor:

 memcached -m 1024 -d

produced the following statistics on number of items and bytes consumed:

...
STAT bytes 9000000
STAT curr_items 100000
STAT total_items 100000
STAT evictions 0
END

Similarly, running the above script against an instance with the new option (‘-C’):

memcached -m 1024 -C -d

had produced the following statistics:

...
STAT bytes 8200000
STAT curr_items 100000
STAT total_items 100000
STAT evictions 0
END

Thats a saving of 781 kilobytes batman!

This is great news for those that cache lots of small objects. For example, at mixi.jp we are probably the largest user of memcached in Japan and turns out the granularity of our cache is darn fine. This patch is going to hit us big.

++ to Domas for groaning… I mean indicating this improvement opportunity and Trond for hacking it :)

Toru Maesaka memcached, oss

  1. No comments yet.
  1. No trackbacks yet.