What is Consistent Hashing?
Consistent hashing is a technique for distributing data across a changing set of nodes such that when nodes are added or removed, only a minimal fraction of data needs to move. It was introduced by David Karger et al. (MIT) in 1997 and became the foundation of nearly every large-scale distributed storage system built since — Dynamo, Cassandra, Redis Cluster, Akamai’s CDN, and Memcached client libraries all use variants of the same idea.
§2The Problem: Naive Modulo Hashing
The simplest way to assign a key to a server is server = hash(key) % N, where N is the server count. This works perfectly when N is fixed. But in any distributed system, servers fail and new capacity is added constantly. Every time N changes, almost every key remaps to a different server. With 100 servers and 1 million keys, removing one server forces ~990,000 keys to migrate — a full data reshuffle that creates a thundering herd across the cluster.
How the Ring Works
Consistent hashing maps both servers and keys onto a circular hash space. Servers are placed at positions on the ring by hashing their identifiers. Keys are hashed to positions on the ring and are owned by the first server encountered when moving clockwise from the key’s position. This tool visualizes the hash space as 0–359° for clarity; production systems use the full 0–2³²−1 range.
When a server is removed, only the keys between that server and its counter-clockwise neighbor need to migrate — they go to the next clockwise server. When a server is added, it only takes keys from one neighbor. In both cases the number of moving keys is K / N on average — the mathematical minimum. For 1 million keys and 100 servers, adding or removing one server moves ~10,000 keys, not ~990,000.
§4Virtual Nodes Deep Dive
With a small number of servers and basic ring placement, load is uneven by chance — a handful of random points on a circle create unequal arc lengths, meaning some servers own far more of the key space than others. Virtual nodes solve this by giving each physical server multiple positions on the ring, spreading its ownership across many small non-contiguous segments.
Each physical server S gets V virtual node identifiers — S-vn-0, S-vn-1, …, S-vn-V — each hashed to a different ring position. The assignment rule is unchanged: go clockwise to the nearest VN. The result: each server’s load is the sum of V small arc segments spread around the ring, and the statistical variance in load shrinks roughly as 1/√V.
How many virtual nodes?
- 1 VN/server: severe imbalance; standard deviation of load can exceed 30–40% of the mean
- 10 VN/server: acceptable balance for small clusters (3–5 servers)
- 100–150 VN/server: Cassandra’s pre-3.0 default; good for stable-size clusters
- 150–256 VN/server: modern Cassandra default; enables smooth incremental scaling
The visualizer defaults to 3 VNs/server to make the imbalance problem visible before improvement. Try sliding from 1 to 16 with 40+ keys and 3 servers to see the effect clearly — the imbalance badge goes from red to green.
§5Algorithm & Complexity
Ring lookup: finding the server for a key
- Hash the key to a ring position: O(1)
- Binary search the sorted VN list for the successor: O(log(N × V))
- Total: O(log(N × V)) — effectively O(log N) with fixed V
Adding a server
- Generate V new VN positions via hashing: O(V)
- Insert V positions into sorted VN list: O(V log(N × V))
- Identify keys to transfer: O(K/N) expected — only one neighbor’s keys
Removing a server
- Remove V VN positions from sorted list: O(V log(N × V))
- Orphaned keys absorbed by successor VNs: O(K/N) expected key movement
Interview Q&A — 15 Questions
Drawn from actual system design and distributed systems interviews at Google, Meta, Amazon, Uber, Stripe, and DoorDash. Prepping seriously? Grokking the System Design Interview (Educative) covers these in depth.
Q1What problem does consistent hashing solve?
Q2Walk me through how a key is assigned to a server in a consistent hash ring.
Q3What are virtual nodes and why are they needed?
Q4What happens to keys when a server goes down?
Q5What is the time complexity of key lookup?
Q6How does Amazon Dynamo use consistent hashing?
Q7How does Apache Cassandra use consistent hashing differently from Dynamo?
Q8What are the tradeoffs of increasing virtual node count?
Q9How would you handle heterogeneous servers — some with 2× the capacity?
Q10How does Redis Cluster implement sharding — is it consistent hashing?
Q11What is rendezvous hashing and how does it compare to consistent hashing?
Q12What is Jump Consistent Hashing?
Q13How does consistent hashing handle hot keys / skewed access patterns?
Q14What is bounded load consistent hashing?
Q15Design a distributed cache handling 10 million req/s across 100 servers using consistent hashing.
Real-World Systems
Amazon Dynamo (2007)
The paper that put consistent hashing on every engineer’s required reading list. Dynamo is Amazon’s highly available key-value store powering the shopping cart, session state, and product catalog. It uses a ring with Q/S virtual nodes per server. Dynamo’s insight: eventual consistency + consistent hashing + quorum replication gives better availability than a strongly consistent system under network partitions.
Apache Cassandra
Cassandra’s vnodes are the production-grade evolution of Dynamo’s ring. Each node gets 256 random tokens by default. The replication strategy walks the ring clockwise, placing replicas on different physical racks. Adding a node triggers token range transfers from 256 different neighbors simultaneously — scale-out is smooth and balanced without manual rebalancing operations.
Redis Cluster
Redis Cluster uses 16,384 fixed hash slots — a discrete ring. Each slot maps via CRC16(key) % 16384 and is assigned to a primary node with one or more replicas. The fixed slot count keeps client routing tables compact: a 16,384-bit bitmap per node is enough. Adding a node means migrating specific slots, which Redis Cluster handles online with MIGRATE commands.
Akamai CDN
Akamai was one of the first commercial deployments of consistent hashing — several of the algorithm’s inventors (Karger et al., MIT) co-founded the company. Content is distributed across edge nodes using ring-based assignment that minimizes cache misses when nodes join or leave. At 350k+ servers across 1,700+ PoPs, the K/N migration property is not academic — a full reshuffle at this scale would be catastrophic.
Nginx Upstream Hashing
Nginx’s hash $request_uri consistent directive uses consistent hashing (Ketama implementation) for upstream proxy load balancing. When a backend server is removed from the pool, only its fraction of traffic redistributes — preventing a cache stampede on the remaining servers that would occur with plain round-robin or modulo hashing.
Follow-Up Concepts
The Chord DHT
Chord (Stoica et al., 2001) is a peer-to-peer lookup protocol built on consistent hashing. Each node maintains a finger table of O(log N) entries pointing to nodes at exponentially increasing ring distances. This gives O(log N) lookup with no central directory — queries hop from node to node, each hop halving the remaining ring distance.
Ketama
Ketama is the consistent hashing implementation popularized by the memcached community (Last.fm, 2007). It uses MD5 to generate 40 VN positions per server. The Ketama ring format is the de facto standard for consistent hashing in memcached client libraries across Ruby, Python, Go, and Java.
CAP Theorem Angle
Systems using consistent hashing (Dynamo, Cassandra) tend to favor AP (Available + Partition-tolerant) over CP. Ring-based replication means reads and writes continue even when some replicas are unreachable — at the cost of potentially stale reads. This is the tradeoff to articulate when an interviewer asks ‘Cassandra or MySQL for this use case?’ — not throughput numbers, but the consistency model.
Consistent Hashing with Bounded Loads
Google’s 2017 extension (Mirrokni et al.) adds a capacity constraint to the ring: no server holds more than (1+ε) × (K/N) keys. Incoming keys that would exceed a server’s bound are redirected to the next clockwise server under its bound. Guarantees near-perfect balance while maintaining minimal key movement on topology changes.