
A Kademlia DHT is a distributed hash table that finds peers and data by measuring the distance between IDs with XOR. Petar Maymounkov and David Mazières published it in 2002, and IPFS runs its own version, the Amino DHT, so your node can find who holds a CID.
TL;DR:
A distributed hash table spreads one large key-value table across many computers, so no single server holds the index. Kademlia is one recipe for it: every node gets an ID in the same space as the keys, and each record sits on the nodes closest to its key.
In IPFS both kinds of ID come from SHA-256. Your node's Kademlia ID is the SHA-256 of its Peer ID, and a CID's position is the SHA-256 of the multihash inside the CID. That gives a 256-bit key space.
The IPFS DHT holds provider records (which peers hold a CID), IPNS records for mutable names, and peer records (where a Peer ID can be reached). The public network, Amino, speaks the libp2p protocol /ipfs/kad/1.0.0. Our guide to how IPFS finds and retrieves files shows where the DHT sits next to Bitswap, while this post stays inside the algorithm.
Kademlia XORs two IDs bit by bit and reads the result as an unsigned number. Matching bits become 0 and differing bits become 1, so IDs that share a long run of leading bits give a small number. That shared prefix length is the figure you will meet most often.
XOR behaves like a real distance: an ID is 0 from itself, A to B equals B to A, and the triangle inequality holds. Because exactly one point lies at any given distance from another, lookups for the same key converge along the same path.
You can test this with the sample multihash and Peer ID in the IPFS DHT spec. We ran this script here with the Python 3 standard library:
import hashlib
multihash = bytes.fromhex("1220e536c7f88d731f374dccb568aff6f56e838a19382e488039b1ca8ad2599e82fe")
peer_id = bytes.fromhex("0024080112209e3b433cbd31c2b8a6ebbdca998bd0f4c2141c9c9af5422e976051b1e63af14d")
content_key = int.from_bytes(hashlib.sha256(multihash).digest(), "big")
peer_key = int.from_bytes(hashlib.sha256(peer_id).digest(), "big")
distance = content_key ^ peer_key
print(f"{content_key:064x}"[:16], f"{peer_key:064x}"[:16])
print("shared prefix bits:", 256 - distance.bit_length())
# Output (run here):
# d623250f3f660ab4 e43d28f0996557c0
# shared prefix bits: 2Both keys match the spec. They share only 2 leading bits, so in Kademlia terms this peer sits far from that content.
Each node splits its routing table into k-buckets by shared prefix length. Bucket 0 holds peers whose first bit differs from yours, and bucket 1 holds peers that differ first at the second bit. Half of all random IDs fall into bucket 0 and a quarter into bucket 1, so you know many peers near you and a thin sample of the far side.

The routing table feeds the lookup. Values from the IPFS Kademlia DHT spec.
Each bucket holds at most k peers, and in IPFS k = 20. The paper chose k so that any k nodes are unlikely to all fail within an hour. The IPFS spec gives that 20 three jobs: bucket size, peers returned per lookup, and peers that store each record.
When a bucket is full, Kademlia keeps the peers it already has while they stay online, because nodes that have been up longer tend to stay up. A DHT server refreshes its table every 10 minutes, drops peers that stop answering and refills thin buckets with lookups for random IDs. To make Sybil attacks harder, the spec also asks servers to keep at most 2 peers from one IP group per bucket.
Every DHT operation starts with one lookup that finds the k peers closest to a key X:
The lookup is iterative: your node contacts every hop itself, and the spec rules out recursive forwarding because it can enable amplification attacks. Each round gets at least one bit closer to X, so a lookup takes about O(log N) steps in a network of N peers.
Record operations reuse this walk. To announce a CID, your node sends ADD_PROVIDER to the 20 peers closest to the SHA-256 of its multihash, and a later GET_PROVIDERS walk collects any provider record on the way. Records expire after 48 hours, which is why provider records get renewed, and Kubo speeds that work up with optimistic provide and provide sweep.
Only DHT servers answer: a reachable node stores records and replies, while a node behind NAT runs as a client that only asks. A new node joins through a bootstrap node, then looks up random IDs and its own ID to fill its buckets.
IPFS kept the core idea and changed the parameters:
| 2002 paper | IPFS Kademlia DHT | |
|---|---|---|
| Key size | 160 bits | 256 bits (SHA-256) |
| k | System-wide, for example 20 | 20 |
| Parallel queries (α) | For example 3 | 10 |
| Lookup | Initiator queries each hop | Iterative; recursive ruled out |
| Transport | UDP | libp2p over TCP or QUIC |
| Record lifetime | 24 hours unless republished | Provider records 48 hours, republished every 22 hours |
BitTorrent's Mainline DHT (BEP 5) also builds on Kademlia, with 160-bit IDs over UDP and buckets of 8 nodes.
If you run Kubo, these commands from its CLI reference show the DHT at work (quoted, not run: no IPFS binary here):
# WAN routing table stats (format may change)
ipfs stats dht wan
# Provider Peer IDs for a CID (default limit: 20)
ipfs routing findprovs --num-providers=5 <cid>
# Addresses for a Peer ID
ipfs routing findpeer <peerID>Kubo's default Routing.Type of auto starts your node as a DHT client and switches to server mode once the node is reachable. It also queries the HTTP routers that delegated routing explains.
Content you pin with AIOZ Pin "becomes available on the public IPFS network", our gateway docs say, so anyone running an IPFS node can fetch it. A node with no connected peer holding your CID falls back to the lookup above. Our docs don't describe how AIOZ Pin announces provider records.
Pin by CID runs the other way: the service fetches a CID that already lives on IPFS. Keep the node holding it online until the status shows pinned. The documented request, with a placeholder CID:
curl --location --request POST 'https://api.aiozpin.network/api/pinning/pinByHash' \
--header 'pinning_api_key: KEY' \
--header 'pinning_secret_key: SECRET' \
--header 'Content-Type: application/json' \
--data-raw '{ "hash_to_pin": "<CID>", "metadata": { "name": "my-file" } }'Next step: the AIOZ Pin pinning API reference has the full request and response.
Is Kademlia the same as the IPFS DHT?
No. Kademlia is the algorithm. Amino is the public network that runs the IPFS version of it.
Why does IPFS use k = 20?
The IPFS docs say K comes from observed churn and the republish rate, so a record survives when some holders go offline.
What does XOR distance mean in Kademlia?
The bitwise XOR of two IDs read as a number, so IDs sharing more leading bits are closer.
Is the IPFS Kademlia lookup iterative or recursive?
Iterative: your node contacts each closer peer itself.
Does my node have to be a DHT server?
No. Nodes behind NAT should run as clients, which query but store no records.
In this cluster: DHT and provider records
Next in content routing

An ENS IPFS contenthash points your .eth name at a site on IPFS. See the byte format, a real encoding, ipfs vs ipns records, gas used, and common mistakes.

ERC-721 metadata JSON explained: the three fields EIP-721 defines, OpenSea attributes and animation_url, ipfs:// image links, and folder CIDs for collections.

Merkle DAG vs Merkle tree explained: a tree gives each node one parent and short proofs, while the DAG IPFS uses shares blocks, carries data and links by CID.

IPNI is the IPFS network indexer behind cid.contact. See how index providers publish signed advertisement chains and how one HTTP query finds providers.

On-chain vs off-chain NFT metadata: what the contract stores, what lives on IPFS or a web server, what each costs in gas, and how to check any token yourself.

A Kademlia DHT finds peers by XOR distance. See how k-buckets, k = 20 and iterative lookups let IPFS locate who holds a CID, and how to watch it on Kubo.