What is DHT and How Does it Work in BitTorrent?

Distrbuted Hash Tables (DHT) are probably the least-understand component of the BitTorrent protocol, but also one of its most useful features.

DHT eliminates the need for centralized servers (trackers) and allows your torrent client to discover peers directly from the p2p network itself.

This article will explain DHT in simple terms, and help you optimize your client settings so that you can take full advantage for better availability and faster downloads.

What is DHT?

DHT stands for Distributed Hash Table. It is a node-based system that allows all participants in a network (in this case bitTorrent) to collectively store the peer list for torrent being shared.

Specifically, BitTorrent uses the Kademlia implimentation of DHT.

Key Concepts

DHT relies on core computer science and cryptographic principles to make the magic happen. The math is complex, but the system itself is rather simple and relies on only a few pieces of data.

Here are some essential concepts:

  • Hash – a unique identifier for each torrent file
  • NodeID – Unique identifier for each peer (torrent client) in the DHT network
  • Hash Table – a table of hashes (torrents) with a list of IP addresses (peers) for each.
  • Closeness – A mathematical calculation of distance between DHT nodes. This is purely based on NodeID and not geographic distance.

Here are the essential elements of BitTorrent’s DHT design:

How DHT Works in BitTorrent

When a new node (your torrent client) starts up, it joins the DHT network through a process called bootstrapping. This how other nodes are first notified of your existence, and your client can start building its hash table.

Nearly all torrent clients use the same bootstrap node: router.utorrent.com

Once you join the network, you will be allocated a share of the hash table. When other nodes request peers for a hash in your table, those requests will be routed to you, and your client will send back a response with a list of peer IP addresses.

DHT Nodes

Each torrent peer (client) is a node in DHT.

Upon joining, your node will generate a unique NodeID.

This ID will then be compared to other ID’s and you will fit into the node matrix using a closeness algorithm. In simple terms, the closer (in bits) another nodeID is to yours, the closer you’re considered in the DHT network.

Each Node has information about nearby nodes, as well as the identity of some distant nodes. The closer the node, the more information your client has about it.

Hash Tables

Hash tables replace centralized trackers in DHT. They contain just a few pieces of information (each torrent in its own row)

  • Hash of the file (unique identifier for the torrent)
  • List of peer IP addresses
  • Port number for each peer

Hashes are distributed efficiently among all nodes. Importantly, each node is assigned hashes that are close to their own NodeID (again, in bits not distance).

This allows network to know which node holds each hash, without pinging each node individually. As such, lookups can be routed efficiently through the network, involving the minimum number of middle-man nodes between the requester and owner of the hash.

Discovering Peers for Torrent

Unlike tracker-based torrents, DHT utilizes magnet links, which are essentially an encoded URL. Magnet links must contain two pieces of information:

  1. an identifier that the magnet uses the BitTorrent protocol
  2. an identifier for the torrent file itself (the hash)

When opening a magnet link, a torrent client will automatically import and decode the components of the magnet URL. It will then submit a request to the DHT network, using the hash as the key.

Successive nodes pass along the request until it reaches the node which has that key in its hash table. It will then respond (again through intermediary nodes) with the list of peers sharing that file.

Once received, your torrent client will attempt to connect to each peer individually. Once connected, it can request information about even more peers (perhaps from a tracker that peer is connected to).

Optimizing DHT Performance

As you can see, DHT makes BitTorrent more resilient and less-reliant on centralized trackers. You don’t have to worry about updating your tracker lists or constantly refreshing tracker connections. Instead, the DHT handles it all seamlessly in the background.

But if DHT isn’t working properly, you can suffer from slower speeds or poor peer availability. Here are some tips to make sure DHT is working smoothly:

Check your connection status

The bottom bar of most torrent clients will show a connection health indicator, along with a list of DHT nodes you’re connected to. If the indicator isn’t green, it may indicated a problem.

Make sure DHT is enabled

DHT enabled in Bittorrent settings

Most clients have a toggle that allows you to turn DHT on/off. Make sure it’s enabled in your client settings. You can also enable PeX (Peer Exchange) for even better availability.

Enable Port Forwarding

If your connection status is anything other than good, it may be a port forwarding issue. Try enabling UPnP in your client settings, or manually forward a port through your router.

Make sure you forward UDP traffic (not just TCP) as that’s the protocol used by DHT.

Check your Firewall

Some aggressive firewalls may block the port that DHT operates on. Add an exception for your torrent client in the firewall rules. You can also manually open the listen port in the firewall.

Consider ISP Interference

Here in the USA, several Internet Service Providers (ISPs) attempt to block or throttle BitTorrent traffic. This includes intercepting DHT requests at the network level.

If DHT is enabled in your client and port forwarding is configured correctly, your ISP’s firewall could be to blame.

The solution? Use a VPN, which will encrypt the torrent traffic and bypass your ISP’s filtering rules. I recommend Private Internet Access which keeps zero logs and costs less than $3/month at their best pricing.

Leave a Comment