SD System Design In Depth
0%
lesson system-designcoordination

Distributed hash tables

Module 3.2: Distributed Coordination, Consensus & Leases Updated Sep 17, 2026
📄 Cheat Sheet
Kademlia, Explained
number 0 Kademlia, Explained Watch on YouTube ↗

'Kademlia, Explained' is a carefully animated walk-through of the XOR metric, k-buckets and iterative lookups, and is widely recommended as the clearest Kademlia explanation on YouTube.

0:00 / --:--

Decentralized Key-Value Routing Across Millions of Nodes

A Distributed Hash Table (DHT) is a decentralized distributed system that provides a lookup service similar to a hash table: key-value pairs are stored in the DHT, and any participating node can efficiently retrieve the value associated with a given key. Unlike client-side consistent hashing (where a client knows all servers), DHTs scale to millions of uncoordinated, churning peer nodes (used in BitTorrent Mainline DHT, IPFS, and Ethereum).

Kademlia: The XOR Metric Space

The gold standard of DHTs is Kademlia (Maymounkov & Mazières, 2002). Kademlia defines distance between two 160-bit keys x and y as their bitwise Exclusive OR (XOR):

d(x, y) = x ⊕ y

The XOR metric is unidirectional, symmetric (d(x, y) = d(y, x)), and satisfies the triangle inequality (d(x, z) ≤ d(x, y) + d(y, z), with ordinary addition; with XOR in place of + it is the identity d(x, y) ⊕ d(y, z) = d(x, z)).

K-Bucket Routing Tree Under the Hood

flowchart TD Lookup["Find Key: 10100110... (Target Key)"] --> Hop1["Query closest known node in k-bucket (Distance: 2^159)"] Hop1 --> Hop2["Next Node returns k closest peers in its routing table (Distance: 2^158)"] Hop2 --> Hop3["Each hop is expected to halve the remaining XOR distance"] Hop3 --> FinalNode["Final Destination Node holding key reached in O(log N) hops!"]

Routing Efficiency: O(log N) Lookups via k-Buckets

  • Routing Tables (k-Buckets): Each node maintains a list of routing contacts partitioned by distance. For each 0 ≤ i < 160, a node keeps a list of k contacts (typically k=20) at distance between 2i and 2i+1.
  • Lookup Routing: A lookup iteratively queries α nodes in parallel (concurrency parameter α=3). Each hop is expected to at least halve the remaining XOR distance to the target key. Finding a key among 10,000,000 nodes takes about log2(107) ≈ 23 to 24 hops with high probability, and usually fewer in practice.

Worked example: a Kademlia lookup with 4-bit IDs

Node 0010 wants key 1101. XOR distance = 0010 XOR 1101 = 1111 (15), the maximum distance, so the key lies in the far half of the space.

  • 0010's k-bucket for "first bit differs" contains node 1011. Distance 1011 XOR 1101 = 0110 (6).
  • 0010 asks 1011 for its closest nodes to 1101. 1011 returns 1100 (distance 0001 = 1) and 1110 (distance 0011 = 3).
  • 0010 asks 1100, which has nothing closer. The k closest nodes, 1100 and 1110, are the ones to STORE or FIND_VALUE the key.

Every hop fixed at least one more leading bit. With 160-bit IDs and N nodes, the expected hop count is about log2 N. For 10 million peers that is about 23 hops, and in practice fewer, because each bucket holds k = 20 contacts and queries run α = 3 in parallel.

DHT designs compared

DHTDistanceRouting stateLookup hopsUsed by
ChordClockwise ring distanceO(log N) finger tableO(log N)Research, and the basis of many variants
Pastry / TapestryPrefix matchingO(log N) with a leaf setO(log N)Research, and Microsoft PAST
KademliaXORk-buckets, about 160 x k contacts at mostO(log N) with parallel queriesBitTorrent Mainline DHT, IPFS/libp2p, Ethereum discv4/v5
One-hop (Dynamo-style)Ring positionFull membership1Cassandra, Riak, DynamoDB internals

Why XOR works well

  • It is symmetric, so a node learns useful contacts from the queries it receives.
  • It is unidirectional: for a given point and distance there is exactly one other point, so lookups for the same key converge along the same paths, which makes caching along the path effective.
  • k-buckets prefer long-lived contacts. Measurements show nodes that have been up longer are more likely to stay up, so the routing table resists churn and flooding attacks.

Failure modes

  • Churn: BitTorrent peers often stay minutes to hours. Values need replication to k nodes and periodic republishing.
  • NAT and firewalls: many peers cannot accept inbound connections, which pollutes routing tables. libp2p uses AutoNAT and relays.
Check yourself Question 1 of 3
XOR closest node apply
From: Worked example: a Kademlia lookup with 4-bit IDs

With 4-bit Kademlia IDs, a lookup targets key 1001. Of the known nodes 1000, 1011, 1101 and 0001, which is closest by XOR distance?

Key Takeaways

Distributed Hash Tables scale decentralized key-value lookups to millions of nodes without central servers.
Kademlia defines routing distance using bitwise XOR (d(x, y) = x ⊕ y), ensuring symmetric, unidirectional routing.
Routing via k-buckets finds keys in O(log N) network hops with high probability, even in very large networks.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue