← Back to portfolio

PROJECT NOTES · RUST

How BitTorrent works

A look at the ideas behind peer-to-peer file sharing and my RustBitTorrent client.

View RustBitTorrent on GitHub

Sharing in pieces

BitTorrent moves a file as many small pieces instead of asking one server to send the whole thing. A torrent’s metadata describes the content and its pieces. As a client downloads pieces, it can also share verified pieces with other clients.

That group of participating clients is called a swarm. Each client can download from several peers and contribute pieces to others, so the work is distributed across the swarm.

From metadata to peers

  1. Read the torrent metadata. The client learns what is being shared and how the data is divided into pieces.
  2. Discover peers. A tracker can introduce the client to other participants in the swarm.
  3. Exchange pieces. The client requests pieces from connected peers while advertising the pieces it already has.
  4. Verify the data. Piece hashes let the client check received data against the metadata before keeping or sharing it.
  5. Assemble the download. Once all required pieces are available and verified, they can be written back in order.

Building RustBitTorrent

RustBitTorrent is my BitTorrent client implementation in Rust. Building a client brings together several useful systems concepts: parsing structured metadata, making network requests, coordinating asynchronous work, and checking data integrity.

The benchmark output below shows a run with peers connected, verified data accumulating, and transfer speed being reported. It makes the client’s progress visible while a download is underway.

RustBitTorrent benchmark output with active peers, verified bytes, and download speed
Benchmark output from a RustBitTorrent run.

Explore the code

For the implementation details and current state of the project, visit the repository.

Open the GitHub repository