# Kademlia

**Kademlia**是一种点对点分布式哈希表（DHT）算法，由纽约大学的彼得·马伊蒙科夫（Petar Maymounkov）和大卫·马齐埃（David Mazières）于2002年提出。在该算法中，节点通过共享键空间上基于异或的距离度量来路由查询和存储数据。论文所声明的贡献包括：在易故障环境中可证明的一致性和性能、作为查找副作用的配置信息自动传播，以及能够容忍节点故障而不对用户施加超时延迟的并行异步查询。<sup>[1](https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | 纽约大学彼得·马伊蒙科夫和大卫·马齐埃的论文，2002年<sup>[1](https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf)</sup><sup> • </sup><sup>[2](https://blockchainlab.com/research/corpus/papers/kademlia)</sup> |
| 键空间 | 不透明的160位键（例如数据的SHA-1哈希）；每个节点在同一空间中持有一个节点ID<sup>[1](https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf)</sup> |
| 距离度量 | 两个ID的异或，解释为无符号整数；值越小表示越近<sup>[3](https://www.bittorrent.org/beps/bep_0005.html)</sup> |
| 存储规则 | {键, 值}对存储在ID接近该键的节点上<sup>[1](https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf)</sup> |
| 查找成本 | 迭代查找以对数步数向目标推进，在网络规模上给出O(log N)的查找时间<sup>[4](https://docs.ipfs.tech/concepts/dht/)</sup> |
| 部署 | BitTorrent主线DHT（BEP 5）、eMule的Kad网络、IPFS/libp2p，以及以太坊的Portal Network规范<sup>[3](https://www.bittorrent.org/beps/bep_0005.html)</sup><sup> • </sup><sup>[5](https://mirage.cs.uoregon.edu/pub/iptps09-montra/)</sup><sup> • </sup><sup>[4](https://docs.ipfs.tech/concepts/dht/)</sup><sup> • </sup><sup>[6](https://github.com/ethereum/portal-network-specs/blob/master/portal-wire-protocol.md)</sup> |
| 引用 | 2002年论文约有2.1千次索引引用<sup>[7](https://www.rankless.org/hit-papers/10.1007/3-540-45748-8_5)</sup> |

## 工作原理

**标识符与距离。** 每个参与节点在键空间中有一个标识符，每个存储项在同一空间中有一个键。在原始设计中，两者都是160位的量，例如某些较大数据的SHA-1哈希。两个标识符之间的距离是两个值的按位异或，读作无符号整数，因此异或结果越小表示ID越近。<sup>[1](https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf)</sup><sup> • </sup><sup>[3](https://www.bittorrent.org/beps/bep_0005.html)</sup> 由于异或是对称的，一个参与者收到的查找查询来自与它自身查询相同的键空间位置分布，作者在证明中使用了这一性质。<sup>[1](https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf)</sup>

异或度量使网络可以被视为一个有序列表。一个知道距离大约为1、2、4、8等处的其他对等节点的对等节点，可以在网络规模的对数时间内搜索该列表。<sup>[4](https://docs.ipfs.tech/concepts/dht/)</sup>

**路由表。** 每个节点维护一张联系人路由表，按异或距离组织成k桶。表在ID越接近节点自身ID时越详细：一个节点认识许多靠近自己的其他节点，而只认识极少数非常远的联系人。<sup>[3](https://www.bittorrent.org/beps/bep_0005.html)</sup> 由于对等节点随时加入、离开和重新加入，Kademlia对等节点不在每个距离尺度上保持单一链接；对于距离2的每个倍数，它保持最多K条链接。在IPFS中，K = 20，因此对等节点不是在距离128处保持一条链接，而是在距离65到128之间保持20条链接。参数K根据观察到的节点变动和重新发布频率来选择，以最大化网络保持连通且不丢失数据的概率。<sup>[4](https://docs.ipfs.tech/concepts/dht/)</sup>

**查找算法。** 要找到距离目标最近的K个节点，一个节点将它知道的最接近的联系人加载到查询队列中，向它们询问更近的联系人，并迭代地查询更接近目标的节点，直到找不到更近的节点为止。查询并行且异步运行；IPFS允许最多10个并发查询，并在最接近目标的三个对等节点已响应且无超时或错误后终止。<sup>[4](https://docs.ipfs.tech/concepts/dht/)</sup> 以太坊Portal Network规范直接将其节点和内容查找定义为Kademlia论文第2.3节的查找算法。<sup>[6](https://github.com/ethereum/portal-network-specs/blob/master/portal-wire-protocol.md)</sup>

**加入与维护。** 一个新节点必须知道至少一个现有参与者。它将该节点插入适当的k桶，发送查找请求以发现更多节点，然后通过在每个桶的范围内选择一个随机ID并对其执行查找，刷新所有距离比其最近邻居更远的k桶。<sup>[6](https://github.com/ethereum/portal-network-specs/blob/master/portal-wire-protocol.md)</sup> 桶通过普通流量保持新鲜，没有最近查找的桶会定期刷新；IPFS每10分钟刷新一次路由表。<sup>[6](https://github.com/ethereum/portal-network-specs/blob/master/portal-wire-protocol.md)</sup><sup> • </sup><sup>[4](https://docs.ipfs.tech/concepts/dht/)</sup>

## 设计特性

论文声称具有若干先前DHT未能同时提供的特性：更少的配置消息、通过查找自动传播配置信息、通过低延迟路径路由、避免失败节点超时延迟的并行异步查询，以及联系人记录算法对某些基本拒绝服务攻击的抵抗能力。若干性质在节点在线时间分布的弱假设下被形式化证明，作者用对现有对等系统的测量验证了这些假设。<sup>[1](https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf)</sup>

有序键空间视图的一个实际后果是，路由要求对等节点能够相互通信。如果某些对等节点不可达，例如位于网络地址转换器（NAT）或防火墙之后，一个对等节点可能被发送到死胡同，朝向它无法到达的内容。IPFS通过让节点经由libp2p的AutoNAT服务验证其公网可连接性，并在客户端模式（可以查询DHT但不能响应查询）和服务器模式（两者皆可）之间切换来解决这个问题。<sup>[4](https://docs.ipfs.tech/concepts/dht/)</sup> libp2p规范对此进行了推广：不受限制的节点应在服务器模式下运行，而具有间歇可用性或有限资源的受限节点应在客户端模式下运行，以免路由表被不可靠的联系人污染。<sup>[8](https://github.com/libp2p/specs/blob/master/kad-dht/README.md)</sup>

## 部署

**BitTorrent。** [BitTorrent](https://www.edgepedia.org/zh/bittorrent)用于无追踪器种子的“分布式松散哈希表”在BEP 5中规定，基于Kademlia并通过UDP实现。每个节点有一个从与BitTorrent信息哈希相同的160位空间中随机选择的节点ID，对等节点通过迭代查询最接近某种子信息哈希的节点来找到。<sup>[3](https://www.bittorrent.org/beps/bep_0005.html)</sup> 后来的BEP扩展了同一DHT：BEP 44增加了任意数据的存储，不可变项以数据本身的SHA-1哈希为键，可变项以签署它们的公钥为键，值限制在约1000字节。<sup>[9](https://www.bittorrent.org/beps/bep_0044.html)</sup>

**eMule的Kad网络。** Kad是eMule文件共享软件的一部分，是一个基于Kademlia的DHT，提供分布式关键词和文件搜索，拥有数百万同时在线用户；在Overnet网络消亡后，它成为最流行的基于DHT的点对点系统。<sup>[5](https://mirage.cs.uoregon.edu/pub/iptps09-montra/)</sup><sup> • </sup><sup>[10](https://journals.sagepub.com/doi/10.3233/FI-2011-518)</sup>

**IPFS与libp2p。** IPFS在256位地址空间上使用Kademlia DHT，以SHA256(PeerID)作为节点的位置，以SHA256(Record Key)作为记录应存储的位置。公共Amino DHT在libp2p协议标识符/ipfs/kad/1.0.0下运行，另有独立的局域网DHT在/ipfs/lan/kad/1.0.0下用于隔离网络。<sup>[4](https://docs.ipfs.tech/concepts/dht/)</sup><sup> • </sup><sup>[11](https://specs.ipfs.tech/routing/kad-dht/)</sup>

**以太坊。** Portal Network是以太坊通过DHT提供轻客户端数据的设计，其规定的路由表遵循Kademlia论文第2.4节描述的结构和演化，并采用其加入程序和刷新规则。<sup>[6](https://github.com/ethereum/portal-network-specs/blob/master/portal-wire-protocol.md)</sup>

## 安全性

DHT易受女巫攻击，即单一实体控制多个恶意节点。由于每个对等节点负责根据其位置索引内容空间的一个范围，攻击者可以伪造ID将女巫节点插入特定区域，并拒绝对目标内容的访问。<sup>[12](https://arxiv.org/abs/2505.01139)</sup> BitTorrent的DHT安全扩展（BEP 42）正是针对这种模式：它根据节点的外部[IP地址](https://www.edgepedia.org/zh/ip-address)限制节点可以选择哪些节点ID，使得启动八个或更多ID接近目标信息哈希的节点来托管并阻止对其访问变得更加困难。<sup>[13](https://www.bittorrent.org/beps/bep_0042.html)</sup>

对已部署Kademlia系统的攻击仍然是一个活跃的关切。2025年一项关于IPFS的研究提出了一种主动[女巫攻击](https://www.edgepedia.org/zh/sybil-attack)，其中恶意节点返回语义正确但虚假的数据，在最新的Kubo版本上完全遮蔽内容，并在针对最新已知缓解措施时拒绝约80%的查找尝试；作者提出了SR-DHT-Store，一种基于由动态计算的到目标ID的异或距离所定义的区域查询的抗女巫发布策略。<sup>[12](https://arxiv.org/abs/2505.01139)</sup> 对eMule Kad网络的测量工作同样表明，简单的攻击可以阻碍网络、审查内容或损害外部Web服务器，并认为在没有某种集中式认证和验证权威的情况下，此类攻击在完全去中心化的系统中难以阻止。<sup>[10](https://journals.sagepub.com/doi/10.3233/FI-2011-518)</sup>

## References

1. [Petar Maymounkov, David Mazières (2002). Kademlia: A Peer-to-peer Information System Based on the XOR Metric.](https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf)
2. [Kademlia paper record, Blockchain Lab Research Corpus (BLC-STUDY-0189).](https://blockchainlab.com/research/corpus/papers/kademlia)
3. [BEP 5: DHT Protocol, BitTorrent.org.](https://www.bittorrent.org/beps/bep_0005.html)
4. [Distributed Hash Tables (DHT), IPFS Docs.](https://docs.ipfs.tech/concepts/dht/)
5. [Large-Scale Monitoring of DHT Traffic, IPTPS 2009.](https://mirage.cs.uoregon.edu/pub/iptps09-montra/)
6. [Portal Wire Protocol, ethereum/portal-network-specs.](https://github.com/ethereum/portal-network-specs/blob/master/portal-wire-protocol.md)
7. [Kademlia: A Peer-to-Peer Information System Based on the XOR Metric, citation record, Rankless.](https://www.rankless.org/hit-papers/10.1007/3-540-45748-8_5)
8. [libp2p Kademlia DHT specification, libp2p/specs.](https://github.com/libp2p/specs/blob/master/kad-dht/README.md)
9. [BEP 44: Storing arbitrary data in the DHT, BitTorrent.org.](https://www.bittorrent.org/beps/bep_0044.html)
10. [Locher, Schmid, Wattenhofer (2011). eDonkey & eMule's Kad: Measurements & Attacks. Fundamenta Informaticae.](https://journals.sagepub.com/doi/10.3233/FI-2011-518)
11. [IPFS Kademlia DHT specification, specs.ipfs.tech.](https://specs.ipfs.tech/routing/kad-dht/)
12. [Netto, Cholez, Ignat (2025). Active Sybil Attack and Efficient Defense Strategy in IPFS DHT. arXiv:2505.01139.](https://arxiv.org/abs/2505.01139)
13. [BEP 42: DHT Security extension, BitTorrent.org.](https://www.bittorrent.org/beps/bep_0042.html)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Data structures › Hashing and hash tables

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/kademlia

English original: https://www.edgepedia.org/kademlia
