Sub topic 13
Consistent Hashing in Practice
This chapter assumes you know how the hash ring, virtual nodes, and replica selection work from the earlier chapters. It covers where consistent hashing shows up in real systems and which operational problems you should expect.
Where it is used
Consistent hashing appears wherever a set of nodes changes over time and each key needs a stable home.
| System type | What the ring decides | Typical node change |
|---|---|---|
| Distributed caches and cache clients | Which cache server holds a key | A server is added for capacity or fails |
| Dynamo-style key-value stores | Which nodes store a key and its replicas | A storage node joins or is replaced |
| Load balancers | Which backend handles a client or session | A backend is drained or restarted |
| Sharded databases and object stores | Which shard holds a record or object | A shard is added to grow capacity |
The approach is the same in each case. The system hashes the key, finds its position on the ring, and uses the node or nodes that follow it.
Operational concerns
Membership must be consistent
The ring only works if every router builds the same ring from the same node list. If two routers disagree about membership, they send the same key to different nodes. A cache then shows misses and a store can accept writes on the wrong node. Real systems spread membership with a coordination service or a gossip protocol, and they accept a short window where views differ.
Moved keys need handling
The ring tells you which keys change owner, but it does not move the data. For a cache, the new owner starts empty and the first reads miss until it warms up. For a store, data must be streamed to the new owner before it serves reads, or reads fail. Plan this transfer and limit its speed so it does not overload the nodes that hold the data.
Failures differ from planned changes
Removing a node on purpose lets you copy its data first. A crash does not. With replication, the next nodes on the ring already hold copies, so reads continue. Without replication, a crashed node's keys are simply lost from the cache or store until they are rebuilt.
Hot keys are not fixed
Consistent hashing balances the number of keys, not the traffic per key. One very popular key still lands on one node, and virtual nodes do not help. Common fixes are a small local cache in front of the ring, or copying the hot key to several nodes.
Choosing the virtual node count
More virtual nodes give smoother balance, but each one costs memory and slows membership changes. The Python ring from earlier chapters uses bisect.insort and list.remove, which are linear in the list size, so adding a node with many virtual nodes gets slower as the ring grows. This is fine for hundreds of positions. Large production rings use faster structures or build the ring once and swap it in.
Checking balance in your own cluster
Measure balance instead of assuming it. This script counts keys per node and reports how far the busiest node is from the average. It reuses the HashRing class from the earlier chapter.
from collections import Counter
ring = HashRing(["a", "b", "c", "d"], vnodes=100)
keys = [f"key{i}" for i in range(10000)]
counts = Counter(ring.get_node(k) for k in keys)
average = len(keys) / len(counts)
for node, count in sorted(counts.items()):
print(node, count, f"{count / average:.2f}x average")
print("max/avg:", max(counts.values()) / average)Run it with different vnodes values. A max-to-average ratio close to 1.0 means good balance. Track the same ratio on real traffic, since real keys are not uniform.
Exercise
A cache cluster uses a consistent hash ring. A team renames its nodes from "cache-1" to "cache-01" while keeping the same machines. Why does the hit rate collapse, and what should they have done?
Show answer
Virtual node positions come from hashing names such as "cache-1#0". Renaming changes every hash, so almost every key maps to a different node and the cache behaves as if it were empty. They should keep the original ring identifiers stable, even if display names change, or roll out the change with a plan for a cold cache.