System Design Interview
Consistent Hashing
A key technique for building scalable distributed systems. Learn how consistent hashing works and why it's superior to traditional hashing in dynamic environments.
Example: Consistent Hashing Ring in Python
A simplified implementation of a consistent hashing ring. This demonstrates how nodes and keys are placed on a ring and how to find the correct node for a given key.
import hashlib
import bisect
class ConsistentHashing:
def __init__(self):
self.ring = {}
self.sorted_keys = []
def _hash(self, key):
return int(hashlib.md5(key.encode()).hexdigest(), 16)
def add_node(self, node):
key = self._hash(node)
self.ring[key] = node
bisect.insort(self.sorted_keys, key)
def get_node(self, string_key):
key = self._hash(string_key)
idx = bisect.bisect_left(self.sorted_keys, key)
if idx == len(self.sorted_keys):
idx = 0
return self.ring[self.sorted_keys[idx]]
Our AI Coach can help you explore advanced concepts like virtual nodes (replicas) to ensure a more even distribution of keys and load across your servers.
Related System Design Guides
Master more system design concepts with AI-powered preparation