Consistent Hashing

Consistent hashing is a technique for spreading keys across a changing set of servers without reshuffling most of the data every time a server is added or removed. It shows up under the hood of distributed caches, sharded databases, and content delivery networks — anywhere data needs to be split across many nodes that come and go over time.

The problem with mod N

The obvious way to pick a server for a key is hash(key) % N, where N is the number of servers. It is simple and spreads keys evenly, but it has a fatal flaw: changing N changes the answer for almost every key. Add a fourth server to a three-server cluster and hash(key) % 3 becomes hash(key) % 4 — a completely different value for most keys. In a cache, that means nearly every cached entry now maps to the "wrong" server and looks like a miss; in a sharded database, it means nearly all the data has to move. A technique that treats "add one server" as "relocate almost everything" doesn't scale.

Placing keys and nodes on a ring

Consistent hashing fixes this by hashing keys and servers into the same space and arranging that space as a ring: hash values from 0 up to some maximum wrap back around to 0. Each server is hashed to a point on the ring; each key is hashed to a point on the ring too, and belongs to the first server found walking clockwise from the key's position.

Three servers placed at points on a hash ring; each key is assigned to the next server clockwise from its own hash position
Keys belong to the next node clockwise on the ring

Adding a fourth server now only affects the keys between the new server and the previous node clockwise from it — everything else on the ring is untouched, because every other key's "next server clockwise" hasn't changed. Removing a server has the same small blast radius: its keys fall through to the next node clockwise, and nothing else moves.

Virtual nodes even out the load

A ring with only a handful of points can land unevenly — one server might own a much larger arc than another just from where the hashes happened to fall. The standard fix is virtual nodes: each physical server is hashed onto the ring many times (say, 100–200 points) under different labels (server-a-0, server-a-1, ...), instead of once. With enough points per server, the arcs average out and the load balances far more evenly, at the cost of a slightly larger routing table.

def ring_for(servers, vnodes=150):
    ring = {}
    for server in servers:
        for i in range(vnodes):
            point = hash(f"{server}-{i}")
            ring[point] = server
    return ring

def lookup(ring, key):
    point = hash(key)
    # the first ring point at or after `point`, wrapping around
    candidates = sorted(p for p in ring if p >= point) or sorted(ring)
    return ring[candidates[0]]

What it costs

Consistent hashing isn't free: routing requires either a sorted structure to find "the next point clockwise" (an O(log N) lookup instead of O(1) for plain modulo) or a precomputed lookup table, and virtual nodes multiply the ring's size by however many points each server gets. In exchange, the system gets a property plain hashing can't offer: scaling the cluster up or down moves roughly 1/N of the keys instead of nearly all of them — the difference between a routine capacity change and an outage-sized cache stampede.