Back to the map

Module 11 · Architectural patterns · Lesson 6 of 9

Peer-to-peer

Systems with no central server: every node is both client and server. Unstructured versus structured overlays, distributed hash tables and how Chord and Kademlia route in log N hops, incentives and churn, NAT traversal.

DeepNice-to-knowSenior 12 minPro Updated Nov 2026

Why it matters

Peer-to-peer is the architecture with no privileged node: no server to overload, to take down or to trust. BitTorrent moves a large share of the internet's bulk data this way, blockchains and IPFS are built on it, and video calls traverse it. Even inside conventional systems, the structured overlays P2P invented (distributed hash tables, consistent hashing, gossip) are how clusters of ordinary servers organise themselves. Understanding P2P is understanding how a system coordinates when nothing is in charge.

The idea

In a peer-to-peer system every participant runs the same software and plays both roles: it requests resources from others and serves resources to others. There is no central coordinator for the data path; nodes find each other and route requests through an overlay network, a logical topology built on top of the internet. The design questions are how peers find content, how the network survives peers constantly joining and leaving (churn), and why anyone contributes.

Unstructured versus structured overlays

 Unstructured (Gnutella, early file sharing)Structured: distributed hash table (Chord, Kademlia, Pastry)
TopologyRandom connections between peersPeers arranged by node id in a key space with defined neighbour rules
Finding contentFlood or random-walk queries with a hop limitHash the key to an id; route to the peer responsible for that id
Cost per lookupMany messages; rare content may not be foundO(log N) hops, deterministic; always found if present
Churn handlingTrivial; connections are arbitraryMaintenance protocol keeps routing tables correct as peers come and go
Used byEarly networks; gossip-based membership todayBitTorrent trackerless mode, IPFS, Ethereum discovery, many storage systems internally
N8N21N38N47N56N1finger +16+8+4 → ownerlookup key 45each node keeps O(log N) fingerskeys replicate to successors for churn
A DHT: node ids and keys share a space; each node knows a few others at increasing distances; a lookup halves the remaining distance each hop and reaches the responsible node in about log N hops.

How a DHT routes

Chord places nodes and keys on a ring (the same ring as consistent hashing) and gives each node a finger table: pointers to the nodes responsible for ids at distances 1, 2, 4, 8 and so on around the ring. A lookup forwards to the finger closest to but not past the target, halving the distance each hop, so any key is found in about log N hops among N nodes. Kademlia uses XOR distance between 160-bit ids and keeps k-buckets of peers per distance range, which makes routing tables self-healing from ordinary traffic and is why it won in practice (BitTorrent's Mainline DHT, IPFS). Keys are stored on the responsible node and replicated to its neighbours so churn does not lose them.

Incentives and churn

Peers leave without notice, so data must be replicated and routing tables refreshed continuously; a DHT with high churn spends much of its bandwidth on maintenance. Peers also have no obligation to contribute, so systems add incentives: BitTorrent's tit-for-tat uploads to peers that upload to you, and rarest-first piece selection keeps swarms healthy; blockchains pay in tokens. Without incentives, free riding collapses the network.

NAT traversal and bootstrapping

Most peers sit behind NAT and cannot be reached directly. The WebRTC lesson's machinery, STUN to learn public addresses, hole punching, and TURN or relay peers as a fallback, applies to every P2P system. New peers need a way in: bootstrap nodes with known addresses, which are the one centralised ingredient most networks retain (trackers, seed lists, DNS seeds).

Where P2P ideas live today

Fully decentralised consumer systems are rare outside file sharing, cryptocurrencies and real-time media. The ideas, though, are everywhere inside data centres: Dynamo and Cassandra are DHT-like rings of equal nodes with gossip membership; consistent hashing came from the same research; service meshes and CDNs use overlay routing. The most common modern use is hybrid: a central service for discovery, identity and incentives, with peers exchanging the heavy data directly (game updates, video calls, large downloads).

 Client-serverPeer-to-peer
ScalingAdd servers; cost grows with usersCapacity grows with users; each brings bandwidth and storage
Single point of failure or controlThe serverNone for the data path; bootstrap nodes for entry
Consistency and trustCentral authority is simpleMust verify content (hashes) and handle untrusted peers
Latency and predictabilityControlled infrastructureVariable; depends on peers' links and churn
FitsMost applicationsBulk distribution, censorship resistance, real-time media between participants, decentralised ledgers
Peer-to-peer · Backplane