Peer-2-Peer

Peer-to-Peer (P2P)

Peer-to-Peer (P2P) is a network architecture where participants (peers) communicate directly with each other rather than through a central server. Each peer acts as both client and server — it consumes resources from other peers and contributes resources back. This contrasts with the client-server model where clients consume and servers provide.

P2P eliminates the central server as a bottleneck and single point of failure. The tradeoff is coordination complexity: without a central authority, peers must discover each other, agree on data consistency, and handle churn (peers joining and leaving).

How Peers Find Each Other: DHT

Distributed Hash Tables (DHT) are the standard mechanism for peer discovery in large P2P networks (BitTorrent, IPFS). A DHT maps keys to values across all peers without a central directory:

Key: SHA1("ubuntu-22.04.iso")
     → Hash determines which peer(s) are responsible for this key
     → Those peers store the list of peers that have the file

Each peer knows a subset of other peers (its "routing table"). Lookups route through O(log N) hops to find the peer responsible for a key. BitTorrent's DHT (Kademlia) allows torrent discovery without a central tracker.

Real-World Applications

Application P2P use Why P2P
BitTorrent File distribution Scales with demand — more seeders = faster downloads
IPFS Content-addressed storage Censorship resistance, no central hosting cost
WebRTC Browser video/audio Direct peer connections reduce server relay cost
Blockchain Transaction ledger No central authority, tamper-evident
Skype (original) VoIP routing Reduced server infrastructure cost

Tradeoffs

Dimension P2P Client-Server
Scalability Scales with peers (more peers = more capacity) Scales with server investment
Single point of failure None (distributed) Central server is SPOF
Consistency Hard (eventual consistency, no central authority) Easy (server is source of truth)
Coordination Complex (peer discovery, churn handling) Simple (clients talk to server)
Latency Variable (depends on peer proximity) Predictable (server location known)

Decision rule: use P2P when you need to distribute large files at scale (BitTorrent), build censorship-resistant systems (IPFS, blockchain), or reduce server costs for direct communication (WebRTC). Use client-server when you need strong consistency, predictable latency, or centralized access control.

Questions

Pitfalls

NAT traversal failure
Most peers are behind NAT and lack public IP addresses. STUN discovers the public IP/port; when symmetric NAT blocks direct connection, TURN relay is required. TURN adds latency and server cost. Mitigation: implement ICE (Interactive Connectivity Establishment) — try direct connection first, fall back to TURN relay only when necessary.

DHT poisoning
A malicious peer can inject false routing table entries, redirecting lookups to attacker-controlled nodes. Mitigations: Sybil resistance (proof-of-work for node IDs), content addressing (IPFS uses the hash as the content ID — poisoned data is detectable because the hash won't match).

Churn instability
Peers join and leave constantly. High churn degrades DHT routing — routing tables become stale, lookups fail. Kademlia's bucket refresh keeps routing tables current; replication factor (k=20 in BitTorrent) ensures data survives peer loss.

WebRTC Connection Setup

// ICE candidate exchange (simplified)
1. Peer A creates RTCPeerConnection
2. Peer A gathers ICE candidates (STUN → public IP/port)
3. Peer A sends offer + candidates to Peer B via signaling server
4. Peer B responds with answer + its own candidates
5. Both peers try all candidate pairs → select lowest-latency direct path
6. If no direct path: fall back to TURN relay

References


Whats next

Parent
04 Networks

Pages