Fast writes, slow reads

Brion Vibber brion@pobox.com
Sun, 10 Aug 2003 03:32:23 -0700


This is a multi-part message in MIME format.
--------------040400050004020103050504
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

I'm finding that reading data back from memcached is surprisingly slow. 
I've probably done something wrong. :)

I wrote a simple loop that reads 500 integers or strings, adding each 
one to the cache if it's not already in. The first run, which checks for 
500 uncached items and adds 500 items to the cache, runs fast enough; 
more or less a second.

The second run, which fetches back the set values, consistently takes 
about 50 seconds, about a tenth of a second per fetched item. 1000 items 
take 100 seconds, 50 items take 5 seconds.

Since I expect to do a lot more reading than writing, this has me a 
little worried! I'm getting this same slow read speed from the PHP and 
Perl clients, and both talking to a local server on the BSD box or over 
the network to the Linux box.

Moving the Perl client from the 2 GHz Athlon (BSD) to the 266 MHz 
Pentium II (Linux), oddly enough my speeds increase a bit: it takes a 
mere 20 seconds to fetch 500 items (though the check + add sequence goes 
up to 2 seconds). It doesn't seem to make a difference whether it's 
talking to the local or the remote server.

Any ideas? I've attached the Perl version of the test script; I'm using 
the 1.0.6 MemCachedClient... Perl 5.6.1 is installed on the FreeBSD box, 
5.8.0 on the Linux box.

-- brion vibber (brion @ pobox.com)

--------------040400050004020103050504
Content-Type: text/plain;
 name="mctest.pl"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="mctest.pl"

#!/usr/bin/perl

#  memcached -p 11000

use MemCachedClient;

$wgMemc = new MemCachedClient {
	'servers' => [ "127.0.0.1:11000" ],
#	'servers' => [ "10.0.0.1:11000" ],
#	'servers' => [ "10.0.0.2:11000" ],
	'debug' => 0
};

$max = 500;
print "Grab $max integers:\n";

$time_start = time();

@arr = [];
for($i=0; $i<$max; $i++) {
	$n = $wgMemc->get( $key = "test:intarray:$i" );
	if(!$n) {
		print "* $i not in cache, adding\n";
		$n = $i;
		$wgMemc->add( $key, $n );
	} else {
		print "* $n found\n";
	}
	$arr[$i] = $n;
}

$time_end = time();
$time = $time_end - $time_start;

print "Retrieved $max ints in $time secs.\n\n";



print "Grab $max strings:\n";

$time_start = time();

@arr = [];
for($i=0; $i<$max; $i++) {
	$n = $wgMemc->get( $key = "test:stringarray:$i" );
	if(!$n) {
		print "* $i not in cache, adding\n";
		$n = "this is the ${i}th string";
		$wgMemc->add( $key, $n );
	} else {
		print "* $n found\n";
	}
	$arr[$i] = $n;
}

$time_end = time();
$time = $time_end - $time_start;

print "Retrieved $max strings in $time secs.\n\n";

--------------040400050004020103050504--