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
Publish a file
Join a swarm
Download a piece
Connect through NATs
Tracker down · use the DHT
Reject a bad piece
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
The numbers, at a glance
6 behind NAT4 reachable
44 minWhole file
36 min+ Pieces
20 min+ Rarest
11 min+ Tit-for-tat
7.3 min+ Punching
107 Mbit/s announces9.3 Tbit/s peer to peer
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.
GET file
Our CDN sends 2 GB
100 PB a day on our bill
+ Simple and controlled.− We pay for 9.3 Tbit/s.
Seeder
2 GB to one peer at a time
80% of peers idle
+ One hash per file.− Only complete peers can upload.
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.
Peer client
TrackerManifest store
Other peers
Peer client
Announce routerManifest CDN
Tracker shardsManifest store
Other peers
Peer client
Announce routerBootstrap nodes
Tracker shardsDHT
Other peers + PEX
Peer client
Announce routerRendezvous
Tracker shardsSwarm statsDHT
Other peers + PEX
Was this walkthrough useful?Your vote tells us what to improve next.