Consistent Hashing

Consistent Hashing

Tech

Consistent Hashing

Simple hashing

Problems of simple hashing function key % n (n is the number of servers):

  • It is not horizontally scalable. Whenever a new cache host is added to the system, all existing mappings are broken.
  • It may not be load balanced, especially for non-uniformly distributed data. Some servers will become hot spots.

Consistent Hashing

  • Consistent hashing maps a key to an integer.
  • Imagine that the integers in the range are placed on a ring such that the values are wrapped around.
  • Given a list of servers, hash them to integers in the range.
  • To map a key to a server:
    • Hash it to a single integer.
    • Move clockwise on the ring until finding the first cache it encounters.
  • When the hash table is resized (a server is added or deleted), only k/n keys need to be remapped (k is the total number of keys, and n is the total number of servers).
  • To handle hot spots, add “virtual replicas” for caches.
    • Instead of mapping each cache to a single point on the ring, map it to multiple points on the ring (replicas). This way, each cache is associated with multiple portions of the ring.
    • If the hash function is “mixes well,” as the number of replicas increases, the keys will be more balanced.

About the Author

Software Engineer

Post a Comment

Enter your comments here...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
Site is Blocked
Sorry! This site is not available in your country.