|
1 | 1 | --- |
2 | 2 | title: "Kademlia DHT" |
3 | | -description: "" |
| 3 | +description: "The libp2p Kad-DHT subsystem is an implementation of the Kademlia |
| 4 | +DHT, a distributed hash table." |
4 | 5 | weight: 25 |
5 | 6 | --- |
6 | 7 |
|
7 | | -Coming soon! |
| 8 | +## Overview |
8 | 9 |
|
9 | | -For now, check out the following [Launchpad guide on the DHT](https://curriculum.pl-launchpad.io/curriculum/libp2p/dht/). |
| 10 | +The Kademlia Distributed Hash Table (DHT), or Kad-DHT, is a distributed hash table |
| 11 | +that is designed for P2P networks. |
| 12 | + |
| 13 | +Kad-DHT in libp2p is a subsystem based on the |
| 14 | +[Kademlia whitepaper](https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf). |
| 15 | + |
| 16 | +Kad-DHT offers a way to find nodes and data on the network by using a |
| 17 | +[routing table](https://en.wikipedia.org/wiki/Routing_table) that organizes peers based |
| 18 | +on how similar their keys are. |
| 19 | + |
| 20 | +<details> |
| 21 | + <summary>A deeper look</summary> |
| 22 | + |
| 23 | + The routing table is organized based on a prefix length and a distance metric. |
| 24 | + The prefix length helps to group similar keys, and the distance metric helps to |
| 25 | + find the closest peers to a specific key in the routing table. The table maintains |
| 26 | + a list of `k` closest peers for each possible prefix length between `0` and `L-1`, |
| 27 | + where `L` is the length of the keyspace, determined by the length of the hash |
| 28 | + function used. **Kad-DHT uses SHA-256**, with a keyspace of 256 bits, trying to maintain |
| 29 | + `k` peers with a shared key prefix for every prefix length between `0` and `255` in |
| 30 | + its routing table. |
| 31 | + |
| 32 | + The prefix length measures the proximity of two keys in the routing table and |
| 33 | + divides the keyspace into smaller subspaces, called "buckets", each containing nodes |
| 34 | + that share a common prefix of bits in their SHA-256 hash. The prefix length is the |
| 35 | + number of bits that are the same in the two keys' SHA-256 hash. The more leading bits |
| 36 | + that are the same, the longer the prefix length and the closer the proximity of the |
| 37 | + two keys are considered to be. |
| 38 | + |
| 39 | + The distance metric is a way to calculate the distance between two keys by |
| 40 | + taking the bitwise exclusive-or (XOR) of the SHA-256 hash of the two keys. The |
| 41 | + result is a measure of the distance between the two keys, where a distance of |
| 42 | + `0` means the keys are identical, and a distance of `1` means that only one |
| 43 | + bit is different, meaning the two keys are close to each other (i.e. their |
| 44 | + SHA-256 hashes are similar). |
| 45 | + |
| 46 | + This design allows for efficient and effective lookups in the routing table when |
| 47 | + trying to find nodes or data that share similar prefixes. |
| 48 | + |
| 49 | +</details> |
| 50 | + |
| 51 | +## Peer routing |
| 52 | + |
| 53 | +The Kad-DHT uses a process called "peer routing" to discover nodes in the |
| 54 | +network. When looking for a peer, the local node contacts the `k` closest nodes to |
| 55 | +the remote peer's ID asking them for closer nodes. The local node repeats the |
| 56 | +process until it finds the peer or determines that it is not in the network. |
| 57 | + |
| 58 | +## Content provider routing |
| 59 | + |
| 60 | +Kad-DHT also includes a feature for content provider discovery, where nodes can |
| 61 | +look up providers for a given key. The local node again contacts the `k` closest |
| 62 | +nodes to the key asking them for either providers of the key and/or closer nodes |
| 63 | +to the key. The local node repeats the process until it finds providers for the |
| 64 | +key or determines that it is not in the network. |
| 65 | + |
| 66 | +## Bootstrap process |
| 67 | + |
| 68 | +To maintain a healthy routing table and discover new nodes, the Kad-DHT includes |
| 69 | +a bootstrap process that runs periodically. The process starts by generating a random peer |
| 70 | +ID and looking it up via the peer routing process. The node then adds the closest peers it |
| 71 | +discovers to its routing table and repeats the process multiple times. This process also |
| 72 | +includes looking up its own peer ID to improve awareness of nodes close to itself. |
| 73 | + |
| 74 | +{{< alert icon="💡" context="note" text="See the Kademlia DHT <a class=\"text-muted\" href=\"https://github.com/libp2p/specs/tree/master/kad-dht\">technical specification</a> for more details." />}} |
0 commit comments