PHP, Python Consistent Hashing

I found out the hashing algorithm used in PHP-Memcache is different from that of Python-Memcache. The keys went to different servers as the hash created by python and php were different.

I posted a question on the memcache groups and was lucky to find this wonderful reply.

import memcache
import binascii
m = memcache.Client(['192.168.28.7:11211', '192.168.28.8:11211
', '192.168.28.9:11211'])

def php_hash(key):
    return (binascii.crc32(key) >> 16) & 0x7fff

for i in range(30):
       key = 'key' + str(i)
       a = m.get((php_hash(key), key))
       print i, a

This is the only thing that has to be done on Python’s end, change the way the hash is calculated. The coding on PHP end remains same. All you guys using PHP for web based front-end with MySQL and Python for back-end scripts shall find this helpful.

Thanks Brian Rue.

Reference: http://groups.google.com/group/memcached/msg/7bb75a026c44ec43

2 thoughts on “PHP, Python Consistent Hashing

  1. Thanks for this easy solution! If you want to make it even easier, set the php_hash function as the default using this easy assignment:

    memcache.serverHashFunction = php_hash

    Then you won’t have to refer to it again!

  2. i’d tried it, but it don’t helpd me.
    i’d set by php and:

    >>> import sys; print(‘%s %s’ % (sys.executable or sys.platform, sys.version))
    /usr/bin/python2.6 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
    [GCC 4.4.5]
    >>> import memcache
    >>> m=memcache.Client(['127.0.0.1:11211'], debug=0)
    >>> import binascii
    >>> def php_hash(key):
    … return (binascii.crc32(key) >> 16) & 0x7fff

    >>> m.get((php_hash(‘mfd.php_nexttime’), ‘mfd.php_nexttime’))
    Traceback (most recent call last):
    File ““, line 1, in
    File “/usr/lib/pymodules/python2.6/memcache.py”, line 779, in get
    return self._get(‘get’, key)
    File “/usr/lib/pymodules/python2.6/memcache.py”, line 766, in _get
    value = self._recv_value(server, flags, rlen)
    File “/usr/lib/pymodules/python2.6/memcache.py”, line 915, in _recv_value
    return val
    UnboundLocalError: local variable ‘val’ referenced before assignment
    >>>

    have you some ideas or advice for me?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>