When every machine that wants a file also hands out pieces of it, adding users adds capacity as fast as it adds demand. The time to serve everybody then stops growing.
Words you will meet
- Distribution time — how long until every peer has a complete copy.
- Torrent — the whole group of machines sharing one particular file.
- Chunk — one equal-sized piece of that file, typically 256 kB.
- Tracker — the one server that keeps the list of who is taking part.
- Rarest first — ask for the piece the fewest of your neighbours have.
- Unchoked — one of the four neighbours a peer is currently sending to.
- Tit-for-tat — send to whoever is currently sending you the most.
- Freerider — a peer that downloads from others and uploads nothing back.
Why this matters
Section 2.1.1 claimed that peer-to-peer architectures are self-scaling. This is where that claim stops being a claim and becomes an equation with a graph.
It is also the only application in this chapter that is not client-server, and the only one where the hard problem is incentives rather than protocol design. BitTorrent works because it makes selfishness produce cooperation, and the book is clear that without that mechanism it would not exist.
The problem
Distribute a large file from a single server to N hosts, called peers. The
file might be a new release of an operating system, a software patch, or a video.
In client-server distribution, the server must send a copy to each peer. That places an enormous burden on the server and consumes a great deal of its upload capacity.
In P2P distribution, each peer can redistribute any portion of the file it has already received to any other peer, so it helps the server do its job.
The assumptions, stated once
The model that follows makes two simplifying assumptions, and the book flags both as generally accurate:
- The Internet core has abundant capacity, so all the bottlenecks are in access networks. Every rate in the equations is an access-link rate.
- The server and the peers are doing nothing else, so all of their upload and download capacity goes to this one file.
Neither is exactly true. Both are close enough that the conclusion survives, and saying which corners were cut is part of stating a result honestly.
Client-server: the time grows without limit
Two observations bound the
distribution time distribution time Simple How long it takes until every peer has a complete copy of the file. Precise The time it takes to get a copy of a file to all N peers. For the client-server architecture it is D_cs = max{NF/u_s, F/d_min}, which grows without bound as N grows. For P2P it is D_P2P = max{F/u_s, F/d_min, NF/(u_s + Σu_i)}, which stays bounded because each peer adds upload capacity.
introduced in ch. 2 — open in glossary
D_cs.
- The server must transmit one copy of the file to each of the N peers, so it
must transmit
NFbits. At upload rateu_s, that takes at leastNF/u_s. - Let
d_minbe the download rate of the slowest peer. That peer cannot obtain allFbits in less thanF/d_minseconds.
Putting them together gives the lower bound, and the homework shows the server can schedule its transmissions so the bound is actually achieved. So take it as the distribution time:
D_{cs} = \max\left\{ \frac{NF}{u_s},\ \frac{F}{d_{min}} \right\} \tag{2.1}
In plain words
For N large enough, the first term wins and D_cs is NF/u_s. That grows
linearly with the number of peers.
So if the number of peers rises a thousand-fold from one thousand to one million from one week to the next, the time to distribute the file rises by 1,000.
Peer-to-peer: the time stops growing
Now let each peer help. Three observations bound D_P2P.
- At the start only the server has the file. To get it into the community it
must send each bit at least once into its access link, so the time is at least
F/u_s. Note the difference from the client-server case: a bit the server sends once may never need to be sent by the server again, because the peers redistribute it among themselves. - As before, the slowest peer needs at least
F/d_min. - The total upload capacity of the system is
u_total = u_s + u_1 + … + u_N. The system must deliverFbits to each ofNpeers, soNFbits in total, and it cannot do that faster thanu_total. So the time is at leastNF/(u_s + Σu_i).
D_{P2P} = \max\left\{ \frac{F}{u_s},\ \frac{F}{d_{min}},\ \frac{NF}{u_s + \sum_{i=1}^{N} u_i} \right\} \tag{2.3}
If each peer could redistribute a bit the instant it received it, there is a scheme that actually achieves this bound. In reality whole chunks are redistributed rather than individual bits, so equation 2.3 is a good approximation of the true minimum.
What each symbol means
- F — size of the file being distributed (bits)
- N — number of peers that want a copy (—)
- u_s — upload rate of the server’s access link (bits/s)
- u — upload rate of each peer’s access link (bits/s)
- d_min — download rate of the slowest peer (bits/s)
Read aloud: Take every reason the distribution cannot finish sooner, work out how long each one forces it to take, and the answer is the largest of them.
Client-server — equation 2.132000 s
the server must send one copy to each peer: N x F = 32,000,000,000,000 bits
so it needs at least NF/u_s = 32000 s
the slowest peer cannot receive F bits faster than F/d_min = 640 s
D_cs = max{ 32000 s , 640 s } = 32000 s
the server's upload is the binding constraint — and it grows with every peer addedPeer-to-peer — equation 2.35333 s
the server must push each bit into the network at least once: F/u_s = 32.0 s
the slowest peer still needs F/d_min = 640 s
the system as a whole uploads at u_s + Σu_i = 6.00 Gbps
and must deliver NF = 32,000,000,000,000 bits, so at least 5333 s
D_P2P = max{ 32.0 s , 640 s , 5333 s } = 5333 s
binding constraint: aggregate uploadChange any number above and the arithmetic re-runs, carrying the units through.
The defaults distribute a 4 GB release to a thousand people. Raise N and watch which line of working becomes the binding constraint in each case.
Worked example — a 4 GB release to a thousand people
F = 32 Gbit, u_s = 1 Gbps, every peer uploads at 5 Mbps and the slowest
downloads at 50 Mbps.
Client-server: the server must push 1,000 × 32 Gbit = 32 Tbit through a 1 Gbps link. That is 32,000 s, or 8.9 hours. The slowest peer’s 640 s is nowhere near binding.
Peer-to-peer: the system uploads at 1 Gbps + 1,000 × 5 Mbps = 6 Gbps, and still has 32 Tbit to deliver. That is 5,333 s, or 1.5 hours — six times faster, with no extra server bought.
Now raise the peer count in the calculator. At 10,000 peers the client-server time is 89 hours and the P2P time is 10.5. The gap widens with every peer, because one denominator grows and the other does not.
At 35.0 (move the pointer over the plot to read it anywhere):
- Client-server:3.5 hours
- Peer-to-peer:0.778 hours
The book’s setup: every peer uploads at rate u, F/u = 1 hour, and the server uploads at k times u. Drag the number of peers to the right-hand end and watch what the two curves do differently.
The ceiling is the whole point
Use the book’s own settings: F/u = 1 hour, u_s = 10u, and d_min not
binding. The P2P curve then rises towards one hour and never passes it, for
any number of peers. Drag N to the end of the axis and it is still under an
hour.
That is self-scalability, made precise:
This scalability is a direct consequence of peers being redistributors as well as consumers of bits.
The client-server curve, meanwhile, rises linearly and without bound.
BitTorrent
BitTorrent is the most popular P2P file distribution protocol. It was originally developed by Bram Cohen, and there are now many independent clients conforming to it — just as there are many browsers conforming to HTTP (HyperText Transfer Protocol) HyperText Transfer Protocol The application-layer protocol that requests and transfers Web documents. introduced in ch. 1 .
A torrent torrent Simple The whole group of machines sharing one particular file. Precise In BitTorrent, the collection of all peers participating in the distribution of a particular file. A torrent may have fewer than ten or more than a thousand peers at any instant of time. introduced in ch. 2 — open in glossary is the collection of all peers participating in the distribution of one particular file. Peers download equal-size chunks chunk Simple One equal-sized piece of a file that peers trade with each other. Precise An equal-size piece of a file that peers in a torrent download from one another, with a typical chunk size of 256 kilobytes. A peer joining a torrent has no chunks and accumulates them over time, uploading chunks while it downloads. introduced in ch. 2 — open in glossary of the file from one another, typically 256 kB each. A peer joining a torrent has no chunks, and accumulates them over time while uploading chunks to others.
Once a peer has the whole file it may selfishly leave, or altruistically stay and keep uploading. Any peer may leave at any time with only a subset of chunks, and rejoin later.
Drag any device to rearrange the picture. Hover a link to see its rate, delay and length.
The tracker is the only infrastructure. Play the journeys: it hands Alice a list of peers and then takes no further part.
Read this diagram as text
- Alice — wired link — Tracker
- Peer B — wired link — Tracker
- Peer C — wired link — Tracker
- Peer D — wired link — Tracker
- Peer E — wired link — Tracker
- Peer F — wired link — Tracker
- Alice — wired link — Peer B
- Alice — wired link — Peer C
- Alice — wired link — Peer D
- Peer B — wired link — Peer C
- Peer C — wired link — Peer E
- Peer D — wired link — Peer E
- Peer E — wired link — Peer F
The tracker, and finding neighbours
Each torrent has one infrastructure node, the tracker tracker Simple The one server in a torrent that keeps the list of who is taking part. Precise The infrastructure node of a torrent. A peer registers with the tracker on joining and periodically confirms it is still there. When a new peer joins, the tracker randomly selects a subset of participating peers — for concreteness, 50 — and sends their IP addresses to the newcomer. The newcomer then tries to open TCP connections to all of them. introduced in ch. 2 — open in glossary . A peer registers with it on joining and periodically confirms it is still there, so the tracker knows who is participating. A torrent may have fewer than ten peers or more than a thousand at any instant.
When Alice joins, the tracker randomly selects a subset of peers — say 50 — and sends her their IP (Internet Protocol) Internet Protocol The network-layer protocol that defines the datagram format and addressing every Internet device must use. introduced in ch. 1 addresses. Alice tries to open TCP (Transmission Control Protocol) Transmission Control Protocol The Internet transport protocol that delivers data reliably and in order, with flow control and congestion control. introduced in ch. 1 connections to all of them. Those she succeeds with are her neighbouring peers. Some will leave and others will arrive, so the set fluctuates.
Which chunk to ask for
Periodically Alice asks each neighbour for the list of chunks it has. With L
neighbours she gets L lists, and can then request chunks she does not have.
Her policy is rarest first rarest first Simple Ask first for the piece that the fewest of your neighbours have. Precise The BitTorrent technique for deciding which chunks to request. From among the chunks it does not have, a peer determines those that are rarest among its neighbours — that have the fewest repeated copies — and requests those first. The rarest chunks are then redistributed more quickly, roughly equalising the number of copies of each chunk in the torrent. introduced in ch. 2 — open in glossary .
| chunk 1 | chunk 2 | chunk 3 | chunk 4 | chunk 5 | chunk 6 | |
|---|---|---|---|---|---|---|
| Alice is missing chunks 1, 3, 4 and 6. Those four are the only candidates — the rule applies to chunks she does not have. | ||||||
| Chunk 5 is scarcer than chunk 3. Alice still requests chunk 3, because rarest first ranks only the chunks she is missing. | ||||||
Cells marked ⓘ have an explanation — click to read it. Sortable columns have a ↕ in the heading.
Six chunks, Alice and four neighbours. Click a ⓘ. The answer is not simply the scarcest chunk on the row.
In plain words
The idea is to determine, from among the chunks she does not have, which are rarest among her neighbours, and request those first.
The effect is on the torrent rather than on Alice. Rare chunks get redistributed quickly, which roughly equalises the number of copies of each chunk. A chunk held by one peer is one departure away from vanishing; a chunk held by many is safe.
Who to send to
Alice must also decide which requests to answer, and here BitTorrent uses a clever trading algorithm.
For each of her neighbours, Alice continually measures the rate at which she receives bits. Nothing is negotiated and nobody is asked to promise anything — she simply watches what actually arrives.
Two clocks run at once: one every 10 seconds, one every 30. Step through to see what each of them does.
Read all steps as text
- She measures what each neighbour gives her — For each of her neighbours, Alice continually measures the rate at which she receives bits. Nothing is negotiated and nobody is asked to promise anything — she simply watches what actually arrives.
- Every 10 seconds: the top four are unchoked — Alice determines the four peers feeding her bits at the highest rate, and reciprocates by sending chunks to those same four. In BitTorrent language they are unchoked. Every 10 seconds she recalculates the rates and may change the set.
- Every 30 seconds: one more, chosen at random — Alice also picks one additional neighbour at random and sends it chunks regardless of rate. That peer is optimistically unchoked. Suppose it is G, who was giving her almost nothing.
- Why the random one matters — Because Alice is now sending to G, she may become one of G’s top four uploaders — in which case G starts sending to her. If G’s rate is high enough, G could in turn become one of Alice’s top four. Every 30 seconds Alice tries a new trading partner this way.
The four highest-rate neighbours are unchoked unchoked Simple Being one of the four neighbours a peer is currently sending chunks to. Precise In BitTorrent, the state of the four neighbours that are feeding a peer bits at the highest rate, to which that peer reciprocates by sending chunks. The rates are recalculated every 10 seconds, and the set may change. All neighbours other than these four and the optimistically unchoked one are choked, and receive nothing. introduced in ch. 2 — open in glossary , recalculated every 10 seconds. One more, chosen at random every 30 seconds, is optimistically unchoked optimistically unchoked Simple The one extra neighbour picked at random every 30 seconds, to give newcomers a chance. Precise In BitTorrent, the additional neighbour a peer picks at random every 30 seconds and sends chunks to, regardless of measured rate. This lets peers discover better trading partners, and lets new peers that have nothing to trade obtain their first chunks. introduced in ch. 2 — open in glossary . Every other neighbour is choked and receives nothing.
The incentive, and its name
This mechanism is called tit-for-tat tit-for-tat Simple Send chunks to whoever is currently sending you the most. Precise The BitTorrent incentive mechanism of giving priority to the neighbours currently supplying data at the highest rate. The effect is that peers capable of uploading at compatible rates tend to find each other. The scheme can be circumvented. Even so, without it or a variant, most users would have been freeriders and BitTorrent would likely not exist today. introduced in ch. 2 — open in glossary . Its effect is that peers capable of uploading at compatible rates tend to find each other. The random selection also lets new peers get chunks, so that they have something to trade.
The book is careful on two points, and both are worth keeping:
- It has been shown that the incentive scheme can be circumvented.
- Nevertheless the ecosystem is wildly successful, with millions of simultaneous peers in hundreds of thousands of torrents. If BitTorrent had been designed without tit-for-tat, but otherwise identically, it would likely not exist now — the majority of users would have been freeriders freerider Simple A peer that downloads from others but uploads nothing back. Precise A participant that consumes the capacity of a P2P system without contributing any. The BitTorrent tit-for-tat mechanism exists to make freeriding unattractive. introduced in ch. 2 — open in glossary .
The second point is the one to remember. Self-scalability needs peers to contribute upload capacity. Make contributing optional and unrewarded, and the system collapses into a very slow client-server one.
BitTorrent has other mechanisms this section does not cover: pieces (mini-chunks), pipelining, random first selection, endgame mode and anti-snubbing.
Distributed hash tables
One more P2P application deserves a mention. A distributed hash table distributed hash table Simple A simple database whose records are spread over the peers of a P2P system. Precise A DHT: a simple database with the database records distributed over the peers in a P2P system. DHTs have been widely implemented, for example in BitTorrent, and have been the subject of extensive research. introduced in ch. 2 — open in glossary ( DHT (Distributed Hash Table) Distributed Hash Table A simple database whose records are spread over the peers of a P2P system. introduced in ch. 2 ) is a simple database whose records are distributed over the peers of a P2P system. DHTs have been widely implemented — in BitTorrent among others — and have been the subject of extensive research.
Check yourself
Check yourself
0 of 7 answered1.predictIn the plot, drag the number of peers up as far as it goes. What happens to each curve?
2.Why does the client-server distribution time contain the term NF/u_s?
3.The P2P expression has three terms inside the maximum. What does the term F/u_s say?
Think about the very first moment of the distribution.
4.Alice is choosing which chunk to request. Chunk 5 is the rarest among her neighbours, held by only one of them. Should she ask for it?
Look at what Alice already has.
5.Why does rarest first help the torrent as a whole, not just the peer using it?
6.Alice sends chunks to four neighbours, and every 30 seconds to one more chosen at random. What is that fifth one for?
7.The book says that without tit-for-tat, BitTorrent would likely not exist today. Why?
What to remember
- Client-server:
D_cs = max{NF/u_s, F/d_min}, whose first term grows linearly with N and without bound. Peer-to-peer:D_P2P = max{F/u_s, F/d_min, NF/(u_s + Σu_i)}, whose last term hasNon both sides of the fraction, so it approaches a ceiling. That is self-scalability. - A torrent trades 256 kB chunks. A tracker hands a joining peer about 50 others and then plays no further part. Rarest first ranks only the chunks a peer is missing, which equalises how many copies of each exist.
- Tit-for-tat: the top 4 uploaders are unchoked, recalculated every 10 s, plus 1 random peer every 30 s. It can be circumvented — but without it most users would have been freeriders and BitTorrent would likely not exist.