BitTorrent Architecture
Most of us have used software like qBittorrent, uTorrent, etc., to download files. But have you ever wondered about the architecture behind it? Worry not, in this post, we'll dive into the architecture of BitTorrent.
The BitTorrent architecture mainly consists of four entities:
Piece
.torrent file
Tracker
Seeder
Leecher
Architecture
Piece
Files are split into pieces of equal length except for the last one. The size of a piece can range from 256 kb to 1mb. The piece is passed through a SHA1 hash function which is a 20-byte hash function. The SHA1 hash of each piece is added in the .torrent file under pieces information. The piece is then fetched by it’s SHA1 from torrent file.
.torrent file
.torrent file is a static file. The .torrent contains important information like announce url, filename, size, pieces, piece length, etc. The torrent file is download from a torrent server via HTTP. Every torrent is uniquely identified through its info hash which is generated through bencoding. The details are covered in the previous post.
Tracker
The announce url in the torrent file points to the tracker which is the only central entity in this network. The trackers tracks the information like:
Tracks of peers (nodes) who hold the pieces.
Number of peers that are currently downloading the file.
Collects the state of the peers. The state here refers to the amount of content in bytes has the certain peer downloaded/uploaded since it join the network.
Suppose few of the peers drop from the network then trackers helps in finding the new peers who hold the pieces.
Leecher
The peer who is downloading the file is known as leecher. The leecher can download from a seeder or from another leecher.
Seeder
Seeder is the peer that has the entire file.
Diagram
Flow
The above diagram outlines the process of torrent file sharing and downloading in a peer-to-peer (P2P) network. Here's the flow explained step by step:
1. Upload the Torrent File:
The first step in this process is when the first seeder (the original source of the file) uploads a torrent file to the torrent server.
The torrent file contains metadata about the files to be shared, such as file size, pieces, and location of the tracker.
2. Search for Torrent File:
A search engine (like Pirate Bay) indexes torrent files and allows users to search for specific torrents.
A Peer (someone looking to download a file) will search for the torrent file they want to download.
3. Download the Torrent File:
After finding the desired torrent file on the search engine, the peer downloads this small torrent file to their machine.
The torrent file itself does not contain the actual content but rather the instructions for obtaining it.
4. Get Peer List from Tracker:
Once the torrent file is downloaded, the peer contacts the tracker, which keeps track of all the peers participating in the torrent swarm (those downloading or uploading the same file).
The tracker provides the peer with a list of other Peers (including the original seeder) from whom pieces of the file can be downloaded.
5. Get Pieces from Seeder/Other Peers:
The peer then connects to the listed peers (including the seeder) and begins downloading pieces of the file.
As more peers join the swarm and begin downloading, they also upload pieces to other peers, speeding up the download process for everyone in the swarm.
This peer-to-peer mechanism allows for efficient distribution of large files, as pieces are downloaded from multiple sources simultaneously.
References
Arpit Bhayani’s BitTorrent Internal Series
https://www.bittorrent.org/beps/bep_0003.html
https://web.cs.ucla.edu/classes/cs217/05BitTorrent.pdf