Deep Dive
distributed systemsalgorithmsdatabases

How Consistent Hashing Powers Distributed Systems

A deep dive into the algorithm that lets systems like Cassandra and Amazon's Dynamo route keys without reshuffling the entire keyspace when nodes come and go.

·27 min read
Medium

Consistent hashing is one of those ideas that sounds complicated until you see the picture — then it’s obvious, beautiful, and you wonder how anything worked before it.

The problem: hash % N doesn’t scale

Imagine you have 4 cache servers and you want to distribute keys across them. The naive approach is hash(key) % N — pick a server by taking the key’s hash modulo the server count.

It works perfectly until N changes. Add a fifth server and almost every key now maps to a different bucket. That triggers a cache miss avalanche — every client races to the database to repopulate a cache that was warm a moment ago.

hash % 4 = 0hash % 4 = 1hash % 4 = 2hash % 4 = 3key: "user:42"hash → 1701key: "order:99"hash → 3388hash(k) % 4Bucket 0Bucket 1Bucket 2Bucket 3
Keys mapped via hash(k) % 4. Bucket 0 owns keys whose hash is divisible by 4, bucket 1 owns hash mod 4 = 1, and so on. Change N to 5 and the modular arithmetic shifts — almost every key lands in a different bucket.

Bump N from 4 to 5 and the modular arithmetic shifts for almost every key — the buckets themselves haven’t moved, but the remainder changes for most hashes. The cache that was warm a second ago is suddenly cold across the board.

Naive — hash(key) % N
  • Changing N re-maps almost every key
  • Adding one server triggers a cache-miss avalanche
  • Scaling means a full, expensive rehash
Consistent hashing
  • Only ~1/N of keys move when N changes
  • New servers slot in between existing neighbors
  • Scaling stays cheap and incremental

The ring

Consistent hashing places both servers and keys on a conceptual ring of 2³² positions, [0, 2³²). Every server is hashed to a position on that ring. Every key is hashed to a position on the same ring and is owned by the first server you encounter going clockwise from the key’s hash position.

When you add a server, it lands somewhere on the ring and takes ownership of the keys between it and its counter-clockwise neighbor. When you remove a server, its keys pass to its clockwise successor. In both cases, only 1/N of keys are affected — the rest stay where they are.

Adding a server moves one boundary, not the whole keyspace — and that’s exactly where the members-only walkthrough picks up: a live ring you can drag nodes around, virtual node mechanics, the vnodes-vs-real-nodes tradeoff, and how Cassandra and Amazon Dynamo put this into production.

Members only

Keep reading with Premium

You've reached the members-only part of this deep-dive — the full implementation, the interactive ring simulator, and the step-by-step walkthrough. Unlock it with a membership.

Related Articles

Mediumsystem design interviewdatabasescachingalgorithms

Design a URL Shortener

The canonical system-design interview, built end to end: how tiny.co/aB3xK9 becomes a database lookup in single-digit milliseconds — base62, key generation, the read-heavy cache, and the scale math that makes 7 characters last a century.

Asked at Amazon, Google, Microsoft +2
·20 min read
Hardsystem design interviewdatabasesalgorithmsconcurrency

Design a Key-Value Store

The capstone that ties the whole series together: consistent hashing to place data, quorums to tune consistency, vector clocks to detect conflicts, and hinted handoff to stay writable through failure. This is Amazon's Dynamo, rebuilt from its four moving parts.

Asked at Amazon, Netflix, Uber +2
·23 min read