Sub topic 15
Distributed Caches and Consistent Hashing
This chapter assumes you know cache-aside, eviction policies such as LRU, and basic client-server networking. It explains how a cache spreads across several nodes and why the way keys map to nodes matters.
Why distribute a cache
One cache node is limited by its memory and its network throughput. A distributed cache splits the data across several nodes, so total capacity and request throughput grow as you add machines. Each key lives on one node (or on a few, if you replicate it).
The client or a proxy must decide which node owns a key. That decision is the central problem of this chapter.
The problem with modulo hashing
The simplest rule is node = hash(key) % N, where N is the number of nodes. It works until N changes. When you add or remove a node, almost every key maps to a different node. The result is a burst of misses, and the database behind the cache absorbs all of that load at once.
Consistent hashing
Consistent hashing places both nodes and keys on a circular space of hash values, called a ring. To find a key's owner, hash the key and walk clockwise to the first node.
When a node joins, it takes over only the keys between itself and the previous node on the ring. When a node leaves, its keys move to the next node. On average only about 1/N of the keys move, instead of nearly all of them.
Virtual nodes
With only a few nodes, the ring splits unevenly, and one node may own much more of it than the others. To fix this, each physical node is placed on the ring many times under different labels, called virtual nodes. This spreads load more evenly. It also spreads a failed node's keys across many survivors, not onto one neighbor.
import bisect
import hashlib
def h(value):
return int(hashlib.md5(value.encode()).hexdigest(), 16)
class Ring:
def __init__(self, nodes, vnodes=100):
self.ring = sorted(
(h(f"{node}#{i}"), node) for node in nodes for i in range(vnodes)
)
self.points = [point for point, _ in self.ring]
def node_for(self, key):
i = bisect.bisect(self.points, h(key)) % len(self.ring)
return self.ring[i][1]
ring = Ring(["cache-a", "cache-b", "cache-c"])
print(ring.node_for("user:42"))Failure modes
- Lost node: its keys miss until they are reloaded from the source. Consistent hashing limits this to that node's share, but the database must still handle the extra load.
- Hot keys: hashing balances the number of keys, not the traffic. One very popular key can overload its node. Mitigate with a short-lived local cache in each application process, or by replicating the hot key.
- Replication: copies on extra nodes improve availability, but they bring back the consistency questions from earlier chapters.
- Disagreeing clients: if two clients hold different node lists, they route the same key to different nodes and can serve different values. Distribute membership changes reliably.
Exercise
A cache cluster uses hash(key) % N with 4 nodes. You add a fifth node. Roughly what share of keys changes owner, and what would it be with consistent hashing?
Show answer
With modulo hashing, about 80% of keys change owner, because a key stays only when hash % 4 equals hash % 5. With consistent hashing, about 1/5, or 20%, move, and they all move to the new node.