Consistent hashing
Overview
To spread keys across servers, the obvious approach is server = hash(key) % N. It distributes keys evenly, until N changes. Add a fifth server to four and about 80% of keys suddenly map somewhere else. For a cache that means a sudden wave of misses; for a database, a massive data migration. Consistent hashing solves this: when a server joins or leaves, only a small fraction of keys move.
The hash ring
- Picture the output range of a hash function (say 0 to 2³²−1) bent into a circle.
- Hash each server (by name or IP) to a point on the ring.
- Hash each key to a point on the ring.
- A key belongs to the first server clockwise from it.
When a server is added, it takes over only the keys between it and the previous server counter-clockwise. Every other key stays where it was. When a server is removed, its keys move to the next server clockwise; again, nothing else changes. On average, adding or removing one of N servers moves about 1/N of the keys.
Finding a key’s server is a binary search over the sorted server positions: fast, and every client can compute it independently.
Virtual nodes
With only one point per server, two problems appear:
- Uneven load: random positions leave some servers owning much larger arcs than others.
- Uneven impact: when a server fails, its whole load lands on a single neighbour.
The fix is virtual nodes: each physical server is hashed to many points (say 100–256), named A#1, A#2 and so on. Each server now owns many small arcs scattered around the ring, so:
- load evens out statistically,
- a failed server’s keys are spread across all remaining servers,
- a bigger machine can take more virtual nodes and therefore more load.
Replication on the ring
To keep R copies of each key, store it on the owner and the next R−1 distinct physical servers clockwise (skipping virtual nodes of a server already chosen). Cassandra and Dynamo-style systems do this, often also placing copies in different racks or zones.
Where it’s used
- Distributed databases: Cassandra, ScyllaDB and Amazon’s Dynamo design partition data this way.
- Distributed caches: Memcached clients and many Redis proxies use consistent hashing to pick a node per key.
- Load balancers and proxies: hashing users or URLs to backends so each backend keeps a warm cache (Envoy and NGINX support it).
- CDNs: deciding which cache server in a location stores a given object.
Some systems use alternatives with the same goal: rendezvous (highest random weight) hashing, jump consistent hashing, or a fixed number of slots, such as Redis Cluster’s 16,384 hash slots assigned to nodes, which moves slots instead of individual keys.
Limits
Consistent hashing balances keys, not traffic. A single very hot key still lands on one server. Combine it with caching, replication of hot keys or key splitting.
In an interview
When you shard a cache or a key-value store, say: “Keys are placed with consistent hashing and virtual nodes, so adding a node moves only about 1/N of the keys and the load stays even; each key is replicated to the next two nodes on the ring.” If asked to go deeper, draw the ring and walk through adding a node.
Recap
Modulo hashing reshuffles almost everything when the number of servers changes. Consistent hashing places servers and keys on a ring and assigns each key to the next server clockwise, so a change moves only about 1/N of the keys. Virtual nodes even out load and spread the impact of failures, and replicas sit on the following distinct servers. It doesn’t fix hot keys, which need their own handling.
Count the moved keys
A cache cluster stores 12 million keys on 4 servers. (a) With placement hash(key) mod N, roughly how many keys end up on a different server when you go from 4 to 5 servers? (b) With consistent hashing (and enough virtual nodes for even spread), roughly how many keys move? (c) Without virtual nodes, what problem might the new server have? (d) Why does the number of moved keys matter so much for a cache?
(a)
(b)
(c)
(d)(a) With mod N, a key stays put only if hash mod 4 == hash mod 5, which is true for
about 1 in 5 keys. So about 80%, roughly 9.6 million keys, move.
(b) The new server should take about 1/5 of the keys, and only those move:
about 2.4 million keys (20%). The other 80% stay where they are.
(c) With a single point on the ring, the new server takes keys only from its one
clockwise neighbour, and its share depends on where its hash lands, so it may get
far more or far less than 1/5 and the neighbour bears all the change.
Virtual nodes spread its points around the ring, so it takes a little from every
server and ends up with an even share.
(d) Every moved key is a cache miss: the data must be re-read from the database.
Moving 80% of keys at once can flood the database (a "thundering herd") and cause
an outage; moving 20% gradually is manageable.Summary
- With hash(key) mod N, changing N moves almost every key; consistent hashing moves only about 1/N of them.
- Servers and keys are hashed onto the same ring; each key belongs to the next server clockwise.
- Virtual nodes give each server many points on the ring, evening out load and spreading the impact of changes.
- Replicas are placed on the next distinct servers clockwise.
- Used by Cassandra, DynamoDB-style stores, distributed caches, load balancers and CDNs.