Torrenting is one of the most widely used file-sharing technologies in the world, yet most people who use it every day have only a vague understanding of what is actually happening under the hood. They click a magnet link, a client opens, progress bars fill up, and a file appears on their hard drive. The mechanics behind that process — the protocol design, the peer discovery system, the data integrity verification, the distributed architecture — are genuinely fascinating and represent one of the most elegant solutions to large-scale file distribution ever built.
This post covers how BitTorrent actually works from the ground up — the protocol architecture, the components involved, the data flow, and the technical decisions that make it one of the most efficient file distribution systems ever designed.
What Problem Does BitTorrent Solve?
Before getting into how it works, it helps to understand what problem BitTorrent was built to solve.
The Traditional Download Model and Its Limits
In a traditional client-server download — downloading a file from a website — the entire file comes from a single server. That server pays for every byte it sends. As more users download simultaneously, the server bears more load and more bandwidth cost. Eventually the server becomes a bottleneck — too many concurrent downloads slow everyone down, and the cost of serving popular files at scale becomes prohibitive.
This is why popular files posted to a single web server often result in what used to be called a slashdot effect — a sudden flood of traffic causes the server to slow or go offline entirely. The more popular a file, the harder it is to distribute from a single source.
BitTorrent’s Fundamental Insight
BitTorrent, created by Bram Cohen and first released in 2001, solved this problem with a single elegant insight — instead of having one server send the complete file to every downloader, make every downloader simultaneously an uploader. As you receive pieces of the file, you immediately begin sharing those pieces with other users who need them.
The result is a system where popularity is a strength rather than a liability. The more people download a file, the more bandwidth is available to distribute it. A file with 10,000 simultaneous downloaders has vastly more distribution capacity than a file with 10 downloaders — the opposite of the traditional server model.
The Core Components of the BitTorrent System
Understanding how torrenting works requires understanding each component that makes the system function.
1. The Torrent File and Metainfo
The .torrent file is the entry point into the BitTorrent system. It is a small metadata file — typically between 10KB and a few hundred KB — encoded in a format called Bencode (short for BitTorrent encoding).
What a Torrent File Contains
A torrent file contains the following structured data:
- announce — the URL of the tracker server that coordinates peer discovery for this torrent
- announce-list — a list of multiple tracker URLs as fallback options
- info dictionary — the core metadata block containing:
- name — the suggested name for the downloaded content
- piece length — the size of each data piece, typically 256KB to 2MB
- pieces — a concatenated list of SHA-1 hash values, one for each piece of the file
- files — for multi-file torrents, a list of all files with their sizes and directory paths
- length — for single-file torrents, the total file size
The Info Hash — The Torrent’s Unique Identity
The info dictionary is hashed using SHA-1 to produce a 20-byte value called the info hash. This hash is the torrent’s unique identifier across the entire BitTorrent network. It is what magnet links encode, what trackers use to group peers, and what distributed hash tables use to find peers without a central tracker.
Every torrent has exactly one info hash. Two torrents with identical content but different metadata — different tracker URLs, for example — will have different info hashes.
Bencode Format
Bencode is a simple serialisation format with four data types:
- Integers encoded as i42e (the number 42)
- Byte strings encoded as 4:spam (the string “spam”)
- Lists encoded as l…e (a list of elements)
- Dictionaries encoded as d…e (key-value pairs where keys are sorted strings)
The entire torrent file is a Bencoded dictionary. It is human-readable with the right tools and contains no binary executable content — which is why .torrent files themselves carry no malware risk under normal circumstances.
2. Magnet Links
Magnet links are an alternative to .torrent files that eliminate the need for a separate metadata file entirely. A typical magnet link looks like this:
magnet:?xt=urn:btih:INFOHASH&dn=DisplayName&tr=TrackerURL
What a Magnet Link Contains
- xt (exact topic) — the info hash of the torrent, encoded in hexadecimal or Base32
- dn (display name) — a human-readable name for the content
- tr (tracker) — one or more tracker URLs
- xl (exact length) — optional file size
When a torrent client receives a magnet link, it has the info hash but not the full metadata. It uses the info hash to find peers through trackers or the DHT network, then downloads the metadata from those peers directly — a process called metadata exchange defined in the BitTorrent Extension Protocol.
3. Trackers — The Coordination Layer
A tracker is a server whose sole job is to maintain a list of peers currently downloading or seeding a specific torrent and provide that list to clients that request it.
How Tracker Communication Works
When your torrent client starts downloading a torrent, it sends an HTTP or UDP request to the tracker URL specified in the torrent file. This request is called an announce and contains:
- The info hash of the torrent
- Your peer ID — a randomly generated 20-byte identifier
- Your IP address and port number
- The amount downloaded, uploaded, and remaining
- An event parameter — started, stopped, or completed
The tracker responds with:
- A list of peers — IP addresses and port numbers of other clients currently participating in this torrent
- An interval — the number of seconds to wait before sending the next announce
- The number of seeders and leechers currently tracked
Your client then connects directly to the peers provided by the tracker. The tracker is not involved in the actual file transfer — it only provides peer addresses. Once your client has a peer list, it communicates peer-to-peer without touching the tracker again until the next announce interval.
Seeder vs Leecher Terminology
- Seeder — a client that has the complete file and is uploading it to others
- Leecher — a client that is still downloading and has an incomplete copy of the file
- Peer — a general term for any client in the swarm, seeder or leecher
The collection of all peers connected to a single torrent is called the swarm.
Tracker Types
- HTTP trackers — use standard HTTP GET requests for announces
- UDP trackers — use a more efficient UDP-based announce protocol with less overhead, now the most common type
- Private trackers — require authentication (passkey in the tracker URL) and restrict which users can participate in announces
- Scrape requests — a separate tracker request that returns aggregate statistics (seeder count, leecher count, completed downloads) for one or more torrents without joining the swarm
4. Distributed Hash Table (DHT) — Trackerless Operation
DHT is one of the most technically sophisticated components of modern BitTorrent. It allows peers to find each other without any central tracker server — making the system fully decentralised.
What a Distributed Hash Table Is
A distributed hash table is a data structure spread across many nodes in a network. Each node stores a portion of the total data. The system provides a key-value lookup — given a key (in BitTorrent’s case, an info hash), find the value (a list of peers).
BitTorrent uses the Kademlia DHT algorithm, defined in BEP 5 (BitTorrent Enhancement Proposal 5).
How DHT Works in BitTorrent
Every DHT-enabled torrent client is also a DHT node. Each node has a randomly generated 160-bit node ID. The DHT network organises nodes by the XOR distance between their node IDs — nodes that are closer in XOR distance are considered neighbours in the DHT space, regardless of their physical network location.
When you want to find peers for a torrent using DHT:
- Your client takes the torrent’s info hash (also 160 bits)
- It looks up which DHT nodes are closest to that info hash in XOR distance
- Those nodes store the peer lists for torrents whose info hash is close to their node ID
- Your client queries those nodes for peers currently downloading that info hash
- The nodes return peer lists, and your client connects directly
Bootstrapping into the DHT Network
When a client has no DHT contacts, it needs a starting point. Most clients include hardcoded bootstrap nodes — well-known stable DHT nodes maintained by torrent client projects. After contacting a bootstrap node, the client discovers its neighbours and gradually builds its own routing table of DHT contacts.
Peer Exchange (PEX)
PEX is a complementary peer discovery mechanism that works between connected peers rather than through DHT or trackers. When two peers are connected, they periodically exchange lists of other peers they are connected to. This allows the swarm to grow and reorganise without tracker or DHT involvement, and is particularly useful for keeping active swarms connected when tracker or DHT contact is temporarily unavailable.
5. The Peer Wire Protocol — How Peers Communicate
Once your client has a list of peers from the tracker, DHT, or PEX, it establishes direct TCP connections with those peers and communicates using the BitTorrent Peer Wire Protocol.
The Handshake
Every peer connection begins with a handshake. Your client sends:
- A protocol identifier string — BitTorrent protocol
- Eight reserved bytes — used to signal protocol extensions
- The 20-byte info hash of the torrent — confirming both clients are in the same swarm
- Your 20-byte peer ID
The remote peer responds with the same structure. If the info hashes match, the connection is established and message exchange begins.
Peer Wire Messages
After the handshake, peers exchange a series of typed messages:
- choke / unchoke — a peer that is choking you will not send you data. Unchoking means it is willing to upload to you
- interested / not interested — signals whether you want data from this peer
- have — announces that the peer has successfully downloaded and verified a specific piece
- bitfield — sent immediately after handshake, a compact binary representation of which pieces the peer currently has
- request — asks a peer to send a specific block of data from a specific piece
- piece — the actual data block being sent in response to a request
- cancel — cancels a previously sent request
- port — announces the DHT port for DHT-enabled clients
The Choking Algorithm and Tit-for-Tat
One of BitTorrent’s most important design decisions is its approach to upload slot management — who you choose to upload to at any given moment.
Each client maintains a limited number of upload slots. It unchokes — and therefore uploads to — a small number of peers at once, typically 4 to 6. The selection of which peers to unchoke is based on the tit-for-tat principle:
- You preferentially unchoke peers who are uploading to you at the highest rates
- Peers who reciprocate your uploads get priority for your upload bandwidth
- This creates a cooperative dynamic where uploading to others is rewarded with better download speeds
Additionally, one slot is reserved for an optimistic unchoke — a randomly selected peer that gets upload access even if they are not currently uploading to you. This allows new peers with nothing to offer yet to enter the system and begin building reciprocal relationships.
6. Pieces, Blocks, and Data Integrity
How the File Is Divided
The complete content being shared is divided into pieces during the torrent creation process. Each piece has a fixed size — defined in the torrent’s piece length field — typically ranging from 256KB to 2MB depending on the total file size. Larger torrents use larger piece sizes to keep the number of pieces manageable.
Each piece is further divided into blocks for transmission purposes. Blocks are typically 16KB. A single piece request is broken into multiple block requests — your client sends separate request messages for each 16KB block within a piece.
SHA-1 Hash Verification — The Integrity System
The torrent file contains a SHA-1 hash for every single piece. When your client finishes downloading all blocks of a piece, it:
- Assembles the blocks into the complete piece
- Computes the SHA-1 hash of that piece
- Compares the computed hash against the hash stored in the torrent file
- If they match — the piece is written to disk and marked as complete
- If they do not match — the piece is discarded and re-downloaded from other peers
This verification system means that data corruption — whether from a malicious peer sending bad data or from network transmission errors — is automatically detected and corrected. You cannot receive a permanently corrupted piece if your client is working correctly. The hash is mathematically guaranteed to detect any modification of the data, no matter how small.
Rarest First Piece Selection
When choosing which pieces to request next, BitTorrent clients use the rarest first strategy — preferentially requesting pieces that the fewest peers in the swarm have.
This serves a critical purpose. If every new peer requested the same popular pieces first, common pieces would become even more common while rare pieces might disappear entirely if the only peer that had them left the swarm. By spreading interest across rarer pieces, the strategy ensures the full file remains available across the swarm even as individual peers join and leave.
Exception — when a new peer joins with nothing downloaded, it uses a random first piece strategy to quickly acquire some data and become a useful upload source before switching to rarest first.
Endgame Mode
Near the end of a download — when only a few pieces remain — the standard request strategy can leave a download stalled waiting for slow peers. Torrent clients enter endgame mode when all remaining pieces have been requested but not yet received. In endgame mode, the client sends requests for the remaining pieces to multiple peers simultaneously, then cancels duplicate requests as pieces arrive. This eliminates the tail latency problem of near-complete downloads.
7. Extensions to the Base Protocol
The base BitTorrent protocol has been extended significantly through the BitTorrent Enhancement Proposal (BEP) process. Key extensions include:
Extension Protocol (BEP 10)
A framework for negotiating protocol extensions between peers. The eight reserved bytes in the handshake signal which extensions a client supports. This allows new features to be added without breaking compatibility with older clients.
Metadata Exchange (BEP 9)
Allows peers to exchange the torrent’s info dictionary directly — enabling magnet link functionality. A client that has only a magnet link (and therefore only the info hash) can request the full metadata from peers it finds through DHT, then use that metadata to join the swarm properly.
Fast Extension (BEP 6)
Adds several messages to reduce connection overhead — including have all, have none, and allowed fast — which allow peers to signal their complete or empty state more efficiently and pre-authorise certain pieces for request even while choked.
µTP — Micro Transport Protocol (BEP 29)
µTP is a transport protocol built on top of UDP that provides reliable ordered data delivery like TCP but with congestion control specifically designed to avoid interfering with other network traffic. When BitTorrent traffic uses µTP, it automatically reduces its bandwidth usage when the network is congested, making it more considerate of other applications sharing the same connection. Most modern torrent clients use µTP by default.
Encryption (MSE/PE)
Message Stream Encryption and Protocol Encryption are optional encryption layers applied to peer connections. They do not provide strong security — they do not authenticate peers or prevent determined analysis — but they obfuscate traffic patterns enough to defeat simple deep packet inspection systems used by some ISPs to throttle BitTorrent traffic. Enabling encryption in your torrent client settings reduces the effectiveness of ISP-level BitTorrent throttling.
8. How a Complete Download Happens — Step by Step
Putting all the components together, here is the complete sequence of events from clicking a magnet link to having a complete verified file on your disk:
Step 1 — Metadata Acquisition
Your client receives a magnet link containing an info hash. It checks its local DHT routing table for nodes closest to that info hash and sends get_peers queries to those nodes. Nodes respond with either peer lists or closer node references. Your client iterates until it finds peers and downloads the torrent metadata from them.
Step 2 — Tracker Announce
Using the tracker URLs from the metadata, your client sends announce requests to each tracker. Trackers respond with peer lists — IP addresses and ports of other clients in the swarm.
Step 3 — Peer Connection and Handshake
Your client attempts TCP (or µTP) connections to peers from the tracker and DHT responses. Successful connections complete the handshake sequence — protocol identifier, reserved bytes, info hash, peer ID exchange.
Step 4 — Bitfield Exchange
After handshake, each peer sends a bitfield message showing which pieces they currently have. Your client builds a picture of piece availability across all connected peers.
Step 5 — Interest Signalling and Unchoking
Your client sends interested messages to peers that have pieces you need. Those peers may unchoke you based on their choking algorithm. You simultaneously unchoke a selection of peers who are uploading to you at good rates.
Step 6 — Piece Downloading
Your client sends request messages for blocks within pieces, prioritising rarest pieces first. Unchoked peers respond with piece messages containing the actual data blocks. Your client assembles blocks into complete pieces.
Step 7 — Hash Verification
As each piece completes, your client computes its SHA-1 hash and compares it to the hash in the torrent metadata. Verified pieces are written to disk. Failed pieces are discarded and re-requested.
Step 8 — Have Announcements
After verifying each piece, your client sends have messages to all connected peers, announcing that you now have that piece and are available to upload it.
Step 9 — Seeding
When all pieces are verified and written to disk, your download is complete. Your client transitions from leecher to seeder status. It continues accepting connections and uploading pieces to other peers in the swarm, contributing back to the distribution network that served your download.
9. Why Some Torrents Are Faster Than Others
Understanding the protocol explains why torrent download speeds vary so dramatically.
Seeder-to-Leecher Ratio
A torrent with 500 seeders and 50 leechers will generally download very fast — there is far more upload capacity in the swarm than there is demand. A torrent with 2 seeders and 200 leechers will download slowly — the available upload capacity is spread thin across many downloaders.
Peer Geographic Distribution
The physical distance between you and your peers affects latency and therefore the efficiency of the request-response cycle for each block. A swarm composed mostly of peers in your region will generally provide better speeds than one where peers are spread across high-latency international links.
Upload Capacity and Tit-for-Tat
Because BitTorrent’s choking algorithm rewards peers who upload with better download access, your own upload speed directly affects your download speed. A peer with no upstream capacity is entirely dependent on optimistic unchokes — a slower and less reliable path to good download speeds.
Piece Availability
A torrent where some pieces exist only on a single seeder that is currently offline will stall at the point of requesting those pieces. Piece availability across the swarm must be complete for a download to finish — a single missing piece from all available peers means the download cannot complete regardless of how many other pieces are available.
10. Private Trackers and Ratio Systems
Private trackers add an authentication and accountability layer on top of the standard BitTorrent protocol.
How Private Tracker Authentication Works
On a private tracker, each user account is assigned a unique passkey — a random alphanumeric string embedded directly in the tracker URL. When your client sends announces to the tracker, the passkey in the URL identifies your account. The tracker records your upload and download totals against your account.
Because the passkey is in the tracker URL, sharing .torrent files from private trackers is explicitly prohibited — anyone who obtained your .torrent file would be announcing to the tracker using your passkey, and their download activity would be recorded against your account.
Ratio Requirements
Private trackers enforce upload-to-download ratio requirements to maintain swarm health. A common requirement is a minimum ratio of 1.0 — meaning you must upload at least as much data as you have downloaded. Users whose ratio falls below the minimum face restrictions on downloading new torrents.
This system directly addresses one of the fundamental problems of public torrenting — free riding, where users download without contributing upload bandwidth back to the swarm. By tying account status to ratio, private trackers create a community where sustained seeding is incentivised.
Hit and Run Prevention
Seeding a torrent briefly and then stopping — hit and run behaviour — is typically penalised on private trackers through minimum seed time requirements. A torrent must be seeded for a minimum number of hours or days before it counts toward a user’s upload ratio, preventing users from seeding briefly during fast initial piece exchange and then disconnecting.
Final Thoughts
BitTorrent is a genuinely remarkable piece of protocol engineering. The combination of cryptographic hash verification, distributed peer discovery through DHT, the tit-for-tat upload incentive system, rarest-first piece selection, and the endgame optimisation produces a file distribution system that becomes more resilient and faster as it scales — the exact inverse of traditional server-based distribution.
The protocol was designed in 2001 and remains architecturally sound more than two decades later. The core mechanisms described in this post are still the foundation of every torrent download happening right now across the global BitTorrent network — billions of transfers per day running on the same elegant distributed architecture that Bram Cohen designed to solve the bandwidth problem of popular file distribution.

