Consistent Hashing

What is Consistent Hashing?

Consistent hashing is a distributed hashing scheme that operates independently of the number of servers or objects in a distributed hash table. It minimizes the number of keys that need to be redistributed when nodes are added or removed from the system.

Problem it Solves

In traditional hashing, when the number of servers changes, most data needs to be redistributed because the hash function depends on the total number of servers. Consistent hashing solves this by ensuring that only a small fraction of data needs to be moved when servers are added or removed.

How it Works

  • • Both servers and data keys are mapped to points on a circle (hash ring)
  • • Each key is assigned to the first server found clockwise from its position
  • • When a server is added, only keys between it and the previous server need to move
  • • When a server is removed, its keys move to the next server in the ring

Common Use Cases

  • Database Sharding: Distributing data across multiple database shards
  • Distributed Caching: Systems like Memcached and Redis clusters
  • Load Balancing: Distributing requests across servers
  • Content Delivery Networks: Routing requests to appropriate edge servers
  • Distributed Storage: Systems like Amazon DynamoDB and Cassandra

Advantages

  • • Minimal data movement when scaling up or down
  • • No single point of failure
  • • Easy to add or remove nodes
  • • Scalable and efficient