Loading…
Loading…
Charting your lesson…
Opening your lesson…
Consistent Hashing
Lesson 6 of 6
A hashing scheme that minimizes key remapping when nodes are added or removed — key to distributed caches and shards.
6 steps•about 11 min•8 flashcards
Step 1/ 61 min
When an interviewer asks "how do you add a cache server without cold-starting the whole cluster?", the answer is consistent hashing. It is the classic technique behind distributed caches (Memcached clients), databases (Cassandra, DynamoDB), and shard routing.
Step 2/ 62 min
Naive sharding maps a key with hash(key) mod N, where N is the number of nodes. It's even and simple — until N changes. Add or remove one node and N changes, so almost every key remaps to a different node, invalidating the whole cache and forcing massive data movement. Consistent hashing solves this: when a node joins or leaves, only about 1/N of the keys move, not all of them.
Imagine the hash output space bent into a ring (0 to 2^32-1, wrapping around). Each node is hashed to a point on the ring. Each key is hashed to a point too, then assigned to the first node found clockwise from its position. Because keys and nodes share one ring, adding a node only steals the slice of keys between it and its predecessor — everything else stays put.
Remove a node and its keys simply roll clockwise to the next node; no other key is affected. Add a node and it takes over a contiguous arc from its clockwise successor. In both cases only the neighbors' keys move, roughly 1/N of the total — the defining benefit.
Plain consistent hashing has a flaw: with few nodes, the ring is uneven, so some nodes own big arcs and become hotspots, and a removed node dumps all its load onto one neighbor. The fix is virtual nodes (vnodes): each physical node is placed at many points on the ring (e.g., 100–200 hashes). This smooths the distribution, spreads a departing node's load across many others, and lets you weight powerful machines by giving them more vnodes. Nearly every production system uses vnodes.
Step 3/ 61 min
A cache cluster of 4 nodes uses hash(key) mod 4. You add a 5th node → N goes 4→5 and roughly 80% of keys now map elsewhere, so the cache is almost entirely cold and the database gets slammed. Switch to consistent hashing with virtual nodes: adding the 5th node moves only about 1/5 (20%) of keys, the rest keep hitting warm cache, and load stays even.
Step 4/ 61 min
Use consistent hashing whenever nodes join/leave frequently and you want to minimize disruption: distributed caches, sharded databases and key-value stores, load distribution across a dynamic pool, and P2P systems.
Step 5/ 61 min
Remember "mod N breaks on N-change; the ring keeps ~(N-1)/N of keys home." And: vnodes = many small arcs = even load.
Don't forget virtual nodes. Plain consistent hashing with one point per node gives lumpy, uneven distribution and dumps a failed node's entire load onto a single neighbor. Virtual nodes are what make it practical.
Step 6/ 6· Worked examples1 min
Problem
Four cache servers route keys with server = hash(key) % N. When you add a 5th server, why do nearly all keys remap, and how does consistent hashing avoid that?
Try it on paper first.
Recall
Keep this
Fast revision of consistent hashing.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.