BitTorrent's DHT and IPFS both route lookups across millions of peers with no ring, no clockwise successor, no central index — just a way to measure "distance" between two ids that has nothing to do with where either one sits on a number line. What could "distance" even mean if not that?
Click through the three targets. The two "closest" nodes are a completely different pair each time — not because the nodes moved, but because XOR distance depends only on which bits differ, not on how far apart the numbers look.
Kademlia measures distance between two ids by XOR-ing them together and reading the result as a number:
Every node keeps its known peers in k-buckets: bucket holds peers at distance from itself. A lookup for target simply asks whichever known peers are currently closest to , who in turn know peers even closer — narrowing in each round.
- — bitwise XOR: a 1 in a bit position exactly where and differ there.
- bucket — holds peers at distance to from self; low means "very close."
- closest- — the known ids with the smallest to a target ; the contact list a lookup uses.
- XOR ignores magnitude entirely
depends only on which bits differ, never on which number is "bigger" — ids and can be just as close as ids and , if the right bits happen to match.
- Distance is symmetric and zero only for identical ids
, and exactly when — so XOR behaves like a genuine distance, just not a familiar numeric one.
Drag the target id across the whole range and switch between 1, 2, and 3. The bars resort completely with every move — there's no "nearby" region of the number line the way there was on Chord's ring; closeness is purely bitwise.
Self is node (binary ); other known nodes are :
- Every node's k-bucket, by hand
- (bucket 0)
- (bucket 1)
- (bucket 2, since )
- (bucket 2, since )
- (bucket 3, since )
- (bucket 3, since )
- Closest 3 to target 10
Sorted ascending, the 3 nearest are — none of them is even in the same bucket as another.
- Closest 2 to target 1
The 2 nearest are — the same pair that shares bucket 2 relative to self, because both happen to share their two lowest bits with target .
Slide the target id so the 2 nearest contacts (by XOR distance) are exactly nodes 0 and 3.
Kademlia replaces Chord's ring-and-successor routing with a metric that needs no ordering at all: XOR distance, plus k-buckets that keep detailed knowledge of nearby ids and coarse knowledge of far ones. It's the same "narrow in toward the target, one hop at a time" idea as Chord's finger tables, just built on a different notion of "close" — and it's the routing scheme underneath BitTorrent's mainline DHT and IPFS's peer discovery today.