Understanding the Torrent File Format and Bencoding



969 views BitTorrent Internals



A torrent file is an independent session of transfer. To distribute a file, say an Ubuntu image, we would create a torrent holding the meta information about it while the actual content is split into pieces and distributed in the network.

Lifecycle of a Torrent

Torrent is alive so long as there is at least a seeder, seeding the pieces in the network. Once all the nodes who can share the pieces have left the network, the download of the network stops, and the torrent ends until a new seeder pops up.

What torrent file holds

The .torrent file holds all critical meta yet static information about the content, like

  • announce - holds the URL of the tracker
  • created by - name and version of the program who created it
  • creation date - time at which the torrent was created
  • encoding - encoding of the strings in info dictionary
  • comment - some additional comments
  • info - a dictionary that describes files in the torrent

The info dictionary holds the information about the file that is being shared through the torrent. If the torrent is about the Ubuntu image, it will hold info like name, length, and md5sum of the actual file.

Given that the file is split into equal-length pieces, the info dictionary also holds

  • pieces - SHA1 of every piece of the file concatenated
  • piece length - number of bytes in each piece

This information is used in identifying and fetching pieces from the network.

Bencoding

Torrent files are encoded with a custom encoding called Bencoding. It supports data types such as Strings, Integers, List, and Dictionaries (which can only hold string keys).

Strings are encoded as <length>:<string>. Hence, a string arpit will be encoded as 5:arpit.

Integers are encoded as i<integer>e. Hence, an integer 10 will be encoded as i10e.

Lists are encoded as l<becoded values>e. Hence, the list ["a","b", 1 ] will be encoded as l1:a1:bi1e1.

Dictionaries are encoded as d<key1><value1><key2><value2>e. Hence, a dictionary {"a": 1, "b": 2} will be encoded as d1:ai1e1:bi2ee.


Arpit Bhayani

Arpit's Newsletter

CS newsletter for the curious engineers

❤️ by 17000+ readers

If you like what you read subscribe you can always subscribe to my newsletter and get the post delivered straight to your inbox. I write essays on various engineering topics and share it through my weekly newsletter.




Other essays that you might like


Exploiting and stealing from the BitTorrent network

406 views 17 likes 2022-08-19

Stealing is bad, but in a P2P network, it is a cakewalk. In this 7th video of the BitTorrent Internals series, we take ...

Kademlia - a Distributed Hash Table implementation to power the overlay network of BitTorrent

667 views 36 likes 2022-08-17

Kademlia is a Distributed Hash Table implementation and it is used as an overlay network for BitTorrent. Instead of talk...

The Piece Selection algorithm that makes BitTorrent fault tolerant

421 views 22 likes 2022-08-15

Performance of the BitTorrent network relies heavily on the order in which the pieces are requested by the peers. In th...

The Choke Algorithm that powers BitTorrent

517 views 23 likes 2022-08-12

One of the most important algorithm that powers BitTorrent is The Choke Algorithm In this 4th video of the BitTorrent I...


Be a better engineer

A set of courses designed to make you a better engineer and excel at your career; no-fluff, pure engineering.


System Design Masterclass

A masterclass that helps you become great at designing scalable, fault-tolerant, and highly available systems.

800+ learners

Details →

Designing Microservices

A free playlist to help you understand Microservices and their high-level patterns in depth.

17+ learners

Details →

GitHub Outage Dissections

A free playlist to help you learn core engineering from outages that happened at GitHub.

67+ learners

Details →

Hash Table Internals

A free playlist to help you understand the internal workings and construction of Hash Tables.

25+ learners

Details →

BitTorrent Internals

A free playlist to help you understand the algorithms and strategies that power P2P networks and BitTorrent.

42+ learners

Details →

Topics I talk about

Being a passionate engineer, I love to talk about a wide range of topics, but these are my personal favourites.




Arpit's Newsletter read by 17000+ engineers

🔥 Thrice a week, in your inbox, an essay about system design, distributed systems, microservices, programming languages internals, or a deep dive on some super-clever algorithm, or just a few tips on building highly scalable distributed systems.



  • v12.7.8
  • © Arpit Bhayani, 2022

Powered by this tech stack.