Zeeshan Adil
Book a call
← All concepts

CONCEPT

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.

Also called: hash ring. By Zeeshan Adil··Databases·Distributed Systems·Performance

A circle of hash positions with databases A, B and C on it, and a new database D inserted at position 60, taking over only the arc just before it.

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 fix that.
  • It does not spread one popular key. One key hashes to one place, so a 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, using 100,000 post ids and a real SQLite file per database.

StrategyPosts that move when a 4th database joins 3
hash % n75.0%
Ring, 64 virtual nodes each28.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.

RUN IT YOURSELF

Challenge · Distributed Systems ChallengesWhere does each comment live?
What are virtual nodes?What is a hot key?

SOURCES

Get the next deep dive by email

New deep dives on how production systems really behave, from Kubernetes networking to PostgreSQL internals. About one email a month.

You will get one email to confirm. Unsubscribe from any email with one click. Your address is used for this newsletter and nothing else.

Written by Zeeshan Adil

I run systems like this for a living. If your team is fighting the same problem, or wants this as a talk, write to me.