FullStack AI Hub
Studio
System Design · 03. Data & Storage
11 min
10 steps
Intermediate

Consistent Hashing

Map servers and keys onto a shared hash ring so scaling moves only a fraction of data.

System Design03. Data & StorageLesson 8 of 11
11 minIntermediate10 steps

Consistent Hashing

Map servers and keys onto a shared hash ring so scaling moves only a fraction of data.

SERVERS ON RING
4 → 5 (% N)
KEYS MOVED
79%
BIGGEST SHARE
25%
Router (% 5)74%hash(k) % 5Cache A86%miss stormCache B88%miss stormCache C84%miss stormCache D85%miss stormMODULO RESHUFFLEold mappinghash(key) % 4new mappinghash(key) % 5FNV-1a moved79% invalidated
cache lookup
migrated key
replica copy
cache miss storm
In This LessonStep 1 of 10
What You Will Master
  • ✓Understand why hash(key) % N invalidates ~80% of keys when cluster size changes
  • ✓Place both servers and keys on a 0 → 2³² ring and resolve ownership clockwise
  • ✓Use virtual nodes to smooth lopsided arcs and cut the biggest server share
  • ✓Replicate keys to the next N distinct servers along the ring for fault tolerance

Architectural Trade-Off Comparison Matrix

PatternBest ForKey Trade-Off
Modulo Hashing (% N)Fixed-size static clusters that almost never add or remove nodesAdding/removing 1 node invalidates ~(N-1)/N (~80%) of cached keys at once
Basic Consistent Ring (1 pin)Conceptual routing where scaling moves only ~1/N of keysRandom 1-pin spacing leaves lopsided arcs (one node can hold 40%+ of keys)
Virtual Nodes (12–200 vnodes)Production caches & Dynamo/Cassandra clusters needing smooth load varianceSlightly larger routing table in memory (still O(log V) binary search)
Clockwise RF=3 ReplicationDurable storage rings surviving host or AZ outages with zero data lossCoordinator must skip duplicate vnodes from the same physical host/AZ

Step-by-Step Architectural Breakdown (10 Steps)

Full architectural walkthrough transcript and state transitions for Consistent Hashing.

STEP 01 · PROBLEMJump to step →

The modulo reshuffle problem

Suppose four cache servers hold your hot user sessions using `hash(key) % 4`. When you add a fifth cache node to handle peak traffic, the formula flips to `hash(key) % 5`.

Across our 48 sampled keys (hashed with real 32-bit FNV-1a), **79% of keys** immediately map to a different server—triggering a massive cache-miss storm straight into your database.

watch:With % N routing, adding or losing a single node remaps roughly (N - 1) / N of all keys—nearly the entire cache.

Modulo RemappingCache StampedeFNV-1a Hash
STEP 02 · SOLUTIONJump to step →

Bend the hash space into a ring

Instead of dividing by **N**, we take the full 32-bit hash range (`0` to `2³² - 1`) and wrap the ends together into a circle—the **Hash Ring**.

Each server is hashed by its name or IP address (`fnv1a32("node:A")`) and placed directly at its resulting angle on the ring. The ring itself never changes size when servers come or go.

idea:Decoupling hash coordinates from the server count N is the core breakthrough of Consistent Hashing.

Hash Ring32-Bit Hash SpaceConsistent Hashing
STEP 03 · TECHNIQUEJump to step →

Place keys and walk clockwise

Every data key is hashed with the exact same 32-bit function (`fnv1a32(key)`), dropping it onto an angle on the ring.

To find who owns a key—for example **key @ 195°**—simply **walk clockwise** around the ring until you hit the first server (**Server A**). Each key dot below is colored by its clockwise owner.

good:In code, the ring is a sorted array of server hashes; walking clockwise is a single O(log M) binary search with wrap-around at index 0.

Clockwise LookupBinary Search on Ring
STEP 04 · SOLUTIONJump to step →

Add Server E: only one arc moves

Now we add **Server E** to the ring. It lands in the middle of one existing server’s arc and claims only the keys immediately counter-clockwise of its new pin.

Every other arc on the ring stays untouched! Computed directly from our 32-bit FNV-1a ring, only **46% of keys** move—compared to **79%** with modulo hashing.

idea:On average, adding a server to a ring of N nodes moves only ~1 / (N + 1) of keys—the theoretical minimum.

Minimal Key MovementArc Splitting
STEP 05 · SOLUTIONJump to step →

Remove Server C: graceful absorption

What happens if **Server C** crashes or gets decommissioned? Its keys simply continue walking clockwise to the very next neighbor on the ring.

All keys owned by A, B, D, and E remain completely intact—only **4% of keys** re-route to C’s clockwise neighbor.

good:Node removal is just as gentle as node addition: zero global reshuffle across the rest of the fleet.

Node EvictionLocalized Failover
STEP 06 · PROBLEMJump to step →

The catch: lopsided arcs

Look closely at the **BIGGEST SHARE** metric when each server only gets one random position on the ring. With just 4 or 5 pins, random hash placement produces uneven gaps.

One unlucky machine ends up owning **46% of all keys** while another owns a tiny sliver. Worse, if one server dies, its single clockwise neighbor absorbs 100% of the fallen server’s load.

watch:Few points on a circle have high variance—a single random gap can easily leave one server with 2×–3× the average load.

Ring SkewArc VarianceCascading Overload
STEP 07 · SOLUTIONJump to step →

Smooth the ring with Virtual Nodes

Instead of placing each physical server at just one point, we hash **12 Virtual Nodes** per server (`node:A#v0`, `node:A#v1`, … `node:A#v11`) all around the circle.

With 60 interleaved pins on the ring, random gaps average out. The biggest server share drops from **46%** down to **33%**—close to the ideal 20% for 5 servers!

idea:Virtual nodes also let you weight heterogeneous hardware: give a machine with 2× RAM twice as many vnodes.

Virtual Nodes (Vnodes)Load SmoothingWeighted Ring
STEP 08 · TECHNIQUEJump to step →

Replicate along the ring

For durable databases like Cassandra or DynamoDB, storing each key on just one server is too risky. To achieve a **Replication Factor of 3**, simply keep walking clockwise past the primary owner.

The coordinator writes the key to the first **3 distinct physical servers** encountered on the ring—skipping duplicate virtual nodes belonging to a server already selected.

good:Always deduplicate by physical host (and ideally availability zone) while walking clockwise so two replicas never land on the same machine.

Preference ListReplication Factor (RF=3)Ring Replication
STEP 09 · TECHNIQUEJump to step →

Consistent hashing in a live cache fleet

In a real Memcached or Redis Cluster deployment, every application server keeps a tiny copy of the ring in memory. A client hashes `session:4821` locally and opens a direct connection to the exact cache host—no central bottleneck needed.

When autoscaling adds a fresh cache node, a gossip protocol or config service broadcasts the updated ring and 80%+ of your hot cache stays warm.

idea:Client-side ring routing eliminates an extra network hop while preserving O(log V) lookup speed.

Client-Side PartitioningDistributed Cache FleetGossip Ring Sync
STEP 10 · RECAPJump to step →

Consistent Hashing recap

By placing both servers and keys onto a fixed `0 → 2³²` ring, **Consistent Hashing** shrinks resharding cost from ~80% down to `~1/N`.

Pair the ring with **Virtual Nodes** (100–200 per machine in production) to keep load variance tight, and walk clockwise across **distinct physical hosts** for clean replication.

good:Used under the hood by Amazon Dynamo, Apache Cassandra, Discord, and modern CDN edge routing.

Consistent Hashing SummaryDynamo Ring Architecture

Related Production Deep-Dives & Simulators

Related Systems Course
Free · Verifiable Credential

FS-401RAG Engineer

Build evidence-carrying retrieval systems from ingestion and provenance through search reranking grounded generation and evaluation.

21 concepts~25h self-paced
Start Free Course
Weekly Engineering Dispatch

Get the weekly systems teardown

One technical note per week covering inference bottlenecks, retrieval harnesses, and commit-pinned teardowns.