Fri, 26 Jun 2009
Consistent hashing and memcached, all you should know
Consistent hashing is a hash discipline to avoid some errors in traditional hashing - like modulus. Some memcached clients uses these techniques, perhaps libketama. With modulus hashing - key % n_servers - you may have problems when some server goes down. If you change the constant n_servers all of your keys will be mapped to other servers, this behavior can has disastrous consequences perhaps a lot of misses in a small fraction of time. Consistent hashing is trying to "address" this situation. Libketama implementation create one vector of integers where each integer is assigned to unique host. To matching some key to this vector you need convert this key in one integer and look up the most close integer host.
vector = [2 -> host a , 10 -> host b, 32 -> host a, 54 -> host b]
hash_key = f_hash("foo") <-- 7 perhaps
host = lookup(vector, hash_key) <-- host b
vector = [2 -> host a , 32 -> host a]
hash_key = f_hash("foo") <-- 7 perhaps
host = lookup(vector, hash_key) <-- host a
/*
host a has 2 Gb = 50% of keys
host b has 1 Gb = 25% of keys
host c has 1 GB = 25% of keys
*/
vector = [2 -> host a , 10 -> host b, 16 -> host c, 32 -> host a]
hash_key = f_hash("bar") <-- 7 perhaps
host = lookup(vector, hash_key) <-- host b
hash_key = f_hash("foo") <-- 12 perhaps
host = lookup(vector, hash_key) <-- host c
/*
host a has 2 Gb = 66% of keys
host c has 1 Gb = 33% of keys
*/
vector = [2 -> host a , 14 -> host a, 16 -> host c, 32 -> host a]
hash_key = f_hash("bar") <-- 7 perhaps
host = lookup(vector, hash_key) <-- host a
hash_key = f_hash("foo") <-- 12 perhaps
host = lookup(vector, hash_key) <-- host a
Posted at: 19:15 | category: /memcached | Comments (0)