Software Development

How Consistent Hashing Works: Hash Rings, Virtual Nodes and Jump Hash

Why adding one server shouldn't reshuffle your whole cache, and the clever ring that prevents it

Zarton EditorsNov 20, 2026 · 8 min read

You have ten cache servers and a simple rule: send each key to hash(key) % 10. It works beautifully until the day you add an eleventh server and almost every key suddenly lives somewhere new. Your caches go cold at once and the database behind them takes the full blast.

This guide explains how consistent hashing works: the hash ring that fixes the problem, why virtual nodes are needed to make it fair, and the newer alternatives (rendezvous hashing, jump hash and bounded loads) that real systems such as Amazon Dynamo, Apache Cassandra and HAProxy rely on.

The problem with modulo hashing

Hashing a key and taking the remainder is the obvious way to spread data across N machines. The trouble is that the answer depends on N itself. Change the server count and the remainder changes for most keys.

Go from 10 servers to 11 and a key only stays put when its hash leaves the same remainder under both divisors. That is true for roughly one key in eleven, so around 90% of keys move. The ideal is the reverse: only about one key in eleven should move, the share the new server is supposed to take over. Google's jump hash paper makes the same point with a 10-to-12 example, noting that just one sixth of the data actually needs to relocate.

For a cache, mass relocation means a wave of misses. For a database or storage cluster, it means copying most of your data over the network just to add one box.

How consistent hashing works: the hash ring

The idea was introduced in a 1997 paper by David Karger and colleagues at MIT, written to relieve "hot spots" on the early web, when a single popular site could be swamped by traffic. Content delivery networks remain classic users of the technique, because it spreads requests across a changing population of servers.

The trick is to stop hashing keys to servers and instead hash both keys and servers onto the same circular number line:

  1. Pick a large hash space, say 0 to 264−1, and imagine it bent into a circle.
  2. Hash each server's name to a point on that circle.
  3. Hash each key to a point on the same circle.
  4. Walk clockwise from the key until you hit a server. That server owns the key.

Now watch what happens when the cluster changes. Adding a server drops one new point on the ring, and it takes over only the keys between itself and the previous server counterclockwise. Removing a server hands its keys to the next server clockwise. Everyone else is untouched. On average only about K/n keys move, where K is the number of keys and n the number of servers.

Virtual nodes: making the ring fair

A ring with one point per server has two practical flaws. Random points land unevenly, so some servers own far bigger arcs than others. And when a server fails, its entire arc falls onto a single neighbor, which can double that machine's load at the worst possible moment.

The fix is virtual nodes (vnodes): give every physical server many points on the ring, for example by hashing "cache-a#0", "cache-a#1" and so on. With many small arcs per server, ownership averages out. When a server leaves, its arcs scatter across many survivors instead of one. A more powerful machine can simply be given more virtual nodes so it carries a bigger share.

Amazon's 2007 Dynamo paper popularized this design. Dynamo places each physical host at multiple ring positions and replicates every key onto the next distinct hosts clockwise, so the ring handles both partitioning and replication.

import bisect, hashlib

def h(s: str) -> int:
    # Any fast, well-distributed hash works; security isn't the goal here.
    return int.from_bytes(hashlib.md5(s.encode()).digest()[:8], "big")

class HashRing:
    def __init__(self, nodes, vnodes=100):
        self.vnodes = vnodes
        self.points = []   # sorted ring positions
        self.owner = {}    # position -> physical node
        for n in nodes:
            self.add(n)

    def add(self, node):
        for i in range(self.vnodes):
            p = h(f"{node}#{i}")
            bisect.insort(self.points, p)
            self.owner[p] = node

    def remove(self, node):
        for i in range(self.vnodes):
            p = h(f"{node}#{i}")
            self.points.remove(p)
            del self.owner[p]

    def lookup(self, key):
        i = bisect.bisect_right(self.points, h(key)) % len(self.points)
        return self.owner[self.points[i]]

ring = HashRing(["cache-a", "cache-b", "cache-c"])
keys = [f"user:{i}" for i in range(10_000)]
before = {k: ring.lookup(k) for k in keys}
ring.add("cache-d")
moved = sum(before[k] != ring.lookup(k) for k in keys)
print(f"{moved / len(keys):.0%} of keys moved")   # about 25%
A minimal hash ring with virtual nodes. Adding a fourth server moves roughly a quarter of the keys, not nearly all of them.

Beyond the ring: other consistent hashing algorithms

Rendezvous (highest random weight) hashing

Published by Thaler and Ravishankar in the mid-1990s, around the same time as Karger's work, rendezvous hashing skips the ring entirely. For each key, compute a score for every server by hashing the key and server name together, then pick the server with the highest score. Removing a server only affects the keys it was winning. It needs no ring structure, but each lookup touches every server, so it fits small or moderate pools best. Microsoft's Cache Array Routing Protocol (CARP) for proxy caches is a well-known example.

Jump consistent hash

In 2014, Google engineers John Lamping and Eric Veach published jump consistent hash, an algorithm of only a few lines that needs no memory beyond a handful of variables. Their paper reports it balances keys more evenly than the original ring and runs faster. The catch: it returns a bucket number from 0 to n−1 rather than an arbitrary server name, and buckets can only be added or removed at the end. That makes it great for numbered storage shards and a poor fit when arbitrary machines can die.

Consistent hashing with bounded loads

Plain consistent hashing only balances about as well as random assignment, so a few unlucky servers end up overloaded. Researchers at Google proposed a fix in 2016: cap every server at (1+ε) times the average load, and when a server is full, push the request clockwise to the next one with room. Vimeo engineer Andrew Rodland added it to HAProxy (released in version 1.7.0 as the hash-balance-factor option), and Google reports that it cut Vimeo's cache bandwidth by a factor of almost eight.

ApproachKeys moved on changeLookup costBest for
Modulo (hash % N)Almost allConstantFixed-size clusters that never change
Ring + virtual nodesAbout 1/nBinary search over ring pointsCaches, Dynamo-style databases, arbitrary node names
Rendezvous (HRW)About 1/nOne hash per serverSmall to moderate pools, simple code
Jump hashAbout 1/nFast, no memoryNumbered shards that grow or shrink at the end
Bounded loadsClose to 1/n, plus overflowRing lookup plus probingLoad balancing where no server may be overloaded

Where you'll meet it in practice

  • Distributed caches. Memcached client libraries commonly use a ring scheme (ketama is the best-known), so adding a cache server doesn't flush everything. It complements the request-level caching described in our guide to how HTTP caching works.
  • Databases. Dynamo, Cassandra and similar stores partition data on a token ring and replicate to successors.
  • Load balancers. Hash-based balancing keeps the same URL or user on the same backend so its warm cache gets reused, as covered in how load balancers work.
  • Peer-to-peer systems. Distributed hash tables such as Chord and Kademlia build on the same idea to find which peer holds a file.

Consistent hashing decides where data lives; it does not make replicas agree with each other. Systems that need strict agreement layer a protocol like Raft consensus on top.

Key takeaways

  • Modulo hashing reshuffles almost every key when the server count changes.
  • Consistent hashing maps keys and servers onto a ring, so a change moves only about 1/n of keys.
  • Virtual nodes smooth out uneven arcs and spread a failed server's load across many survivors.
  • Rendezvous hashing, jump hash and bounded loads trade memory, speed and flexibility differently.
  • It decides placement only; replication agreement needs a separate protocol.

Frequently asked questions

What problem does consistent hashing solve?

It lets you add or remove servers while moving only a small fraction of keys, instead of remapping nearly all of them as modulo hashing does. That keeps caches warm and avoids massive data copies.

Why do I need virtual nodes?

With one ring position per server, arcs are uneven and a failed server dumps all its keys on one neighbor. Many positions per server even out ownership and spread failover load.

Which hash function should I use?

Any fast hash with good, uniform distribution. Cryptographic strength is not required, which is why non-cryptographic hashes, or MD5 used purely for spread, are common choices.

Is consistent hashing the same as sharding?

Sharding is the general practice of splitting data across machines. Consistent hashing is one strategy for deciding which shard each key belongs to, chosen when the set of machines changes over time.

Sources

Comments

No comments yet — be the first to share your thoughts.

Join the discussion

Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Keep reading

All posts

Run your whole business from one conversation.

Get startedTalk to us