In my recent talk to Big Data London I said that memcached can’t count. Turns out that’s not really true, and the problem lies somewhere else.
Here’s the original evidence memcached has counting issues, using Python & pylibmc:
In : cache.set('wibble', 0) In : cache.get('wibble') Out: 0 In : cache.decr('wibble', 1) Out: 0L In : cache.incr('wibble', 1) Out: 1L In : cache.incr('wibble', -1) Out: 4294967296L
Incrementing by -1 has resulted in memcached incrementing by 2^32-1 – a classic signed/unsigned conversion problem.
But hang on – according to the memcached spec, incr takes unsigned 64bit integers – and it’s an error to send a signed number.
Sure enough, using the ASCII protocol (with memcached 1.4.9):
get wibble VALUE wibble 0 1 1 END incr wibble -1 CLIENT_ERROR invalid numeric delta argument
It seems memcached can count after all, or at least refuse to do operations it might get wrong. It’s another question why memcached won’t deal with signed numbers…
The good news? Both bugs are now fixed in the latest github version, so very soon memcached and pylibmc will be able to count. Three cheers – hooray, hooray!