Distributed Systems

Consistent Hashing Algorithms

Consistent hashing algorithms decide which server owns a key, and they do it so that adding or removing a server moves only a small share of the keys. The ring, rendezvous, jump, and Maglev methods all solve this, with different trade-offs in speed, memory, and flexibility. Here is a practical guide: the comparison table first, then each method in depth.

At a glance

Say you spread data across several servers by computing hash(key) % n. It works until n changes. Then almost every key points to a different server. Consistent hashing algorithms fix this. They make sure that when a server joins or leaves, only about 1/n of the keys move.

There are four main designs. The ring (Consistent Hashing) is the classic. Rendezvous Hashing picks the server with the highest score. Jump Consistent Hash uses a tiny formula and no memory. Maglev Hashing precomputes a lookup table for the fastest reads. The table below compares them, and the sections after it explain how to choose.

Distributed Systems Lookup timeMemoryKeys moved on add/removeArbitrary node removalLoad balanceBest for
Consistent Hashing O(log n) with a sorted ring (binary search)O(n·v) ring entries for n nodes with v virtual nodesAbout 1/n of keys (near the minimum)Yes, any nodeGood with 100+ virtual nodes per server; spread shrinks roughly as 1/√vSharded caches and databases where nodes join and leave freely
Rendezvous Hashing O(n) hashes per lookup (O(log n) with a tree variant)O(n), just the node listAbout 1/n of keys (the minimum)Yes, any nodeVery even, with no virtual nodes neededSmall to mid-size clusters, weighted choices, stateless clients
Jump Consistent Hash O(ln n) expectedO(1), no stored stateAbout 1/n of keys (the minimum), when growing or shrinking at the endNo, only the last bucket can be droppedNear-perfect, equal share per bucketSharded storage with numbered shards that grow or shrink at the end
Maglev Hashing O(1) table read (the table is rebuilt when backends change)O(M) table, with M much larger than nSmall but not minimal; closer to 1/n as M growsYes, any backend (the table is rebuilt)Near-uniform, within about one slot per backend by constructionNetwork load balancers that need very fast per-packet lookups

Each algorithm in depth

Click any card for the full article with an interactive visualization, complexity analysis, code examples, and FAQ.

The problem: mod-N rehashing moves almost everything

With hash(key) % n, a key stays put only if its remainder is the same for the old and new n. Going from 4 servers to 5, a key keeps its server only about 1 time in 5. So roughly 80% of keys move. In general, growing from n to n+1 moves about n/(n+1) of the keys.

For a cache, that is a wave of misses that lands on your database. For a sharded database, it is a huge data migration. Every algorithm on this page exists to avoid that.

What "minimal disruption" means

When you add a server to a cluster of n, the new server must take some keys. A fair share is 1/(n+1) of them. Moving that many is the minimum possible, because the new server has to end up with its fair share. A scheme has minimal disruption if it moves no more than that, and no key moves between two old servers.

Rendezvous Hashing and Jump Consistent Hash achieve this exactly, in expectation. The ring comes close, with some random variation. Maglev Hashing accepts a little extra movement in exchange for O(1) lookups. The literature describes its disruption as small but not minimal, and it gets closer to ideal as the table gets larger.

Virtual nodes: fixing the ring's uneven spread

If each server sits on the ring once, the arcs between servers have random sizes. Some servers get far more keys than others. The fix is virtual nodes: each real server is placed on the ring many times, under many different hashes. The random arcs average out. Load spread shrinks roughly as 1 over the square root of the number of virtual nodes, so 100 to a few hundred per server is common.

Virtual nodes also help recovery. When a server dies, its load spreads across many survivors instead of landing on one neighbor. The cost is memory: the ring holds n times v entries, and lookups are a binary search over them. See Consistent Hashing for a walkthrough.

How the four compare

Ring: flexible and well understood. Lookup is O(log n) over a sorted ring. You pay for the ring memory and must tune the virtual-node count.

Rendezvous: the simplest to get right. Hash the key with each server and take the max. There is no state and no tuning, and balance is very even. Lookup is O(n), which is fine for tens or hundreds of servers but costly for thousands on a hot path. A tree-based variant cuts this to about O(log n).

Jump: the smallest and fastest in memory terms. It takes a key and a bucket count and returns a bucket number. It needs no ring and no list. The catch is that buckets are numbered 0 to n-1, and you can only add or remove at the end. It cannot drop a server in the middle, and it cannot weight servers. It suits sharded storage, where a failed shard is replaced rather than removed.

Maglev: trades build time and memory for the fastest lookup. Each backend has a pseudo-random preference list of table slots. Backends take turns claiming their top free slot until the table is full. A lookup is then one array read. The table is rebuilt whenever backends change, which is cheap enough for a load balancer.

Decision guide

  • Nodes come and go freely, and you want a proven default: use the ring with virtual nodes (Consistent Hashing).
  • Fewer than a few hundred nodes, and you want simplicity and even balance: use Rendezvous Hashing.
  • Numbered shards that only grow or shrink at the end: use Jump Consistent Hash.
  • Per-packet or per-request lookups at very high rates, with a changing backend set: use Maglev Hashing.
  • Servers have different capacities: weighted rendezvous, or a ring with more virtual nodes for bigger servers. Plain jump hash has no weights.

Real systems

Amazon's Dynamo paper (2007) popularised the ring with virtual nodes for partitioning data. Apache Cassandra uses a token ring with virtual nodes in the same spirit. Many distributed caches and CDN edge-routing layers use ring-style or rendezvous-style hashing to decide which node holds an object.

Google's Maglev paper (NSDI 2016) describes the software network load balancer that uses the table method to spread connections. Open-source load balancers and proxies have since adopted Maglev-style hashing. Jump hash comes from a 2014 Google paper by John Lamping and Eric Veach, "A Fast, Minimalist Consistent Hash Algorithm". It is meant for sharded storage with numbered shards. Rendezvous hashing was introduced by Thaler and Ravishankar in 1996 and is used where a stateless, ring-free choice is wanted.

The ring, rendezvous, and Maglev methods all handle removing any node with no extra layer. Only jump hash cannot. If you use jump hash with named servers, you need a mapping from bucket numbers to servers on top.

Frequently asked questions

What is consistent hashing?

Consistent hashing is a way to assign keys to servers so that adding or removing a server moves only a small fraction of the keys, roughly 1/n, instead of nearly all of them as with hash(key) % n.

Rendezvous hashing vs consistent hashing: which is better?

Neither wins everywhere. Both move about 1/n of keys when membership changes. Rendezvous hashing is simpler and balances load evenly without virtual nodes, but each lookup checks every server, which is O(n). A ring does lookups in O(log n) but needs virtual nodes and more memory. Rendezvous suits smaller clusters. The ring suits very large ones.

When should I use jump consistent hash?

Use it when your shards are numbered 0 to n-1 and you only grow or shrink the cluster at the end, as in many sharded storage systems. It needs no memory and gives very even balance. Do not use it when you must remove an arbitrary server from the middle.

Why can't jump hash remove a node from the middle?

It only knows how many buckets exist, not which servers they are. Removing bucket 17 of 50 would mean renumbering every bucket above it, and that renumbering would shift many keys. Only the highest-numbered bucket can be dropped without that cost.

What is Maglev hashing used for?

It is the hashing method behind Google's Maglev software network load balancer, described in a 2016 paper. It sends each connection to a backend using a precomputed lookup table, so the per-packet lookup is O(1) while keeping a connection on the same backend when the backend set changes slightly.

Is Maglev hashing minimal-disruption like rendezvous hashing?

No. When a backend changes, Maglev can move somewhat more keys than the ideal 1/n. The paper reports that disruption is small and gets closer to ideal when the lookup table is much larger than the number of backends. Rendezvous hashing is provably minimal in expectation.

What are virtual nodes in consistent hashing?

Virtual nodes are extra positions on the hash ring for each real server. Instead of one point per server, each server gets many, such as 100 or more. That evens out the load and spreads a failed server's keys across many survivors. The cost is more ring memory.

Which load balancing hash algorithm is fastest?

For lookup speed, Maglev's table read is O(1). Jump hash is O(ln n) with no memory and is very fast in practice for modest bucket counts. Rendezvous is O(n) per lookup, and a sorted ring is O(log n). The fastest choice also depends on how often membership changes.