Skip to content
SD-52 · Back to the write-up

Design a Peer-to-Peer File Transfer

Hash every piece, let sharded trackers introduce peers, and let peers trade rarest pieces first, with a DHT and hole punching so swarms run without us.

Follow a request

Peers
Coordination
Manifests
Your clientDownloads + uploads
SeederHas every piece
Other peersSwapping pieces
DHTPeers store peers
Trackers8 shards by info hash
RendezvousHole punching
Manifest CDNCached forever
Manifest storeBy info hash
1you → swarm · interestedThe client picks the rarest piece among its neighbours and tells peers that have it it is interested.1 / 5

The numbers, at a glance

Out of every 10 peers……6 are behind a NAT that blocks incoming calls
6 behind NAT4 reachable
Time to download 2 GBAs each part is added, in the lab’s model
Bytes through our serversAnnounces vs file data moved by peers
Peers carry the file; we only introduce them.

Joining and getting a piece, in order

With a tracker. If it is down, the DHT replaces steps 3 and 4.

Client
Manifest CDN
Tracker
Peer A
Peer B
1 GET manifest
2 64 KB, hash checks
3 announce started
4 50 peers
5 handshake + bitfield
6 handshake + bitfield
7 unchoke
8 request blocks 0–31
9 request blocks 32–63
10 blocks
11 blocks
12 have piece
13 have piece
RequestResponseAsync, does not block

How bytes reach downloaders

The decision this question is really about.

Central servers
Too costly
GET file
Our CDN sends 2 GB
100 PB a day on our bill
+ Simple and controlled.− We pay for 9.3 Tbit/s.
Whole files from peers
Wastes the swarm
Seeder
2 GB to one peer at a time
80% of peers idle
+ One hash per file.− Only complete peers can upload.
Hashed pieces from many
Our pick
Manifest: 2,048 hashes
Peer A: piece 7Peer B: piece 912
Check SHA-256, then share it
+ Everyone uploads; bad data costs 1 MB.− A manifest of hashes per file.

How it grows

Add a box only when a number forces you to. Highlighted boxes are new at each stage.

Stage 1 · ≈ 100 announces/sMake it work
Peer client
TrackerManifest store
Other peers
Hashed pieces and one in-memory tracker.
Stage 2 · ≈ 16,700 announces/sShard the tracker
Peer client
Announce routerManifest CDN
Tracker shardsManifest store
Other peers
Route by info hash to 8 shards with standbys; manifests at the edge.
Stage 3 · ≈ 10M peersSurvive without us
Peer client
Announce routerBootstrap nodes
Tracker shardsDHT
Other peers + PEX
DHT and peer exchange; the announce interval doubles to 60 minutes.
Stage 4 · 10M peers, 6M behind NATConnect everyone
Peer client
Announce routerRendezvous
Tracker shardsSwarm statsDHT
Other peers + PEX
Hole punching lifts reachable pairs from 60% to 90%; stats keep scrapes off trackers.
Was this walkthrough useful?Your vote tells us what to improve next.