# How do you add a cache node without moving every key?

By Vishal Kumar · Published 2026-11-10 · Updated 2026-11-13
Source: https://fungyaan.com/p/consistent-hashing
Tags: data-structures, system-design, caching, distributed-systems, go

**The verdict, which a million keys will prove:** don't pick a node with `hash(key) % N`. When N changes, almost every key maps to a different node: adding an 11th node to 10 moved **91%** of a million keys. For a cache, that means 91% misses at once, all landing on the database. Use a scheme that moves only the keys the new node should take, about 1/N of them:
- **consistent hashing** with virtual nodes;
- **rendezvous hashing**;
- **jump hash**;
- **a fixed number of slots** assigned to nodes, as Redis Cluster does.

The case opens with a good deed punished. Traffic grows, you add one more cache node to help, and the database immediately falls over. You added capacity, and somehow made things worse. This is one of those problems where a small change of perspective makes a big problem almost vanish. David Karger and colleagues' consistent hashing (1997) is the classic example, and it has a whole family of clever relatives. Let's line them up and measure them side by side.

The code is in [systems-data-structures](https://github.com/the-vishal-kumar/systems-data-structures/tree/main/hashring) (Go, standard library only), and `make demo` reruns every number.

> **Case file**
>
> | | |
> |---|---|
> | **The case** | You add one cache node and the database falls over |
> | **Suspects** | Mod N, consistent hashing ring, rendezvous, jump hash |
> | **Culprit** | `hash(key) % N`: 91% of keys move when N changes |
> | **The fix** | A scheme that moves only about 1/N of keys |
> | **Evidence** | 1,000,000 keys, Go standard library only, `make demo` |

## The four suspects

- **Mod N:** `node = hash(key) % N`. Perfectly even, and the culprit this post is about.
- **Consistent hashing ring:** hash every node to a few points on a circle; a key belongs to the first node point clockwise from the key's hash. Adding a node only takes over the arcs just before its points. More points per node ("virtual nodes") make the arcs more even.
- **Rendezvous (highest random weight):** every node scores the key with `hash(node, key)`; the highest score wins. Removing a node only moves the keys it was winning. No ring, but every lookup scores every node.
- **Jump hash** (Lamping and Veach, Google, 2014), which I think is one of the most charming algorithms around: a few lines of arithmetic that map a key to a bucket 0..N-1, moving the minimum when N grows. No memory at all, but buckets are numbers: you can add or remove only the *last* one.

## The evidence: a million keys

| | Mod N | Ring, 1 point/node | Ring, 100 points/node | Rendezvous | Jump hash |
|---|---:|---:|---:|---:|---:|
| **Keys moved, 10 → 11 nodes** (ideal 9.1%) | **90.9%** | 11.4% | 8.8% | 9.0% | 9.1% |
| …of which moved between old nodes | 81.8% | 0% | 0% | 0% | 0% |
| **Keys moved, one node of 10 removed** (ideal 10%) | **90.0%** | 0.2% | 8.5% | 10.1% | 10.0% (last node only) |
| …of which moved off nodes still up | 80.0% | 0% | 0% | 0% | 0% |
| **Busiest node vs average** (10 nodes) | 1.01× | **2.71×** | 1.13× | 1.01× | 1.00× |
| **Lookup, 10 nodes** | 25 ns | 45 ns | 76 ns | 63 ns | 50 ns |
| **Lookup, 1,000 nodes** | 26 ns | 76 ns | 161 ns | **2,226 ns** | 73 ns |
| **Memory** | none | N points | 100·N points | none | none |
| **Remove any node?** | yes (moves everything) | yes | yes | yes | **only the last** |

```mermaid
xychart-beta
  title "Busiest node vs average, 10 nodes (x)"
  x-axis ["Mod N", "Ring, 1 point", "Ring, 100 points", "Rendezvous", "Jump hash"]
  y-axis "times the average load"
  bar [1.01 , 2.71 , 1.13 , 1.01 , 1.00]
```

Three clues stand out:

- **Mod N doesn't only move the keys the new node needs; it reshuffles the old nodes too.** That's the smoking gun. Over 80% of keys moved between two nodes that both still existed. Every one of those is a cache miss for no reason.
- **A ring needs virtual nodes.** With one point per node, the busiest node got 2.71× the average load, and removing a node moved only 0.2% of keys, because that node happened to own a tiny arc. That low number is a red herring, not a virtue. With 100 points per node it was within 13% of even, and 1,000 points got within 4% (2.3% standard deviation). The price is memory and a slightly slower binary search.
- **Rendezvous is even and simple, but O(N).** At 10 nodes it's as fast as the ring; at 1,000 nodes, 2.2 µs per lookup. Fine for a handful of replicas, too slow for a big cluster on a hot path.

## Case 1: growing a cache cluster

**Situation:** 10 memcached or Redis nodes, each client picking the node itself, and traffic growing, so you add an 11th.

With mod N, about 91% of lookups miss right after the change, and the database takes nearly the full read load at once: the stampede from the [cache post](/p/cache-made-data-wrong), across the whole key space. With consistent hashing, about 9% miss, spread over the moments those keys are next requested.

**What I'd use:** a ring with 100-200 virtual nodes, which is what most memcached clients implement (the "ketama" scheme). If you run Redis Cluster, you don't choose: it splits keys into **16,384 fixed hash slots** (`CRC16(key) mod 16384`) and assigns slots to nodes. Adding a node moves some slots to it, so only the keys in those slots move. It's the same idea with a fixed number of partitions, which also makes rebalancing explicit and resumable. It trades a little cleverness for a lot of predictability, and I admire that trade.

## Case 2: sharded storage and stateful services

**Situation:** uploads, or per-tenant state, spread over storage nodes by key. Moving a key here means copying data, not only a cache miss.

Here, "which keys move" matters even more than "how many", because each move is a copy you have to plan (the [sharding post](/p/replicas-partitioning-sharding) covers the wider decision). Two common designs:
- **Fixed partitions:** many more partitions than nodes (Redis's slots; Kafka's partitions; "split into 1,024 shards, assign shards to machines"). Moving a partition is a bounded unit of work, and you can choose *which* partitions move.
- **Consistent hashing with virtual nodes**, as in the Dynamo paper (Amazon, 2007) and Cassandra. Cassandra 4.0 lowered its default from 256 to 16 tokens per node to cut repair and streaming costs, a reminder that more virtual nodes isn't free.

## Case 3: sticky routing at the load balancer

**Situation:** you want requests for the same user, tenant or session to reach the same backend (for an in-memory cache, or a WebSocket). When a backend is added or dies, you want only its users to move.

Load balancers ship this:
- nginx: `hash $key consistent;` (ketama);
- Envoy: `ring_hash`, and `maglev` (Google's 2016 scheme, built for fast lookup and even load);
- HAProxy: `hash-type consistent`.

For a handful of backends, rendezvous is also a fine choice.

## Case 4: numbered shards that only grow

**Situation:** data split into N numbered shards that you only ever add to (a time-ordered log, an append-only store).

**Jump hash** is ideal here: no memory, perfectly even, and minimal movement when N grows. It's the wrong choice for a cache cluster, where any node can fail: you can't remove node 3 of 10.

```mermaid
flowchart TD
  A{"Does your store already do it?"} -->|yes| B["Use it: Redis Cluster slots, client ring, load balancer"]
  A -->|no| C{"Can any node fail or leave?"}
  C -->|no, shards only grow| D["Jump hash"]
  C -->|yes| E{"Under about 20 nodes?"}
  E -->|yes| F["Rendezvous"]
  E -->|no| G["Ring with 100-200 virtual nodes, or fixed partitions"]
```

## The loose ends people forget

The algorithms are the easy part. The rest is the everyday work that makes them pay off.

- **Moving keys is only half the work.** Something must copy the data (storage) or absorb the misses (cache). Add capacity *before* you need it, and warm a new cache node, or route a share of traffic to it gradually.
- **Every client must agree on the membership list.** If half your app servers think there are 10 nodes and half think 11, the same key is read from two places. Distribute membership from one source: configuration, a service registry, or the cluster's own protocol.
- **Hot keys don't spread.** Consistent hashing balances *keys*, not *traffic*. One celebrity key still lands on one node; replicate it, or cache it in-process.
- **Weights:** a bigger node can take more virtual nodes (ring), or a weighted score (rendezvous).

> **Remember this**
> - `% N` moved 91% of keys for one extra node: consistent schemes moved 9%.
> - A ring needs virtual nodes, or one node takes 2.71× the average.
> - Consistent hashing balances keys, not traffic: hot keys still land on one node.

## What to do on Monday

1. **Use what your store already does:** Redis Cluster slots, your memcached client's ring, your load balancer's consistent hash. Don't write your own on the hot path.
2. **When you do choose:**
   - a ring with 100-200 virtual nodes for caches whose nodes can fail;
   - rendezvous for small sets (under ~20 nodes);
   - jump hash for numbered shards that only grow;
   - fixed partitions (many more than nodes) for storage you'll rebalance.
3. **Never `% N`** for anything whose N will change.

I still enjoy how the difference between 91% and 9% comes down to how you map a key to a node. A little geometry, or a little arithmetic, and a growing cluster stops being an emergency. For what happens when those misses do land at once, the [cache post](/p/cache-made-data-wrong) is the next case to read.

*Measured Nov 2026 with Go 1.27 on a laptop (Intel i7-8550U): 1,000,000 keys, 10 nodes (1,000 for lookup cost). Keys moved and balance are properties of the algorithms and repeat exactly; nanoseconds depend on the machine. Code and full results: [systems-data-structures](https://github.com/the-vishal-kumar/systems-data-structures).*
