<!-- Consistent Hashing: The Algorithm That Quietly Powers Half the Internet — by Bhagya Rana -->
<!-- Canonical: https://bhagyarana.in/blog/consistent-hashing-algorithm -->
---
title: "Consistent Hashing: The Algorithm That Quietly Powers Half the Internet"
publishedAt: "2026-01-26"
summary: "From DynamoDB to Discord, from Akamai to Cassandra — one algorithm decides where your data lives. Here's how consistent hashing works, why it's brilliant, and how Google improved it."
series: "Data & Storage"
---

Every time you load a webpage, send a message, or stream a video, an algorithm you've never heard of decides which server handles your request.

It's not round-robin. It's not random. It's **consistent hashing** — an algorithm invented in 1997 that became the backbone of virtually every distributed system built in the last two decades.

If you only understand one distributed systems algorithm, make it this one.

## The Problem: What Happens When Servers Change?

Imagine you have 4 cache servers and you use simple modular hashing to distribute keys:

```
server = hash(key) % num_servers

hash("user:alice")   = 7  → 7 % 4 = 3 → Server 3
hash("user:bob")     = 12 → 12 % 4 = 0 → Server 0
hash("user:charlie") = 15 → 15 % 4 = 3 → Server 3
hash("user:diana")   = 22 → 22 % 4 = 2 → Server 2
```

This works perfectly. Until a server goes down.

Now you have 3 servers:

```
hash("user:alice")   = 7  → 7 % 3 = 1 → Server 1  ← CHANGED
hash("user:bob")     = 12 → 12 % 3 = 0 → Server 0  ← same
hash("user:charlie") = 15 → 15 % 3 = 0 → Server 0  ← CHANGED
hash("user:diana")   = 22 → 22 % 3 = 1 → Server 1  ← CHANGED
```

**75% of keys now map to different servers.** Every remapped key is a cache miss. If you have a billion cached items and lose one server, 750 million requests simultaneously hit your database.

This is called the **rehashing problem**, and at scale, it causes cascading failures.

## The Insight: A Hash Ring

Consistent hashing solves this with an elegant geometric insight. Instead of mapping keys to servers with modular arithmetic, imagine arranging servers on a **circle** (ring):

```
                    0
                  /   \
               S1       S2
              /           \
            /               \
          S4                 S3
            \               /
              \           /
                \       /
                  \   /
                  2^32/2
```

Both keys and servers are hashed onto the same ring (positions 0 to 2^32 - 1). A key is assigned to the **first server clockwise from its position**.

```
Ring positions (simplified to 0-100):
  Server A at position 10
  Server B at position 35
  Server C at position 65
  Server D at position 90

Key "alice" hashes to position 20 → next server clockwise → Server B
Key "bob"   hashes to position 50 → next server clockwise → Server C
Key "charlie" hashes to 75       → next server clockwise → Server D
Key "diana"   hashes to 5        → next server clockwise → Server A
```

### What Happens When a Server Dies?

Remove Server C (position 65):

```
Key "alice" at 20  → next server: Server B (35) ← UNCHANGED
Key "bob"   at 50  → next server: Server D (90) ← CHANGED (was C)
Key "charlie" at 75 → next server: Server D (90) ← UNCHANGED
Key "diana" at 5   → next server: Server A (10) ← UNCHANGED
```

**Only keys that were assigned to Server C get remapped.** That's approximately 1/N of all keys (where N is the number of servers). Going from 4 to 3 servers only remaps ~25% of keys instead of ~75%.

This is the magic of consistent hashing: **adding or removing a server only affects the keys in its immediate neighborhood on the ring.**

## The Problem of Uneven Distribution

Basic consistent hashing has a flaw. With only 4 points on the ring, the distribution is likely uneven:

```
Uneven ring:
  Server A at 10  → responsible for range 91-10 (19% of ring)
  Server B at 35  → responsible for range 11-35 (25% of ring)
  Server C at 40  → responsible for range 36-40 (5% of ring)  ← barely used
  Server D at 90  → responsible for range 41-90 (51% of ring) ← overloaded
```

Server D handles 10x the traffic of Server C. This isn't acceptable.

### Virtual Nodes: The Solution

Instead of placing each server at one position on the ring, place it at **many positions** (virtual nodes):

```
Server A: positions [10, 120, 230, 340, ...]  (150 virtual nodes)
Server B: positions [35, 145, 255, 365, ...]  (150 virtual nodes)
Server C: positions [65, 175, 285, 395, ...]  (150 virtual nodes)
Server D: positions [90, 200, 310, 420, ...]  (150 virtual nodes)
```

With 150 virtual nodes per server, the distribution becomes nearly uniform. Each server handles approximately 25% (±5%) of the key space.

**How many virtual nodes?** In practice:
- 100-200 virtual nodes per server gives good balance
- DynamoDB uses 256 virtual nodes per partition
- Cassandra uses 256 tokens per node by default

More virtual nodes = better balance but more memory for the ring lookup table. At 256 nodes across 1,000 servers, the ring has 256,000 entries — trivially small.

## Consistent Hashing in Real Systems

### Amazon DynamoDB
DynamoDB uses consistent hashing to distribute data across partitions. When you write an item with a partition key, DynamoDB hashes the key, maps it to the ring, and routes the request to the responsible partition.

When traffic increases and a partition splits, only the data in that partition's range needs to move. The rest of the system is unaffected.

### Apache Cassandra
Cassandra's entire data distribution model is built on consistent hashing. Each node owns a range of tokens on the ring. When a new node joins the cluster:

```
Before:  Node A [0-50], Node B [51-100]

Node C joins at position 33:
After:   Node A [0-33], Node C [34-50], Node B [51-100]

Only data in range [34-50] transfers from Node A to Node C.
Nodes B is completely unaffected.
```

This is why Cassandra can scale horizontally by adding nodes with minimal data movement.

### Akamai CDN
Consistent hashing was literally invented for Akamai's CDN. When a user requests a webpage, the URL is hashed to determine which edge server should cache it. When edge servers go up and down (which happens constantly at CDN scale), only a fraction of cached content needs to be re-fetched.

### Discord
Discord uses consistent hashing to route users to the correct WebSocket server. When a server fails, only the users assigned to that server need to reconnect — everyone else maintains their connection.

## Google's Improvement: Jump Consistent Hashing

In 2014, Google published a paper on **Jump Consistent Hash** — an algorithm that achieves the same goals as ring-based consistent hashing but with:
- Zero memory usage (no ring structure to store)
- Perfect load balancing (exactly uniform distribution)
- Faster computation

The entire algorithm fits in 7 lines:

```cpp
int32_t JumpConsistentHash(uint64_t key, int32_t num_buckets) {
    int64_t b = -1, j = 0;
    while (j < num_buckets) {
        b = j;
        key = key * 2862933555777941757ULL + 1;
        j = (b + 1) * (double(1LL << 31) / double((key >> 33) + 1));
    }
    return b;
}
```

**How it works:** Given a key and the number of buckets, it deterministically computes which bucket the key belongs to. When you add a bucket (increase num_buckets), exactly 1/(num_buckets) of keys move to the new bucket. Perfect minimal disruption.

**The limitation:** Jump consistent hash only works when buckets are numbered sequentially and you only add/remove from the end. You can't remove bucket 3 out of 10 — you can only remove bucket 10. This makes it ideal for scenarios like adding shards but not for handling arbitrary server failures.

## Google's Subsetting Algorithm: Minimizing Connection Churn

Google also tackled a related problem: in a system with 1,000 clients and 100 servers, you don't want every client connected to every server. You want each client connected to a subset of servers, and when servers change, you want minimal connection disruption.

Their **deterministic subsetting** algorithm ensures:
- Each client connects to exactly K servers
- Load is balanced across all servers
- When a server fails, only the clients connected to it need to reconnect
- The reassignment is deterministic (no coordination needed)

This is consistent hashing applied to connection management rather than data distribution.

## Implementing It Yourself

Here's a production-quality consistent hash ring in under 50 lines:

```python
import hashlib
from bisect import bisect_right

class ConsistentHashRing:
    def __init__(self, virtual_nodes=150):
        self.virtual_nodes = virtual_nodes
        self.ring = {}          # hash -> server
        self.sorted_keys = []   # sorted hash values

    def _hash(self, key: str) -> int:
        return int(hashlib.md5(key.encode()).hexdigest(), 16)

    def add_server(self, server: str):
        for i in range(self.virtual_nodes):
            virtual_key = f"{server}:vn{i}"
            hash_val = self._hash(virtual_key)
            self.ring[hash_val] = server
            self.sorted_keys.append(hash_val)
        self.sorted_keys.sort()

    def remove_server(self, server: str):
        for i in range(self.virtual_nodes):
            virtual_key = f"{server}:vn{i}"
            hash_val = self._hash(virtual_key)
            del self.ring[hash_val]
            self.sorted_keys.remove(hash_val)

    def get_server(self, key: str) -> str:
        if not self.ring:
            return None
        hash_val = self._hash(key)
        idx = bisect_right(self.sorted_keys, hash_val)
        if idx == len(self.sorted_keys):
            idx = 0  # Wrap around the ring
        return self.ring[self.sorted_keys[idx]]
```

**Usage:**

```python
ring = ConsistentHashRing()
ring.add_server("cache-1")
ring.add_server("cache-2")
ring.add_server("cache-3")

# Route a key to a server
server = ring.get_server("user:alice")  # → "cache-2"

# Remove a server — only ~1/3 of keys remap
ring.remove_server("cache-2")
server = ring.get_server("user:alice")  # → "cache-3" (remapped)
server = ring.get_server("user:bob")    # → "cache-1" (unchanged if wasn't on cache-2)
```

## The Mental Model That Matters

Consistent hashing teaches a principle that applies far beyond load balancing:

**When the topology of your system changes, minimize the blast radius.**

This principle shows up everywhere:
- **Database sharding:** Add a shard without reshuffling all data
- **CDN routing:** Lose an edge server without invalidating all cached content
- **Service discovery:** A service going down shouldn't cascade
- **Feature flags:** Rolling out a feature to 5% of users consistently

Every time you design a system where components can come and go, ask yourself: "How much disruption does a single change cause?" If the answer is "proportional to the total system size," you probably need consistent hashing or something like it.

The algorithm is 28 years old. It's more relevant today than ever.

---

*This exploration draws from the original consistent hashing paper by Karger et al. (1997), Google's Jump Consistent Hash paper (2014), Amazon's DynamoDB design papers, and Apache Cassandra's architecture documentation.*
