Newbie question: memcached slow the first time a value is read?

Tim Strehle tim at digicol.de
Wed Dec 7 11:27:02 UTC 2005


Hi,

I'm a first-time memcached user, so this is probably a dumb question... 
Anyway.

It seems to me that fetching an entry from memcached (using the PHP PECL 
memache module) is relatively slow for the first access, then fast (40 
times faster) for subsequent accesses.

Can the memcached data be trusted to actually be in RAM (not swapped 
out)? Is there any other explanation for previously-fetched keys to be 
returned fourty times faster than random ones?

Here's a more detailed explanation of what I'm trying to use memcached for:

- We have indexed 2.5 million documents with a full-text search engine. 
Search results (document IDs) are returned fast by the search engine, 
but fetching metadata for the first 500 found documents (from a 
PostgreSQL database) is slow.

- So I installed memcached (memcached-1.1.12 with libevent-1.1a on SuSE 
Linux 9.3, kernel version 2.6.11.4-21.9-bigsmp, single Xeon 2.4 GHz with 
3 GB RAM) and the PHP PECL memcache extension (memcache-1.5 on PHP 
4.4.0), started memcached with 1.5 GB RAM ("memcached -d -u digicol -m 
1536 -M -l 127.0.0.1 -p 11211") and ran a PHP script to fill it with the 
metatdata for those 2.5 million documents.

I understand from "top" that my PHP script wrote 1145 MB of data into 
memcached:

========================================================================
Tasks:  73 total,   1 running,  72 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0% us,  0.2% sy,  0.0% ni, 99.3% id,  0.5% wa,  0.0% hi,  0.0% si
Mem:   3111540k total,  1868280k used,  1243260k free,   833312k buffers
Swap:  2104472k total,   479964k used,  1624508k free,    86204k cached

   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND 

13278 digicol   16   0 1145m 739m  468 S  0.0 24.4   2:38.86 memcached
========================================================================

- Now my PHP code does 500 fetches from memcached:

   $memcache = memcache_connect('localhost', 11211);
   foreach ($ids as $id)
     $str = $memcache->get('PREFIX:' . $id);

Fetching 500 random entries takes 2.0 seconds on average (which sounds 
way too slow for RAM access).

But fetching the same 500 entries later (immediately, or with a couple 
of other fetches in the meantime) just takes 0.05 seconds (average).

Note that I don't have a fallback to the database, so these are just 
memcached reads. And memcached always returns the correct data. So I 
don't think I'm doing something dumb - the measured time should really 
be the time memcached needs.

I was kind of surprised by these numbers, I had assumed that memcached 
reads would always be fast... (Before trying memcached, we tried GDBM 
files on Linux ramdisks, which did work - it was constantly quite fast. 
But using memcached sounded a lot easier.)

Any help would be greatly appreciated.

Thanks,
Tim

-- 
------------------------------------------------------------
Tim Strehle                           http://tim.digicol.de/
Digital Collections                   http://www.digicol.de/
------------------------------------------------------------



More information about the memcached mailing list