Skip to the workspace
Pebellslearn by building
Probabilistic & System-Scale Structures  ›  Lesson

Consistent Hashing

Consistent Hashing 26 minPlace nodes and keys on the same hash ring; a key belongs to the first node clockwise, so adding a node only steals one arc

dsa / Consistent Hashing

Explore each scene at your own pace.

01 · Add a node without reshuffling the whole cluster

Add a node without reshuffling the whole cluster

Consistent Hashing

Definition
Consistent hashing assigns keys to servers by placing both on a circular hash ring and giving each key to the next server clockwise. Adding or removing a server then moves only the keys on one arc, rather than reassigning almost every key.
In plain words
Picture servers and keys as points around a clock face, with each key belonging to the first server clockwise from it. A new server only takes over keys in the gap just before it, and everyone else stays where they are.

In short: plain sharding with hash(key) % num_nodes reassigns almost every key the moment num_nodes changes, because the modulus itself changed. Consistent hashing places both nodes and keys on the same hash ring (a fixed range like 0..359, wrapping around), and assigns each key to the first node it meets going clockwise. Adding a node only steals the arc of keys between it and the previous node clockwise — every other key's nearest node never changes. With few nodes, one node's arc can still end up much bigger than another's by chance; virtual nodes (each real node placed at many positions on the ring) fix that by averaging away the luck.

A hash ring with three nodes A, B, C in clockwise order B, D, C, A; adding node D only steals the shaded arc of keys between B and D — those keys used to belong to C, the next node clockwise, and A and B's keys never move

step through it
1import hashlib
2 
3def ring_position(item, seed, ring_size=24): # a small ring for a readable trace
4 h = int(hashlib.md5(f"{seed}:{item}".encode()).hexdigest(), 16)
5 return h % ring_size
6 
7def assign(keys, nodes, seed, ring_size=24):
8 positions = sorted((ring_position(n, seed, ring_size), n) for n in nodes)
9 for k in keys:
10 kp = ring_position(k, seed, ring_size)
11 owner = next((n for pos, n in positions if pos >= kp), positions[0][1])
12 print(f"key {k} (ring pos {kp}) -> {owner}")
13 
14nodes = ["A", "B", "C"]
15print("-- before adding D --")
16assign(["k1", "k3", "k4", "k6"], nodes, seed=3)
17print("-- after adding D --")
18assign(["k1", "k3", "k4", "k6"], nodes + ["D"], seed=3)

Only k6 moves — from C to the new D, because D's ring position (16) sits between B (12) and C (19), right where k6 (15) already was. k1, k3 and k4 land on positions D can't reach, so their owners don't change at all.

Add a node without reshuffling the whole cluster

Quarry compares how many of 8 keys actually move when a third node joins, under consistent hashing versus plain modulo sharding:

python

Consistent hashing moves 1 key; modulo sharding moves 7 of the same 8 — because hash(key) % 2 and hash(key) % 3 agree on almost nothing, even though only one node was added.

Scene 1 of 6
Project context and further resources
You're building a piece ofQuarry — the storage engine
This piece — keys_moved_on_add(): Adds a storage node while moving as little data as possible.
Scenario Quarry shards its data across storage nodes and needs adding a node — a routine, frequent operation — to reshuffle as little data as possible.
keys_moved_on_add.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
keys_moved_on_add(keys, nodes, new_node, seed) → intHow many keys change owner when new_node joins the ring, out of the whole key set.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.