# What is consistent hashing?

> Consistent hashing places both keys and servers on the same circle of hash values. A key belongs to the first server clockwise from it. When a server joins or leaves, only the keys in the arcs next to it change owner, instead of almost every key as with hash % n.

- Author: Zeeshan Adil (https://www.internalswithzee.com/about)
- Published: 2026-11-12
- Source: https://www.internalswithzee.com/concepts/consistent-hashing

Also called: hash ring.

## The problem it solves

You have comments spread over three databases and you pick one with `crc32(post_id) % 3`. It works until you add a fourth database. Now the formula is `% 4`, and a key keeps its owner only if both remainders happen to match, which is true for about 1 key in `n + 1`. Nothing about the post changed, only `n`, yet most rows have to be copied to a different machine while the app is live.

## How it works

Treat the range of hash values as a clock face. Hash each server onto it, and hash each key onto it. The rule is one sentence: walk clockwise from the key and the first server you meet owns it.

Take a tiny clock numbered 0 to 99, with DB A at 40, DB B at 70 and DB C at 90. Post 15003 hashes to 49. The next server clockwise is DB B at 70, so the comment lives on DB B.

Now DB D joins at 60. It takes positions 41 to 60, the arc just before it, and nothing else. DB A and DB C give up nothing. Post 15003 (position 49) moves from B to D. A post at position 65 stays on B.

Every router can compute the owner from the list of servers alone, with no lookup table to keep in sync.

## What it does not do

- **It does not move the data.** The ring only says where a key belongs. Copying rows to the new owner, without losing writes that arrive meanwhile, is a separate job.
- **It does not balance by itself.** With one position per server the shares are a lottery. [Virtual nodes](/concepts/virtual-nodes) fix that.
- **It does not spread one popular key.** One key hashes to one place, so a [hot key](/concepts/hot-key) still lands on one server.
- **It needs everyone to agree on the member list.** A router with an old list computes the wrong owner. The database has to check ownership, for example with an epoch number, and refuse the write.

## Measured

These numbers come from my [sharding challenge](/challenges/distributed-systems/where-does-each-comment-live), using 100,000 post ids and a real SQLite file per database.

| Strategy | Posts that move when a 4th database joins 3 |
|---|---|
| `hash % n` | 75.0% |
| Ring, 64 virtual nodes each | 28.3% |
| Fair share (the least that can move) | 25.0% |

Every post that moved on the ring went to the new database. With `hash % n`, posts were also shuffled between the old ones. Going from 8 to 9 databases, `hash % n` moves 88.9% of posts.

The same run shows the lottery: with one token per database, one of four databases owned 98.6% of the ring, and in one growth step the new database swallowed 97.3% of the keys.

Consistent hashing is the idea behind token rings in Amazon's Dynamo, Apache Cassandra and ScyllaDB.

## Related

- Challenge to run: https://www.internalswithzee.com/challenges/distributed-systems/where-does-each-comment-live
- Related concept: https://www.internalswithzee.com/concepts/virtual-nodes
- Related concept: https://www.internalswithzee.com/concepts/hot-key

## Sources

- [Karger et al., Consistent Hashing and Random Trees (STOC 1997)](https://dl.acm.org/doi/10.1145/258533.258660)
- [DeCandia et al., Dynamo: Amazon's Highly Available Key-value Store (SOSP 2007)](https://www.allthingsdistributed.com/files/amazon-dynamo-sosp2007.pdf)

