Ketama: Consistent Hashing

libketama is a consistent hashing library written in C. You supply a list of servers and ketama will map keys to servers in a consistent way, even after adding or removing servers from the list.

It is a generic library, but was initially written to replace how our memcached clients mapped keys to servers.
Previously, clients mapped keys->servers like this:

 server = serverlist[hash(key)%serverlist.length];
This meant that whenever we added or removed servers from the pool, everything hashed to different servers, which effectively wiped the entire cache.

Ketama solves this problem in the following way:

If you then add or remove a server from the list, only a small proportion of keys end up mapping to different servers.

Implementation details

The server list is stored in a file, whenever the file mtime is changed, libketama will rebuild the continuum data structure. This structure is stored in shared memory and used every time you lookup the mapping for a key. You can have all your machines using memcache pull down the latest copy of the file periodically (if it changed) and libketama will rebuild the continuum automatically.

See the README and test scripts for examples and additional information.

Source Code

Latest code is in our public svn repo here:

libketama is BSD licensed, the php extension uses the php license, and the java code is TODO(same as the official java client).


Consistent Hashing and Random Trees: Distributed Caching Protocols for Relieving Hot Spots on the World Wide Web
Web Caching with Consistent Hashing