Consistent Hashing
When a distributed cache or key-value store spreads keys across N servers, the routing rule needs two properties: every client must map a key to the same server without querying a central directory, and adding or losing a server must move as few keys as possible. Consistent hashing achieves both by mapping servers and keys onto a fixed circular hash space, introduced by David Karger and colleagues in their 1997 paper "Consistent Hashing and Random Trees."
The word "consistent" here refers to stable key-to-server assignment across topology changes, not replica consistency or ACID consistency.
The Problem with Modulo Hashing
The simplest way to route a key across N servers is server = hash(key) mod N. While N stays fixed, every lookup for a given key lands on the same server.
Once a server joins or crashes, N changes and the remainder changes for most keys. Adding a 4th server to a 3-server cluster remaps , and adding a 10th server to a 9-server cluster remaps .
In a distributed cache, remapping most keys at once triggers a wave of cache misses that slams the backing database. In a partitioned store, it forces copying almost the entire dataset across the network. The root cause is coupling each key's position directly to N.
The Hash Ring
Consistent hashing decouples key placement from server count by fixing the hash space to a large constant range (such as 0 to 2^32 - 1 for a 32-bit hash) and connecting the end back to 0 to form a hash ring.
Both servers (hashed by node ID or IP address) and keys (hashed by primary key) map onto points around that circle. Each key belongs to the first server reached by walking clockwise from the key's position.
In code, a router does not walk around a circle one integer at a time. It stores the sorted ring positions of all servers in an array or balanced binary search tree and runs binary search in O(log M) time (where M is the number of points on the ring) to find the first server position greater than or equal to hash(key). If hash(key) is larger than the highest server position, the lookup wraps around to index 0.
Adding, Failing, and Balancing Servers with Virtual Nodes
Because each server owns only the arc between itself and its counter-clockwise neighbor, adding or removing a server remaps only that single arc—roughly —while every other key stays untouched.
Placing each physical server at only one point on the ring still leaves two weaknesses:
- Uneven key ranges: With few servers, random hashes rarely land evenly spaced, so one server can end up owning far more keys than another.
- Single-neighbor failover spike: When
Server Cfails, 100% of its arc shifts ontoServer Aalone, risking a cascading overload.
Virtual nodes (also called vnodes) fix both weaknesses by placing each physical server at V points around the ring (for example, hashing Server A#1, Server A#2, …, Server A#V). Each physical server owns V small interleaved arcs around the circle instead of one large contiguous slice:
- Even load distribution: More points per server shrink arc-size variance, so every server holds close to an equal share of keys.
- Distributed failover: When
Server Cfails, itsVsmall arcs hand off across all surviving peers instead of overloading one neighbor. - Weighted hardware: A server with twice the RAM or CPU can be assigned twice as many virtual nodes to carry twice the share of keys.
Click ✕ Fail Server C in Basic (1 point / server) mode below to watch 100% of Server C's keys shift onto Server A. Then click ↺ Reset, switch to Virtual nodes (3 / server), and click ✕ Fail Server C again to see the colored arcs and failover spread across both Server A and Server B.
The trade-off for virtual nodes is a larger sorted routing table in router memory (N × V entries instead of N). Production systems such as Apache Cassandra and Amazon Dynamo typically use tens to a couple hundred virtual nodes per physical server, which keeps skew low while keeping binary search (O(log(N × V))) fast.
Replication on a Hash Ring
Consistent hashing pairs naturally with replication. To keep R replicas of a key (for example, R = 3), the coordinator walks clockwise from the key's hash and stores copies on the first R distinct physical servers.
When virtual nodes are enabled, walking clockwise can encounter two virtual nodes belonging to the same physical machine back to back (such as A#1 followed by A#2). The coordinator must skip duplicate physical servers so that two replicas never land on the same machine.
Limitations and Where Consistent Hashing Fits
Consistent hashing solves topology-change churn, but two limits remain:
- Hot keys still hit one server: Consistent hashing balances the number of distinct keys across servers, not skewed traffic on one viral key. A single popular key still routes to one server and requires caching, read replicas, or key salting.
- No range queries: Hashing scatters adjacent keys around the ring, so range queries must scatter-gather across all servers.
| Property | Consistent Hashing Ring | Fixed Hash-Slot Table (e.g., Redis Cluster) |
|---|---|---|
| Central coordinator needed | No (clients hash independently) | Yes (cluster tracks 16,384 slot owners) |
| Keys moved when adding a node | Roughly 1/N | Exact 1/N slots reassigned explicitly |
| Best fit | Stateless caches (Memcached, CDNs), leaderless KV stores (Cassandra, Dynamo) | Managed clusters with explicit slot migration control |
Key idea. Modulo hashing (
hash(key) mod N) couples key placement toNand remaps most keys on every cluster change. Consistent hashing places keys and servers on a fixed circular ring (O(log M)binary search clockwise) so adding or failing a server moves only about1/Nof keys. Virtual nodes split each server into many interleaved arcs to smooth load and spread failover across all surviving peers, and ring replication skips duplicate physical servers when walking clockwise to pickRreplicas.
Further reading
- Consistent Hashing and Random Trees — Karger et al. (1997) — the original paper introducing consistent hashing for distributed caching.
- Dynamo: Amazon's Highly Available Key-value Store — DeCandia et al. (2007) — production use of consistent hashing, virtual nodes, and clockwise distinct-host replication.
- Architecture and Token Allocation — Apache Cassandra Docs — how virtual nodes balance ring ownership in practice.