Consistent hashing

Topic 9 of 10 in Week 3: Databases10 min readIntermediateFree course

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

Consistent hashing. Servers A, B and C and eight keys are placed on a ring by hashing. Each key belongs to the next server clockwise, so keys are coloured by their owner. When server D is added between B and C, only the key that lies between B and D moves to D; every other key stays on its server. With hash mod N, adding a server moves most keys; here only about 1/N move. Virtual nodes even out the load.

  1. Picture the output range of a hash function (say 0 to 2³²−1) bent into a circle.
  2. Hash each server (by name or IP) to a point on the ring.
  3. Hash each key to a point on the ring.
  4. 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.

Exercise

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?

Text
(a)
(b)
(c)
(d)

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.