Consistent Hashing
Definition
Hashing
- In plain English
- Hashing turns a key into a number in a fixed range using a formula that always gives the same number for the same key.
- Think of it like
- It is like a cloakroom assigning each coat a hook number from its owner's name.
Consistent hashing
- In plain English
- Consistent hashing places servers and keys on a ring so that adding or removing a server moves only a small share of keys.
- Think of it like
- It is like seats around a round table where a new guest only takes over the seats beside them.
Why it exists
You add a fifth cache server. With hash(key) % N, about 80% of keys now point somewhere different.
Most lookups miss their old cached values and reach the database. Scaling the cache can briefly overload the system it protects.
How it works
Virtual nodes
- In plain English
- Virtual nodes give each real server many points on the ring so keys spread more evenly.
- Think of it like
- Like a large restaurant with several entrances around the block.
This extends Module 3 sharding and Module 2 hash-based load balancing. The ring changes how keys map to machines.
Three servers form a cycle; a key between A and B belongs to B, the next server clockwise.
- Place servers
Hash server names to positions on a ring.
- Place a key
Hash the key and choose the first server clockwise, wrapping around if needed.
- Add a server
Move keys between the new server and its predecessor to the new server.
- Balance ownership
Use many virtual nodes per machine, weighted by capacity.
| Method | Expected movement: four to five | Spread | Cost |
|---|---|---|---|
| hash % N | 80% | Even with uniform hashes | Small formula |
| Consistent hashing | 20% with balanced equal capacity | Virtual nodes reduce imbalance | Maintain sorted ring positions |
package main
import (
"fmt"
"hash/crc32"
"sort"
)
type node struct {
point uint32
name string
}
func main() {
ring := []node{}
for _, name := range []string{"A", "B", "C"} {
ring = append(ring, node{crc32.ChecksumIEEE([]byte(name)), name})
}
sort.Slice(ring, func(i, j int) bool { return ring[i].point < ring[j].point })
for _, key := range []string{"episode-1", "episode-2"} {
h := crc32.ChecksumIEEE([]byte(key))
i := sort.Search(len(ring), func(i int) bool { return ring[i].point >= h })
if i == len(ring) {
i = 0
}
fmt.Println(key, ring[i].name)
}
}Examples
Grow an episode cache
The cache grows from four equal servers to five.
Uniform hashes: compare hash % 4 with hash % 5
Check 20 equally likely remainders: 4 unchanged, 16 changed
16 / 20 = 80% movement
Balanced five-server ring: new server owns about 1 / 5 = 20%The ring moves only keys the new server takes over. Twenty percent is an expectation, not a guarantee for every small ring.
The insight
Consistent hashing reduces cache movement during scaling.
Replace one failed destination
Lobby sessions map to servers using a ring, and server B fails.
Keys assigned to failed server B move to next live server C, while keys assigned to A stay there.
Routing changes do not recover session contents by themselves. Keep copies or rebuild sessions before the replacement serves them.
The insight
A failure disturbs one slice of assignments.
Weight different machine sizes
Two large servers get 200 virtual nodes each, while two small servers get 50 each.
Total positions: 200 + 200 + 50 + 50 = 500
Large A: 200 / 500 = 40% expected ownership
Large B: 200 / 500 = 40% expected ownership
Small A: 50 / 500 = 10% expected ownership
Small B: 50 / 500 = 10% expected ownershipThese are shares of possible keys, not guaranteed traffic shares. One extremely popular key can still overload its owner.
The insight
Virtual nodes improve balance and represent unequal capacity.
Trade-offs
Pros
- Moves fewer keys when membership changes.
- Limits reassignment after failures.
- Can weight unequal machines.
Cons
- Needs ring membership coordination.
- Small rings can be uneven.
- Popular keys can overload one owner.
Use when
- Caches grow or shrink.
- Sharded data needs stable assignments.
Avoid when
- A fixed mapping already meets your needs.
- You expect routing alone to recover lost data.
Quick check
Takeaways
- Changing mod-N moves most keys when capacity grows from four servers to five.
- Consistent hashing moves only the affected ring ranges.
- Virtual nodes improve expected balance but do not remove hot keys.