§2.5Peer-to-Peer File Distribution

Application layer Kurose & Ross pp. 136–142 · ~18 min read

  • distribution time
  • torrent
  • chunk
  • tracker
  • rarest first
  • unchoked
  • optimistically unchoked
  • tit-for-tat
  • freerider
  • distributed hash table

Where you are

  • Application layer you are here
  • Transport layer
  • Network layer
  • Link layer
  • Physical layer

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.

Figure 2.22 — the file distribution problem, and every symbol in the equationsInternetassumed to have capacity to spareServerholds the file, F bitsuspeer 1peer 2peer 3peer 4peer 5peer 6u₁d₁Every peer has an upload rate uᵢ and a download rate dᵢ. d_min is the smallest of the dᵢ.All the bottlenecks are in these access links — the core is assumed to have capacity to spare.

The assumptions, stated once

The model that follows makes two simplifying assumptions, and the book flags both as generally accurate:

  1. The Internet core has abundant capacity, so all the bottlenecks are in access networks. Every rate in the equations is an access-link rate.
  2. 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 D_cs.

  • The server must transmit one copy of the file to each of the N peers, so it must transmit NF bits. At upload rate u_s, that takes at least NF/u_s.
  • Let d_min be the download rate of the slowest peer. That peer cannot obtain all F bits in less than F/d_min seconds.

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.

  1. 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.
  2. As before, the slowest peer needs at least F/d_min.
  3. The total upload capacity of the system is u_total = u_s + u_1 + … + u_N. The system must deliver F bits to each of N peers, so NF bits in total, and it cannot do that faster than u_total. So the time is at least NF/(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.

Distribution time, both architectures

What each symbol means

  • Fsize of the file being distributed (bits)
  • Nnumber of peers that want a copy ()
  • u_supload rate of the server’s access link (bits/s)
  • uupload rate of each peer’s access link (bits/s)
  • d_mindownload 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 added

Peer-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 upload

Change 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.

Figure 2.23 — minimum distribution time against the number of peers
0.0000.8751.82.63.51.07.814.621.428.235.0N — number of peersminimum distribution time (hours)P2P never passes 1 hour, however many peers join

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) .

A torrent is the collection of all peers participating in the distribution of one particular file. Peers download equal-size chunks 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.

Figure 2.24 — file distribution with BitTorrent
1 hops: Alice → Tracker
One torrent — every peer sharing this one fileregister, get 50 peerschunkschunkschunkschunkschunkschunkschunksTrackerAlicePeer BPeer CPeer DPeer EPeer F

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 . 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) addresses. Alice tries to open TCP (Transmission Control Protocol) 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 — which chunk should Alice ask for?
chunk 1chunk 2chunk 3chunk 4chunk 5chunk 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.

Tit-for-tat — who does Alice send chunks to?
She measures what each neighbour gives herstep 1 of 4
AliceAlice has more neighbours than she can serve. Which ones get her chunks?Peer BPeer CPeer DPeer EPeer FPeer G420 kbps to Alice310 kbps to Alice180 kbps to Alice95 kbps to Alice40 kbps to Alice5 kbps to Alice

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
  1. She measures what each neighbour gives herFor 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.
  2. Every 10 seconds: the top four are unchokedAlice 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.
  3. Every 30 seconds: one more, chosen at randomAlice 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.
  4. Why the random one mattersBecause 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 , recalculated every 10 seconds. One more, chosen at random every 30 seconds, is optimistically unchoked . Every other neighbour is choked and receives nothing.

The incentive, and its name

This mechanism is called tit-for-tat . 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:

  1. It has been shown that the incentive scheme can be circumvented.
  2. 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 .

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 ( DHT (Distributed Hash Table) ) 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 answered
  1. 1.predictIn the plot, drag the number of peers up as far as it goes. What happens to each curve?

  2. 2.Why does the client-server distribution time contain the term NF/u_s?

  3. 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. 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. 5.Why does rarest first help the torrent as a whole, not just the peer using it?

  6. 6.Alice sends chunks to four neighbours, and every 30 seconds to one more chosen at random. What is that fifth one for?

  7. 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 has N on 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.