Skip to content

PAPER EXPLAINED

Kademlia: A Peer-to-Peer Information System Based on the XOR Metric

IntermediateAbout 35 min to read the paper

TL;DR

Kademlia is a peer-to-peer distributed hash table that measures distance between IDs with XOR. That one choice drives a simple routing table, parallel lookups that tolerate failures, and a proof sketch of logarithmic lookups and reliable storage.

What problem does Kademlia solve?

A distributed hash table must let any participant find the nodes responsible for a key, even though nodes constantly join and fail. Earlier designs had drawbacks. In Chord, distance is asymmetric, so nodes learn little from the queries they receive and routing tables are rigid. Pastry switches distance metrics partway through a lookup, which creates discontinuities and makes worst-case analysis hard. Designers also wanted lookups that avoid timeout delays from dead nodes, routing state that spreads cheaply, and resistance to basic denial-of-service attacks.

The core idea

Give nodes and keys 160-bit IDs and define the distance between two IDs as their XOR. This distance is symmetric, so a node hears from the same kinds of nodes it stores in its routing table, and ordinary traffic keeps that table fresh. It is also unidirectional, so lookups for the same key from anywhere converge along the same path, which makes caching effective. Each node keeps k-buckets of contacts at exponentially growing distances and prefers long-lived contacts. Lookups query several of the closest known nodes in parallel and repeat with ever-closer nodes, using a single algorithm from start to finish.

IN THE AUTHORS’ WORDS

“We describe a peer-to-peer distributed hash table with provable consistency and performance in a fault-prone environment.”

Kademlia: A Peer-to-Peer Information System Based on the XOR Metric, p. 1

“Finally, Kademlia is the first peer-to-peer system to exploit the fact that node failures are inversely related to uptime.”

Kademlia: A Peer-to-Peer Information System Based on the XOR Metric, p. 13

How Kademlia works

  1. Nodes and keys share a 160-bit ID space. The distance between two IDs is their XOR read as an integer. Key-value pairs are stored on the k nodes whose IDs are closest to the key.
  2. For each distance range from 2^i to 2^(i+1), a node keeps a k-bucket of up to k contacts sorted by when each was last seen. Every message updates the sender's entry. When a bucket is full, the oldest contact is pinged and is replaced only if it fails to respond.
  3. The protocol has four RPCs: PING, STORE, FIND_NODE and FIND_VALUE. Each RPC carries a random 160-bit RPC ID that the recipient must echo, which gives some protection against address forgery.
  4. Node lookup: the initiator sends alpha parallel FIND_NODE requests to the closest nodes it knows. It then repeatedly queries not-yet-asked nodes among the k closest it has heard of. The lookup ends once it has queried and heard back from the k closest nodes it has seen.
  5. To store a value, a node finds the k closest nodes and sends them STORE requests. To retrieve one, it uses FIND_VALUE, stops as soon as any node returns the value, and caches the pair at the closest node that did not have it.
  6. The routing table is a binary tree of k-buckets. A full bucket is split only if its range includes the node's own ID, plus extra splits so the node knows every contact in the smallest surrounding subtree that has at least k nodes. A new node joins by looking up its own ID and refreshing its buckets.
  7. Key-value pairs are republished hourly to cope with churn. A node that has just received a STORE for a pair skips its own republish for the next hour, and bucket refreshes replace per-key lookups before republishing.

Results

The paper gives a proof sketch rather than experiments. It argues that, with overwhelming probability, lookups take about log n plus a small constant number of steps, and that a lookup returns a stored key. If buckets on different nodes do not overlap, a lookup fails only if k nodes on the path each lose k nodes in the same bucket, which happens with probability 2^(-k^2). If new nodes have a 1/2 chance of lasting an hour, a pair survives an hour on at least one of its k closest nodes with probability 1 - 2^(-k). Its only empirical support is an analysis of Gnutella trace data collected by others, showing that longer uptime predicts staying online. The authors note that a key is lost if all k closest nodes fail and the pair was not cached elsewhere.

Limitations and what came later

  • There is no experimental evaluation of Kademlia's own lookup latency, bandwidth or failure behavior. Its support is a proof sketch plus Gnutella uptime data from another study.
  • The analysis is explicitly a sketch. It relies on assumptions such as randomly chosen node IDs, an invariant on bucket contents, and k being chosen so that k nodes rarely fail within an hour.
  • A key-value pair is lost if all k closest nodes fail before it is republished and it has not been cached elsewhere.
  • The denial-of-service resistance claimed covers only certain basic attacks, mainly attempts to flush routing tables by flooding the system with new nodes.
  • As described, the bucket-eviction rule would need many PING messages. The implementation adds a replacement cache and delayed probing to reduce this traffic.

Why Kademlia matters

The paper shows how one carefully chosen distance function can unify the routing table, lookup, data placement and caching of a peer-to-peer system, and make it easier to analyze. Its practical ideas speak to the main difficulties of large, unreliable networks: learning contacts from ordinary traffic, sending parallel queries to avoid timeouts, and favoring long-lived nodes. The comparisons with Chord, Pastry, Tapestry and Plaxton's algorithm make clear which trade-offs the design is addressing.

Kademlia is a compact, readable design paper that shows how one simple idea, measuring distance between IDs with XOR, can make a peer-to-peer lookup system easier to build and easier to reason about. The same XOR distance drives the routing table, the lookup procedure, data placement, caching and the proof sketch, so the paper is a good lesson in how a single well-chosen abstraction can unify a whole design. It also explains several practical choices. Contacts are learned as a side effect of ordinary traffic. Lookups run in parallel, so one slow or dead node does not stall a request. Older, long-lived contacts are preferred, based on measured uptime data from Gnutella. The authors compare their choices directly with Chord and Pastry, which helps you see what problem each design decision is meant to solve. If you want to understand how a distributed hash table routes requests and copes with nodes constantly joining and leaving, this paper rewards a careful read.

Before you read

Who it’s for

Students and engineers interested in distributed systems, peer-to-peer networks or distributed hash tables who are comfortable with binary numbers, basic networking (UDP, RPC) and informal big-O and probability arguments.

What to know first

  • Binary representation of numbers and the bitwise XOR operation
  • What a hash table is and the idea of a distributed hash table (DHT)
  • Basic networking concepts: IP addresses, UDP, remote procedure calls (RPCs)
  • Logarithmic complexity and simple probability arguments
  • Helpful but optional: a rough idea of how Chord or Pastry route lookups

Glossary

Distributed hash table (DHT)
A system spread across many computers that stores key-value pairs and lets any participant find the nodes responsible for a given key.
XOR metric
Kademlia's distance between two IDs: their bitwise exclusive or, read as an integer. It is symmetric, satisfies the triangle inequality, and is unidirectional.
Unidirectional
For any point x and any distance greater than zero, exactly one point lies at that distance from x. As a result, lookups for the same key converge along the same path no matter where they start.
k-bucket
A list of up to k contacts (IP address, UDP port, node ID) at distances between 2^i and 2^(i+1) from the node, ordered by when each was last seen.
k
A system-wide replication parameter: the bucket size and the number of closest nodes that store each key. It is chosen so that k given nodes are very unlikely to all fail within an hour (example value: 20).
alpha (concurrency parameter)
The number of parallel, asynchronous requests a node sends during each step of a lookup (example value: 3).
Node lookup
The procedure for finding the k nodes closest to a given ID by repeatedly querying ever-closer nodes. It is the basis for most other operations.
Replacement cache
An implementation detail: a list of newly seen contacts that can replace stale entries in a full k-bucket the next time that bucket's contacts are queried.
Republishing
Periodically re-sending key-value pairs so they stay on the k nodes closest to the key as nodes leave and join.

What to pay attention to

  • How XOR distance maps onto the binary-tree picture: the distance between two IDs is set by the highest bit where they differ (page 4).
  • Why symmetry of XOR matters: nodes receive queries from the same kinds of nodes they keep in their routing tables, so incoming traffic refreshes routing state (pages 1-2, 10-11).
  • The k-bucket update rule: a full bucket keeps a live old contact rather than replacing it with a new one, and the Gnutella uptime data in Figure 3 that motivates this (page 5).
  • The node lookup loop: alpha parallel FIND_NODE requests, choosing from the k closest nodes heard of, and the stopping condition (page 6).
  • The routing-table splitting rule, including the extra splits used to handle highly unbalanced trees (pages 7-8).
  • The republishing optimizations that stop every one of the k holders from republishing every hour (pages 8-9).
  • What the proof sketch actually assumes, in particular the invariant about bucket contents and the choice of k (pages 10-11).

You can skip it if… Skip it if you only need a high-level picture of what a DHT is, or if you want an empirical evaluation with benchmarks: the paper offers a design, a proof sketch and one borrowed uptime measurement, not performance experiments of Kademlia itself.

Questions to keep in mind

  • Why does using a single metric (XOR) from start to finish make Kademlia easier to analyze than systems that switch metrics in a second routing phase?
  • What does unidirectionality of the XOR metric guarantee, and how does that make caching along lookup paths effective?
  • Why would preferring old live contacts over new ones protect against an attacker who floods the system with new nodes?
  • How does the concurrency parameter alpha trade bandwidth for lower latency and tolerance of failed nodes?
  • Which claims in the paper are supported by proof sketches, which by external measurements, and which are not evaluated at all?

Kademlia quiz: check your understanding

1. How does Kademlia define the distance between two 160-bit identifiers?

Show the answer

As their bitwise exclusive or (XOR), interpreted as an integer. The paper notes that this is a valid, though non-Euclidean, metric: distance from an ID to itself is zero, it is symmetric, and it satisfies the triangle inequality.

2. What is a k-bucket, and which contacts does bucket i hold?

Show the answer

For each i from 0 up to 160, a node keeps a list of (IP address, UDP port, Node ID) triples for nodes whose distance from it lies between 2^i and 2^(i+1). Each list holds up to k entries and is sorted by when each contact was last seen, with the least recently seen at the head and the most recently seen at the tail.

3. When a node hears from a new contact but the matching k-bucket is already full, what happens?

Show the answer

The node pings the least recently seen contact in that bucket. If that contact fails to respond, it is evicted and the new sender is added at the tail. If it responds, it moves to the tail and the new contact is discarded. Live nodes are therefore never removed in favor of new ones.

4. What evidence motivates the preference for keeping old contacts?

Show the answer

The authors analyzed Gnutella trace data collected by Saroiu et al. Figure 3 shows that the longer a node has been up, the more likely it is to stay online for another hour.

5. What are Kademlia's four RPCs?

Show the answer

PING (check whether a node is online), STORE (ask a node to store a key-value pair), FIND_NODE (return the k nodes the recipient knows that are closest to a target ID), and FIND_VALUE (like FIND_NODE, but return the stored value if the recipient has one for the key).

6. What role does the parameter alpha play in a node lookup, and what example value is given?

Show the answer

Alpha is a system-wide concurrency parameter: the number of parallel, asynchronous FIND_NODE requests the initiator sends in each round. The paper gives 3 as an example. With alpha set to 1, the lookup resembles Chord's in message cost and in the latency of detecting failed nodes.

7. How does Kademlia prevent popular keys from being cached at too many nodes?

Show the answer

A cached key-value pair expires on a timer. The expiration time is made exponentially inversely proportional to the number of nodes between the caching node and the node whose ID is closest to the key.

8. In the implementation, what does the parameter b change, and what value does the current implementation use?

Show the answer

Routing considers IDs b bits at a time rather than one bit. This enlarges the routing table to an expected 2^b log_{2^b} n k-buckets and reduces the expected hops per lookup from log_2 n to log_{2^b} n. The current implementation uses b = 5.

Your answers stay on this page and are cleared when you leave.

Go a little deeper

The proof sketch rests on the invariant that every k-bucket holds at least one contact whenever a node exists in its range, and on k being chosen so that k nodes rarely all fail within an hour. How robust are these assumptions when node churn is heavy or correlated, and which parts of the design would break first?

Read next

All papers explained →

Cite Kademlia

Maymounkov, P., & Mazières, D. (2002). Kademlia: A Peer-to-Peer Information System Based on the XOR Metric. IPTPS (LNCS 2429). https://doi.org/10.1007/3-540-45748-8_5

@inproceedings{maymounkov2002kademlia,
  title = {{Kademlia: A Peer-to-Peer Information System Based on the XOR Metric}},
  author = {Petar Maymounkov and David Mazières},
  year = {2002},
  booktitle = {IPTPS (LNCS 2429)},
  doi = {10.1007/3-540-45748-8_5},
  url = {https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf}
}

DOI: 10.1007/3-540-45748-8_5 · Original PDF ↗

Study Kademlia in ReadingTrack

Add the paper to your ReadingTrack library with its authors, venue and year, and get this page’s quiz as suggested review cards. You choose which cards to keep; nothing enters your reviews until you accept it.

Last reviewed 26 November 2026 by the ReadingTrack editors. Explanations are our own; quotes are short and attributed, and the paper is linked at its original source.