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.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

hash_ring-1.1-py2.5.egg (12.6 kB view details)

Uploaded Egg

File details

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

File metadata

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

File hashes

Hashes for hash_ring-1.1.tar.gz
Algorithm Hash digest
SHA256 b444dea3927f5abe5a470f726d27d90d9b712e6981b9b3f3cfd4bb183a425fa5
MD5 e059a1ddca398bb24c33621b4f44269b
BLAKE2b-256 8161f45b974bc39c6dc16039c4b096ed1ec4532bbe87dd7d3d3c5fed13bf62dc

See more details on using hashes here.

File details

Details for the file hash_ring-1.1-py2.5.egg.

File metadata

  • Download URL: hash_ring-1.1-py2.5.egg
  • Upload date:
  • Size: 12.6 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for hash_ring-1.1-py2.5.egg
Algorithm Hash digest
SHA256 2659782c0cb061f962250f04b0230602ad8feb4c398c645e3d1e97bf74d312b8
MD5 644a0935a85e2a4ed0537b222082550c
BLAKE2b-256 f926e6a45f1826aeb643a4e6ba377f0586b22eedafefd3ec75c07dd5c77c6d2d

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