Skip to main content

Implements consistent hashing in Python (using md5 as hashing function).

Project description

About hash_ring

Implements consistent hashing that can be used when the number of server nodes can increase or decrease (like in memcached). The hashing ring is built using the same algorithm as libketama.

Consistent hashing is a scheme that provides a hash table functionality in a way that the adding or removing of one slot does not significantly change the mapping of keys to slots.

More about hash_ring can be read in a blog post (that explains the idea in greater details):

More information about consistent hashing can be read in these articles:

There is also a wrapper MemcacheRing that extends python-memcache to use consistent hashing for key distribution.

Example

Basic example of usage (for managing memcached instances):

memcache_servers = ['192.168.0.246:11212',
                    '192.168.0.247:11212',
                    '192.168.0.249:11212']

ring = HashRing(memcache_servers)
server = ring.get_node('my_key')

Example using weights:

memcache_servers = ['192.168.0.246:11212',
                    '192.168.0.247:11212',
                    '192.168.0.249:11212']
weights = {
    '192.168.0.246:11212': 1,
    '192.168.0.247:11212': 2,
    '192.168.0.249:11212': 1
}

ring = HashRing(memcache_servers, weights)
server = ring.get_node('my_key')

How to use MemcacheRing:

from hash_ring import MemcacheRing
mc = MemcacheRing(['127.0.0.1:11212'])
mc.set('hello', 'world')
print mc.get('hello')

The code should be clean and simple. Feel free to concat the author if you detect bugs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hash_ring-1.3.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

hash_ring-1.3-py2.7.egg (8.2 kB view details)

Uploaded Egg

File details

Details for the file hash_ring-1.3.tar.gz.

File metadata

  • Download URL: hash_ring-1.3.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for hash_ring-1.3.tar.gz
Algorithm Hash digest
SHA256 f08830eae68ef6679edf2f444f2146d3fc606da17eda0aa4dde28a04090986f1
MD5 fc35b8de33d5d1374d1a77f075bf38b6
BLAKE2b-256 e6149ea746a5ee620ad844095ff2aa6275f9a03be9f4b95fa932ef681ef5f854

See more details on using hashes here.

File details

Details for the file hash_ring-1.3-py2.7.egg.

File metadata

  • Download URL: hash_ring-1.3-py2.7.egg
  • Upload date:
  • Size: 8.2 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for hash_ring-1.3-py2.7.egg
Algorithm Hash digest
SHA256 b03976c2547fab6e8306cf9926430a05580cdd7de37c8d443397721657312c3a
MD5 bb554145658fc04d57c52a5aedb13ac5
BLAKE2b-256 63f0ada04fb52f82d02793a3b9f6401b2ea2fc4d32d9945493e04102f93e4286

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page