โ† Wiki Index / Computation & Algorithms / Consistent Hashing
๐Ÿ’ป Computation & Algorithms

Consistent Hashing

Consistent hashing is a distributed key-space partitioning scheme that maps both storage servers and data keys onto a circular identifier ring so adding or removing a node remaps only O(K/N) keys.

Consistent Hashing is a distributed hashing and key-space partitioning scheme in computation and algorithms and distributed systems that assigns K data keys across a dynamic cluster of N storage servers or cache nodes such that when a server joins or leaves the cluster, only an average of K / N keys must be relocated to a new server. Introduced in 1997 by David Karger, Eric Lehman, Tom Leighton, Rina Panigrahy, Matthew Levine, and Daniel Lewin at MIT to prevent hot-spot congestion across distributed World Wide Web proxy caches (and commercialized at Akamai Technologies), consistent hashing decouples key placement from the current cluster cardinality N.

In a traditional modular hash table, a key k is assigned to server index h(k) mod N. While modular hashing distributes keys uniformly when N is static, incrementing or decrementing N by 1 (due to node failure, elastic autoscaling, or rolling maintenance) changes (h(k) mod (N ยฑ 1)) for roughly (N โˆ’ 1) / N โ‰ˆ 99% of all keys, triggering a simultaneous cache-miss stampede across origin databases. Consistent hashing eliminates this Rehashing Storm by embedding both servers and keys into a fixed circular identifier space [0, 2m โˆ’ 1] using a uniform or cryptographic hash function, forming the partitioning backbone of distributed hash tables (Chord), distributed caches (Memcached, Couchbase), and cloud databases (Amazon Dynamo, Apache Cassandra, Riak) alongside vector clock versioning and Merkle tree anti-entropy repair.

Identifier Ring Architecture, Key Lookup, and Monotonicity

Mapping Nodes and Keys onto the Unit Circle (Hash Ring)

Consistent hashing selects a fixed, cluster-independent hash output range {0, 1, ..., 2m โˆ’ 1} (for example, m = 64 with MurmurHash3/xxHash or m = 128 with MD5) and bends this integer interval into a circular identifier ring (topologically equivalent to the unit circle S1 = [0, 1) where 2m wraps around to 0):

  1. Node Placement: Each storage server si (identified by its IP address, port, or node UUID) is hashed via h(si) โˆˆ [0, 2m โˆ’ 1] to a point on the ring.
  2. Key Placement: Each data item with key k is hashed using the same function h(k) โˆˆ [0, 2m โˆ’ 1] to a point on the same ring.
  3. Clockwise Successor Assignment: Key k is assigned to the first server s = succ(k) whose ring position h(s) is greater than or equal to h(k) moving clockwise around the ring (if h(k) exceeds the largest server position on the ring, it wraps around 0 to the server with the smallest position).

Because the ring positions of all N servers are stored in a sorted array or balanced binary search tree of size N (or N ยท V with V virtual nodes), any client or coordinator holding the cluster membership table resolves the owning server for key k in O(log N) time via lower-bound binary search, or in O(1) expected time by quantizing the ring into a fixed-size lookup table.

Minimal Disruption and Karger's Four Balance Properties

Karger et al. (1997) proved that consistent hashing satisfies four foundational properties across a set of N active views of the cluster:

  • Balance: With high probability, no server receives more than O((1/N) log N) fraction of the keys (improved to (1 + ฮต)/N using V = ฮ˜(log N) virtual nodes per server).
  • Monotonicity: When a new server snew is added to the ring, keys move only from existing servers to snew, and never between two existing servers.
  • Minimal Disruption (Smoothness): When a server si is added or removed, only the keys falling in the single arc directly preceding h(si) change ownershipโ€”an expected fraction of 1 / N of all keys (K / N keys in total).
  • Spread and Load: Even when different clients have slightly inconsistent views of which servers are alive, each key is replicated across only a small bounded number of servers.

Virtual Nodes (Vnodes), Replication, and Bounded Loads

Eliminating Arc-Length Variance with Virtual Nodes

If each physical server si is placed at only a single random point on the circle (V = 1), the spacings between N uniform random points on a circle follow anOrder-Statistic Dirichlet distribution where the maximum arc length is ฮ˜((log N) / N)โ€”meaning the unluckiest server receives up to O(log N) times the average load, and when one server fails, its entire arc merges into a single clockwise neighbor (doubling that neighbor's load).

To achieve tight load uniformity, consistent hashing assigns each physical server V distinct virtual nodes (vnodes) (also called tokens) by hashing suffixed identifiers h(si โˆฅ 1), h(si โˆฅ 2), ..., h(si โˆฅ V) across the ring:

  • By the Chernoff bound, using V = O((1 / ฮต2) log N) virtual nodes per physical server ensures that with high probability every server owns between (1 โˆ’ ฮต)/N and (1 + ฮต)/N of the ring circumference. In practice, V = 128 to 256 vnodes reduces the standard deviation of load imbalance across nodes to ฯƒ โ‰ˆ 1 / โˆšV โ‰ˆ 6%โ€“8%.
  • Fault Scattering: When a physical server fails, its V virtual arcs are scattered across up to V different clockwise neighbors, distributing the failover load evenly across the surviving cluster rather than overwhelming a single neighbor.
  • Heterogeneous Capacity Weighting: A server with twice the RAM or NVMe throughput can simply be assigned 2V virtual nodes to absorb twice the request share.

R-Way Replication and Consistent Hashing with Bounded Loads

In distributed databases such as Amazon Dynamo and Apache Cassandra, high availability requires replicating each key k across R distinct servers (typically R = 3). Starting from the coordinator node succ(k), the system walks clockwise around the ring and selects the first R vnodes belonging to distinct physical servers (and distinct failure domains/racks) as the key's preference list, using sloppy quorums (W + R > Nrep) or Paxos (computer science) for consistency.

Furthermore, to prevent hot keys (Zipfian popularity skew) from overloading a single node, Vahab Mirrokni, Mikkel Thorup, and Mikkel Zadimoghaddam (2018) introduced Consistent Hashing with Bounded Loads, which caps each server's capacity at โŒˆc (K/N)โŒ‰ (for c = 1 + ฮต, e.g., c = 1.25) and forwards any overflow keys clockwise to the next non-full server with provably O(1 / ฮต2) relocation overhead.

Partitioning SchemeLookup Time ComplexityState Space per ClientKeys Remapped on Node Join/LeaveLoad Variance Across NodesRepresentative Production Systems
Modular Hashing (h(k) mod N)O(1)O(1)(N โˆ’ 1)/N โ‰ˆ 100% of keysOptimal (O(1/โˆšK))Static sharded caches
Ring Consistent Hashing (V = 1)O(log N)O(N)1 / N of keysHigh (O(log N) max-to-mean ratio)Chord DHT (Stoica et al., 2001)
Vnode Consistent Hashing (V vnodes)O(log(NV))O(NV)1 / N of keys (scattered across V peers)Low (ฯƒ โ‰ˆ 1/โˆšV โ‰ˆ 6% at V = 256)Amazon Dynamo, Apache Cassandra, Riak
Rendezvous Hashing (HRW, 1998)O(N) (or O(log N) skeleton)O(N)1 / N of keysUniform (1/N exact expected share)Microsoft Cache Array Routing (CARP), Ceph CRUSH
Jump Consistent Hash (Lamping & Veach, 2014)O(ln N) arithmeticO(1) (zero memory)1 / N of keys (sequential buckets only)Uniform (1/N exact expected share)Google shard routers, ClickHouse

Frequently Asked Questions

How does Rendezvous (Highest Random Weight) hashing compare with ring consistent hashing?

Introduced by Thaler and Ravishankar in 1998, Rendezvous Hashing (HRW) avoids constructing a ring altogether: for a given key k, the client computes a combined hash score w(k, si) = h(k โˆฅ si) for every server si โˆˆ {1, ..., N} and routes k to the server with the maximum score arg maxi w(k, si). HRW achieves uniform 1/N load balance without virtual nodes and O(K/N) minimal disruption, at the cost of O(N) hash evaluations per lookup (making HRW ideal for small-to-medium cluster sizes N โ‰ค 100, whereas vnode ring hashing scales to thousands of nodes in O(log(NV)) lookup time).

What is Jump Consistent Hash, and what is its limitation?

Jump Consistent Hash (Lamping & Veach, 2014) is a 10-line stateless algorithm that maps a 64-bit key k to one of N buckets {0, 1, ..., N โˆ’ 1} using zero memory and O(ln N) pseudo-random floating-point jumps with zero load variance. Its only limitation is that buckets must be numbered sequentially from 0 to N โˆ’ 1, so only the highest-numbered bucket N โˆ’ 1 can be added or removed without remapping intermediate buckets.

How do distributed databases verify that replicas on a consistent hash ring remain synchronized after node joins or failures?

When virtual node boundaries shift or a temporarily unreachable replica recovers, each vnode range [ a, b ) maintains a hierarchical Merkle tree over the keys in that arc. Two replicas compare their root hashes in O(1) network messages and recursively traverse only the diverging subtree branches to stream missing keys in time proportional to the actual differences rather than the total dataset size.

References

  1. Karger, D., Lehman, E., Leighton, T., Panigrahy, R., Levine, M., & Lewin, D. (1997). "Consistent hashing and random trees: Distributed caching protocols for relieving hot spots on the World Wide Web." Proceedings of the Twenty-Ninth Annual ACM Symposium on Theory of Computing (STOC '97), 654โ€“663. https://doi.org/10.1145/258533.258660
  2. Stoica, I., Morris, R., Karger, D., Kaashoek, M. F., & Balakrishnan, H. (2001). "Chord: A scalable peer-to-peer lookup service for Internet applications." ACM SIGCOMM Computer Communication Review, 31(4), 149โ€“160. https://doi.org/10.1145/964723.383071
  3. DeCandia, G., Hastorun, D., Jampani, M., Kakulapati, G., Lakshman, A., Pilchin, A., Sivasubramanian, S., Vosshall, P., & Vogels, W. (2007). "Dynamo: Amazon's highly available key-value store." Proceedings of the 21st ACM SIGOPS Symposium on Operating Systems Principles (SOSP '07), 205โ€“220. https://doi.org/10.1145/1294261.1294281
  4. Lamping, J., & Veach, E. (2014). "A fast, minimal memory, consistent hash algorithm." arXiv preprint arXiv:1406.2294. https://doi.org/10.48550/arXiv.1406.2294
  5. Mirrokni, V., Thorup, M., & Zadimoghaddam, M. (2018). "Consistent hashing with bounded loads." Proceedings of the Twenty-Ninth Annual ACM-SIAM Symposium on Discrete Algorithms (SODA '18), 587โ€“604. https://doi.org/10.1137/1.9781611975031.39
๐Ÿงฌ Support Open Science
Explore interactive science games on LowKey Games or browse scientific apparel & accessories in the CRV Science Shop.
๐Ÿ’ก Spot an inaccuracy or have primary literature to expand this article?
Submit a citation or revision note using your shared CRV Science / LowKey Games profile ID.
Suggest Revision