Kademlia DHT
The libp2p Kad-DHT subsystem is an implementation of the Kademlia DHT, a distributed hash table.
Overview
The Kademlia Distributed Hash Table (DHT), or Kad-DHT, is a distributed hash table that is designed for P2P networks.
Kad-DHT in libp2p is a subsystem based on the Kademlia whitepaper.
Kad-DHT offers a way to find nodes and data on the network by using a routing table that organizes peers based on how similar their keys are.
Details
Peer routing
The Kad-DHT uses a process called "peer routing" to discover nodes in the
network. When looking for a peer, the local node contacts the k closest nodes to
the remote peer's ID asking them for closer nodes. The local node repeats the
process until it finds the peer or determines that it is not in the network.
Content provider routing
Kad-DHT also includes a feature for content provider discovery, where nodes can
look up providers for a given key. The local node again contacts the k closest
nodes to the key asking them for either providers of the key and/or closer nodes
to the key. The local node repeats the process until it finds providers for the
key or determines that it is not in the network.
Bootstrap process
To maintain a healthy routing table and discover new nodes, the Kad-DHT includes a bootstrap process that runs periodically. The process starts by generating a random peer ID and looking it up via the peer routing process. The node then adds the closest peers it discovers to its routing table and repeats the process multiple times. This process also includes looking up its own peer ID to improve awareness of nodes close to itself.