Design a Consistent Hashing & Sharding Service
Design the ring-based consistent hashing algorithm that powers DynamoDB and Cassandra — covering virtual nodes, rebalancing cost, hotspot prevention, and when to use jump hash instead.
The problem with modular hashing
Modular hashing is seductive in its simplicity: shard = hash(key) % n. Hash the key to an integer, take the modulus of the node count, and you have a perfectly balanced mapping. For a static cluster this works fine. The problem arrives the moment n changes.
When you add a single node to a 100-node cluster, n changes from 100 to 101. Every key whose hash(key) % 100 != hash(key) % 101 — which is approximately 99% of all keys — now maps to a different shard. For a distributed cache, this is catastrophic: the entire working set is simultaneously invalidated, and every request falls through to the origin database. If your cache was absorbing 95% of traffic, you just fired a full-load SQL volley at a database provisioned for 5% of that traffic. This pattern has a name in post-mortems: the re-shard storm.
The same problem manifests for a distributed key-value store or a sharded database. Removing a node (whether planned for scale-down or unplanned due to failure) means that the data those keys pointed to must be migrated across the cluster — and if you don’t have a clean mapping of “this key was on shard 7, now it belongs on shard 3”, you have a consistency problem, not just a performance problem. The migration has to be coordinated, and during migration, reads can return stale or missing data.
Consistent hashing was introduced precisely to break this dependency between key assignment and cluster size. The insight: instead of computing hash(key) % n, map both keys and nodes onto the same abstract ring and use proximity — “which node is clockwise-nearest to this key?” — as the assignment function. Now adding or removing a single node only affects the keys in the arc immediately preceding it on the ring, leaving all other assignments untouched.
Clarifying questions
Before sketching any architecture, lock down these eight questions. Each one gates a significant design decision:
| Question | Ideal answer | Why it matters |
|---|---|---|
| Are nodes homogeneous or heterogeneous? | Heterogeneous (different RAM/CPU) | Determines vnode count per node — equal vnodes on unequal hardware causes load imbalance |
| What is the read/write ratio and consistency requirement? | Read-heavy with eventual consistency | Drives quorum configuration: lower R for read perf, lower W for write perf |
| What replication factor? | 3 (standard for HA) | N=3 is the DynamoDB/Cassandra default; fewer means less fault tolerance, more means write amplification |
| Do nodes need stable names or IDs? | Yes, nodes are named (e.g., redis-1.us-east) | Named nodes → consistent hashing ring; anonymous sequential IDs → jump hash is viable |
| Is the key space known upfront? | No — keys are user-generated | Rules out range-based sharding; consistent hashing handles arbitrary hash distributions |
| How often do nodes join and leave? | Infrequently (planned scaling events, not seconds) | Frequent churn means the ring is constantly rebalancing; at very high churn, Chord-style DHT routing may be preferred |
| Is this cross-datacenter? | Multi-region active-active | Adds rack/zone awareness to replica placement — you never want all 3 replicas in the same AZ |
| What is being sharded? | Distributed cache, KV store, or database partition | Cache tolerates key movement (cache miss); database does not — data must be physically migrated |
Functional requirements
getNode(key)— return the primary node responsible for a given key inO(log n)timegetReplicaNodes(key, count)— return the nextcountdistinct physical nodes clockwise from the key’s hash position (for replication)addNode(nodeInfo)— add a node to the ring, distributing its vnodes across the ring; only keys in the predecessor arc migrateremoveNode(nodeId)— gracefully remove a node, transferring its key range to the successor node- Ring state must be consistent across all clients querying the same ring instance; ring updates must be serializable
- Support heterogeneous node weights: a node with
weight=2receives 2× as many vnodes as a node withweight=1
Non-functional requirements
- Lookup latency:
O(log n)per key lookup using a sorted ring structure — for 15,000 vnode positions this is ~14 comparisons, sub-microsecond in memory - Load balance: even distribution within ±10% of the mean across all physical nodes, sustained across the full key distribution
- Key movement on topology change: adding or removing one node should move at most
O(k/n)keys — ideally exactly1/nof the keyspace - Replication placement: replica nodes must be distinct physical nodes (not just distinct vnodes) and should span availability zones when possible
- Ring state convergence: after a topology change, all clients should observe the updated ring within a configurable gossip TTL (typically < 5 seconds in Cassandra’s implementation)
Capacity estimation
API design
The hashing service exposes four operations. Keep the surface minimal — callers should never need to know internal ring mechanics:
// Core lookup — returns the primary node for a given key
NodeInfo getNode(String key)
// Replica lookup — returns count distinct physical nodes, in clockwise order
List<NodeInfo> getReplicaNodes(String key, int count)
// Topology mutation — adds a node with an optional weight (default 1)
void addNode(NodeInfo node, int weight)
// Topology mutation — removes a node by its stable ID
void removeNode(String nodeId)
NodeInfo is a plain value type: { id: String, host: String, port: int, datacenter: String, rack: String }. The datacenter and rack fields drive zone-aware replica placement. Clients poll for topology changes via a gossip protocol or subscribe to a configuration store (ZooKeeper, etcd) — the hash ring itself is stateless given a node list.
Data model
The ring is represented as a TreeMap<Long, NodeInfo> — a red-black tree keyed by hash position. This gives O(log n) insertion, deletion, and range search. The critical operation — “find the first node clockwise from position p” — maps directly to TreeMap.ceilingKey(p), with a wrap-around fallback to firstKey() when p exceeds all positions.
Ring (TreeMap<Long, NodeInfo>):
Position 0x0A3F... → NodeInfo { id="node-A", host="10.0.1.1", ... }
Position 0x1C22... → NodeInfo { id="node-B", host="10.0.1.2", ... }
Position 0x2B91... → NodeInfo { id="node-A", host="10.0.1.1", ... } ← vnode #2 of A
Position 0x3E4D... → NodeInfo { id="node-C", host="10.0.1.3", ... }
... (15,000 entries for 100 nodes × 150 vnodes)
Virtual nodes are stored as separate entries in the same TreeMap. The vnode key is computed as hash(nodeId + "#" + vnodeIndex) — this spreads each node’s positions uniformly across the ring, rather than clustering them, which is what produces the statistical load balance guarantee.
High-level architecture
Deep dives
Ring construction and lookup
Hash function selection is the first decision. MD5 and SHA-1 produce good uniform distributions but are slower than purpose-built non-cryptographic hashes. MurmurHash3 (128-bit output) is the production choice: it runs at ~2 GB/s on modern hardware, has excellent avalanche properties (small input changes → large output changes), and produces near-uniform bucket distributions. xxHash is faster still and used in newer systems. Avoid CRC32 for key distribution — it has structural patterns that cluster certain inputs.
For vnode placement, hash the string nodeId + "#" + vnodeIndex. The # separator prevents collisions between a node "node1" with vnode index 2 and a node "node12" with vnode index "". Each resulting hash is stored as a long (64 bits, though most implementations use only 32 bits to keep ring positions within [0, 2^32)).
Lookup proceeds in three steps:
- Hash the key:
long pos = hash(key) - Find the ceiling entry:
SortedMap<Long, N> tail = ring.tailMap(pos) - Wrap around if at the end of the ring:
tail.isEmpty() ? ring.firstKey() : tail.firstKey()
The TreeMap.tailMap(pos) call returns a live view of all entries with keys >= pos. Taking firstKey() on that view returns the nearest clockwise neighbor. This is O(log n) in the number of vnode positions.
Virtual nodes
Without virtual nodes, a 3-node ring has only 3 positions. Any non-uniform key distribution will cause one node to receive dramatically more traffic than others. More concretely: if keys hash uniformly but positions are fixed, the arc sizes between adjacent nodes follow an exponential distribution — the largest arc is expected to be O(log n) times larger than the average. At 3 nodes, that imbalance is severe.
Adding 150 vnodes per physical node changes the mathematics. With 450 total ring positions for 3 nodes, the central limit theorem kicks in: the total arc owned by any physical node converges toward the mean. The standard deviation of load across nodes scales as 1/sqrt(v) where v is the vnode count per node — 150 vnodes gives ~8× better balance than 2 vnodes.
Heterogeneous hardware gets proportional allocation: a node with 2× the RAM and CPU gets vnodeCount = 300 instead of 150. It receives 2× the key space, 2× the read/write traffic, and 2× the storage — exactly proportional to its capacity.
The cost of vnodes is rebalancing granularity: when a node joins, it only takes over the specific arcs where its vnodes land. If those arcs happen to cluster in one part of the ring (statistically unlikely with 150 vnodes but possible), the load may not rebalance as evenly as expected in the short term. This self-corrects as more vnodes are added.
Replication and consistency
Each key is assigned to a preference list of N nodes: the primary (first clockwise node) and the next N-1 distinct physical nodes clockwise. “Distinct physical nodes” is critical — you must skip vnodes that belong to already-selected physical nodes to ensure replication spans actual failure domains.
DynamoDB’s default is N=3, R=2, W=2:
R + W > Nguarantees at least one node is in both the read and write quorum — ensuring reads always see the latest write in the common case- Allows 1 node failure without losing read or write availability
- A write to 2-of-3 nodes is acknowledged; the third is updated asynchronously
For zone-aware placement, filter the preference list to ensure replicas span at least 2 availability zones. Cassandra’s NetworkTopologyStrategy implements this by separating the preference list into per-datacenter sub-lists and placing replicationFactor/datacenters replicas in each.
Consistency levels are caller-controlled at query time. A read with QUORUM reads from R nodes and returns the value with the highest version (vector clock or timestamp). A read with LOCAL_QUORUM only counts replicas in the local datacenter — useful for latency-sensitive reads that tolerate brief cross-DC inconsistency.
Jump hash
Lamping and Veach’s jump hash (2014) is a radical simplification: given a 64-bit key and a bucket count, it produces the correct bucket assignment in O(log n) iterations (empirically ~log2(n) jumps), uses zero memory beyond two integer registers, and achieves perfect load balance.
The algorithm is strikingly short:
static int jumpHash(long key, int numBuckets) {
long b = -1, j = 0;
while (j < numBuckets) {
b = j;
key = key * 2862933555777941757L + 1;
j = (long) ((b + 1) * (double) (1L << 31) / (double) ((key >>> 33) + 1));
}
return (int) b;
}
Jump hash has three hard constraints that make it unsuitable for many production use cases:
- Sequential numeric node IDs required: nodes are numbered
0, 1, ..., n-1. There are no named nodes. You cannot say “this key must go to the node at IP10.0.1.7” — you only know “this key goes to bucket 12”. - No replication groups: the algorithm returns a single bucket. Getting the next
Rbuckets requires running the algorithmRtimes with modified parameters — and the resulting groups have no topological guarantees (zone awareness, rack awareness). - Node removal is add-only in spirit: removing a node from the middle of the range (not the last added) requires renaming buckets, which reshuffles keys. Jump hash is designed for append-only growth with occasional removal of the highest-numbered bucket.
Use jump hash for: stateless request routing in homogeneous fleets (e.g., splitting analytics writes across identical Kafka partitions), batch job partition assignment, or anywhere you need perfect balance without the overhead of maintaining a ring data structure in memory.
Use consistent hashing for: any system where nodes have identities, where replication requires topological placement, or where the cluster undergoes arbitrary add/remove operations.
Java integration
Part A — Consistent hash ring implementation
package dev.pushkar.hashing;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
public final class ConsistentHashRing<N> {
private final SortedMap<Long, N> ring = new TreeMap<>();
private final int vnodeCount;
public ConsistentHashRing(int vnodeCount) {
this.vnodeCount = vnodeCount;
}
public void addNode(N node) {
for (int i = 0; i < vnodeCount; i++) {
long pos = hash(node.toString() + "#" + i);
ring.put(pos, node);
}
}
public void removeNode(N node) {
for (int i = 0; i < vnodeCount; i++) {
long pos = hash(node.toString() + "#" + i);
ring.remove(pos);
}
}
public N getNode(String key) {
if (ring.isEmpty()) throw new IllegalStateException("Ring is empty");
long pos = hash(key);
SortedMap<Long, N> tail = ring.tailMap(pos);
// wrap around the ring if no node clockwise
Long nodePos = tail.isEmpty() ? ring.firstKey() : tail.firstKey();
return ring.get(nodePos);
}
public List<N> getReplicaNodes(String key, int count) {
var result = new ArrayList<N>();
long pos = hash(key);
// iterate clockwise, deduplicate physical nodes
var seen = new HashSet<N>();
for (var entry : ring.tailMap(pos).entrySet()) {
if (seen.add(entry.getValue())) result.add(entry.getValue());
if (result.size() == count) return result;
}
// wrap around
for (var entry : ring.entrySet()) {
if (result.size() == count) break;
if (seen.add(entry.getValue())) result.add(entry.getValue());
}
return result;
}
private static long hash(String key) {
try {
var md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest(key.getBytes());
// Use first 8 bytes as a long
long h = 0;
for (int i = 0; i < 8; i++) h = (h << 8) | (digest[i] & 0xFFL);
return h;
} catch (Exception e) { throw new RuntimeException(e); }
}
}
The addNode / removeNode pair is O(v log(nv)) where v is the vnode count — 150 TreeMap insertions or deletions at O(log 15000) ≈ 14 comparisons each. Topology changes are fast even at high vnode counts. The getReplicaNodes method iterates the ring’s tailMap view — a live, zero-copy submap backed by the same TreeMap — so it allocates only the result list.
Note the HashSet<N> in getReplicaNodes: it deduplicates by physical node identity. This ensures that even if a physical node has three consecutive vnodes, it only appears once in the replica list. The N type must implement equals and hashCode correctly — if N is a String (node address), this is automatic; if it is a custom NodeInfo object, define those methods explicitly.
Part B — Spring Boot shard routing service
@Service
public class ShardRouter {
private final ConsistentHashRing<String> ring;
private final Map<String, JedisPool> shardPools;
public ShardRouter(@Value("${shards}") List<String> shardAddresses) {
this.ring = new ConsistentHashRing<>(150);
this.shardPools = new HashMap<>();
for (String addr : shardAddresses) {
ring.addNode(addr);
shardPools.put(addr, new JedisPool(addr.split(":")[0],
Integer.parseInt(addr.split(":")[1])));
}
}
public String get(String key) {
String shard = ring.getNode(key);
try (Jedis j = shardPools.get(shard).getResource()) {
return j.get(key);
}
}
public void set(String key, String value) {
for (String replica : ring.getReplicaNodes(key, 3)) {
try (Jedis j = shardPools.get(replica).getResource()) {
j.set(key, value);
}
}
}
}
In production, the set method’s sequential write to 3 replicas should be parallelized with a CompletableFuture.allOf and a quorum check — wait for 2-of-3 to succeed rather than blocking on all three serially. The third write completing asynchronously cuts p99 write latency significantly since the slowest replica no longer blocks the response.
The shards property is read from application configuration (e.g., application.yml) and should be backed by a dynamic source: Consul, ZooKeeper, or AWS Parameter Store. When a new shard is added to the config, a @RefreshScope bean restart (Spring Cloud) rebuilds the ring with the new node, adding only its 150 vnode positions and moving only the affected key arc.
Sharding strategy comparison
Sharding strategy tradeoffs
| Strategy | Key movement on resize | Load balance | Named nodes | Use case |
|---|---|---|---|---|
Modular hashing (key % n) | O(k) — nearly all keys | Perfect (mathematical) | Yes | Never at scale — use only for static, never-changing clusters |
| Consistent hashing (ring) | O(k/n) — only affected arc | Good with vnodes (±10%) | Yes | Cache, KV store, DB sharding — the production default |
| Jump hash | O(k/n) — same asymptotic | Perfect (exact) | No — sequential IDs only | Stateless partitioning, Kafka topic routing, homogeneous fleets |
| Rendezvous hashing | O(k/n) | Good | Yes | Peer-to-peer routing, CDN cache selection, no central ring needed |
Rendezvous hashing (highest random weight) deserves a note: each node scores HRW(node, key) and the highest scorer wins. No ring data structure required — any client with the node list can compute the same result independently. The tradeoff is O(n) per lookup instead of O(log n), which becomes expensive at node counts above a few hundred.
Curveballs
One node is 4× more powerful than the others. How do you configure the ring to give it proportionally more load?
Assign it 4× the vnode count. If the standard allocation is 150 vnodes per node, the high-capacity node gets 600 vnodes. Its 600 ring positions collectively own 4× the arc length (in expectation), meaning it receives 4× the key requests and is responsible for 4× the storage. No changes are needed to the routing logic — the ring arithmetic handles it automatically. The only operational concern is that rebalancing this node (adding or removing it) moves 4× as many keys as a standard node, so plan maintenance windows accordingly.
A node leaves the ring unexpectedly (crash). Before it's removed from the ring, clients still route to it and get errors. How do you handle the transition?
There are three layers of defense. First, the client SDK implements a retry with failover: on connection timeout to the primary node, immediately retry the next clockwise node in the preference list (the first replica). This is the same list returned by getReplicaNodes — clients should always hold the full preference list, not just the primary. Second, the health-checking layer (a gossip protocol or a heartbeat monitor) detects the node failure and removes it from the ring within the detection timeout (typically 5–30 seconds depending on the heartbeat interval). Third, hinted handoff: writes that were intended for the failed node are stored temporarily by the coordinator with a hint, and replayed to the recovering node once it rejoins. DynamoDB uses all three mechanisms. The key insight for the interviewer: there is an unavoidable window between crash and ring removal where the primary route fails — the replica failover in the client SDK is what keeps availability above 99.99% during that window.
You are using consistent hashing for a database (not just a cache). A key rebalances to a new node. How do you migrate the data without downtime?
The two-phase ownership model: the new node joins with shadow ownership of its arc — it is in the preference list but not yet the primary. Reads still go to the old primary with the new node as a replica-in-training. A background streaming job reads every key in the arc from the old primary and writes it to the new node (this is Cassandra’s streaming phase during nodetool move). During streaming, the new node returns misses for keys not yet migrated; the coordinator falls back to the old primary (normal replica fallback behavior). Once streaming is complete (verified by comparing key counts), the new node is promoted to primary and the old node relinquishes the arc. The old node continues to serve as a replica for the transferred arc until the next replication-factor enforcement cycle. Total downtime: zero, at the cost of temporary write amplification (keys written to both nodes during the migration window) and the streaming bandwidth.
Further reading
- Dynamo: Amazon’s Highly Available Key-value Store — The 2007 SOSP paper that popularized consistent hashing with virtual nodes, preference lists, and sloppy quorums in production. Section 4.2 covers the partitioning algorithm in precise detail.
- Chord: A Scalable Peer-to-peer Lookup Service for Internet Applications — Stoica et al., SIGCOMM 2001. The foundational DHT paper that formalized the ring structure and
O(log n)distributed lookup using finger tables. - A Fast, Minimal Memory, Consistent Hash Algorithm — Lamping and Veach, 2014. The two-page jump hash paper. Read the derivation — it is a masterclass in probabilistic reasoning from first principles.
- Apache Cassandra Architecture: Data Distribution — Cassandra’s implementation of Dynamo-style consistent hashing, including
num_tokens(vnodes),NetworkTopologyStrategy, and how gossip propagates ring state across the cluster.