Hello!

© 2024 Kishan Kumar. All rights reserved.

What is P2P and Why Should You Care?

Peer-to-Peer (P2P) architecture is a decentralized model where each node, or "peer," acts as a client and a server.

June 29, 2023

Hero

Photo by Steve Johnson on Unsplash

Peer-to-Peer (P2P) architecture is a decentralized model where each node, or "peer," acts as a client and a server. This contrasts with the traditional client-server model, where a central server provides resources or services, and clients consume them.

Here's a detailed breakdown of how P2P architecture works:

  1. Node Equality: In a P2P network, all nodes are equal. Each node can function as both a server and a client. This means each node can request services (acting as a client) and provide services (acting as a server).
  2. Decentralization: There is no central server in a P2P network. Instead, tasks and functions are distributed among the nodes in the network. This decentralization makes the network more robust, as there is no single point of failure.
  3. Resource Sharing: Each node in the network can share resources, such as processing power, disk storage, or network bandwidth, with other nodes. This resource sharing is often what makes P2P networks efficient.
  4. Scalability: P2P networks are highly scalable. As more nodes join the network, the network's total capacity increases. This is because each new node brings additional resources to the network.
  5. Routing and Discovery: In a P2P network, nodes need to be able to find each other to share resources. This is often done through a process called discovery. Once nodes have found each other, they can use routing to send messages or help each other. There are different methods for discovery and routing in P2P networks, and the best way often depends on the network's specific requirements.
  6. Redundancy and Reliability: Because resources are distributed among many nodes, P2P networks are often more reliable than traditional client-server networks. If one node fails, others can take over its tasks. This redundancy can also make P2P networks more resistant to attacks or censorship.
  7. Security: Security can be challenging in P2P networks, as there is no central authority to enforce security policies. However, various methods can secure P2P networks, such as encryption, routing protocols, and reputation systems.

Examples of P2P architecture can be seen in file-sharing networks like BitTorrent, communication applications like Skype, and cryptocurrencies like Bitcoin. Each of these uses P2P architecture to provide a service in a decentralized, scalable, and robust way.

Let's look at how a file is distributed in a Peer-to-Peer architecture.

P2P File Distribution

In this section, we consider a very natural P2P application:distributing a large file from a single server to many hosts (called peers). The file might be a new version of the Linux operating system, a software patch for an existing operating system or application, an MP3 music file, or an MPEG video file.

In client-server file distribution, the server must send a copy of the file to each of the peers — placing an enormous burden on the server and consuming a large amount of server bandwidth.

Let's consider a simple model for distributing a file to a fixed set of peers for both architecture types. In the fig shown below, the server and the peers are connected to the Internet.

Peers connected to Internet trying to download file provided by Server.

Peers connected to Internet trying to download file provided by Server.

Let's denote the upload rate of the server by Us, and the upload rate of the ith peer by Ui. Similarly, let's denote the download rate of the server by Ds and the download rate of the ith peer by Di.

Since we are talking about distributing a file, it must have some size; let's denote the file size to be F (in bits) and the number of peers that want to obtain it by N.

How long will it take to distribute this file to all the peers (N)?

The distribution time is when it takes to get a copy of the file to all N peers.

This will be tough if we don't make certain assumptions. To compute the distribution time for client-server and P2P architecture, we'll simplify the assumption that the Internet core has abundant bandwidth, implying that all of the bottlenecks are in access networks. The server and clients are not participating in any other network applications, so their upload and download access bandwidth can be fully devoted to distributing this file.

Client-Server Architecture Distribution Time

Let's first determine the distribution time for the client-server architecture, which we denote by Dcs, where cs stands for, you guessed it right, client-server. In the client-server architecture, none of the peers aids in distributing the file. Here, we make the following observations:

  • The server must transmit one copy of the file to each N peer. Thus the server must share N * F bits. Since the server's upload rate is Us, the time to distribute the file must be at least N * F/Us. Let's take some example, if we have 1-kilobyte (1kB) file (1024 bytes, or 8192 bits), and the upload rate of our server is 1 Mbps (megabit per second), and there are 100 peers (N=100). It will take at least 0.8s. It will take our server at least 0.8s to deliver it to all the peers.
  • Let Dmin denote the download rate of the peer with the lowest download rate that is, Dmin = min { D1, D2, . . . , DN}. The peer with the lowest download rate cannot obtain F bits of the file in less than F/Dmin seconds. Thus the minimum distribution time is at least F/Dmin.

Putting these two observations together, we obtain

Formula for distribution time in client-server

Formula for distribution time in client-server

This provides a lower bound on the minimum distribution time for the client-server architecture.

For N large enough, the client-server distribution time is given by N * F/Us.

Thus, the distribution time increases linearly with the number of peers N. So, for example, if the number of peers from one week to the next increases a thousand-fold from a thousand to a million, the time required to distribute the file to all peers increases by 1,000.

Peer-2-Peer Architecture Distribution Time

In the peer-to-peer (P2P) architecture, each peer can help distribute the file to other peers, unlike in the client-server architecture where only the server handles the distribution.

Calculating the distribution time in a P2P architecture is more complex because it depends on how each peer distributes portions of the file to others. However, we can derive a simple expression for the minimum distribution time based on the following observations:

  • Minimum distribution time based on server upload speed: At the start of distribution, only the server has the complete file. The server needs to send each bit of the file at least once through its access link to make it available to the peers. Therefore, the minimum distribution time is at least the file size (F) divided by the server's upload speed (Us). It assumes that the peers can redistribute the bits they receive, reducing the need for the server to send them again.
  • Minimum distribution time based on the slowest peer download speed: Similar to the client-server architecture, the peer with the slowest download speed (Dmin) cannot receive all the bits of the file in less than the time it takes to transmit the entire file (F / Dmin). Therefore, the minimum distribution time is at least F divided by the download speed of the slowest peer.
  • Total upload capacity of the system: The total upload capacity of the entire system is the sum of the server's upload rate (Us) and the upload rates of each individual peer (U1, U2, …, Un). To distribute the file to each of the N peers, a total of NF bits needs to be delivered. This delivery cannot happen faster than the total upload capacity (Utotal). Therefore, the minimum distribution time is also at least the total file size (NF) divided by the total upload capacity of the system (Utotal).

Putting these three observations together, we obtain the minimum distribution time for P2P, denoted by DP2P.

Distribution time for p2p architecture

Distribution time for p2p architecture

It provides a lower bound for the minimum distribution time for the P2P architecture. If we imagine that each peer can redistribute a bit as soon as it receives the bit, then there is a redistribution scheme that achieves this lower bound.

The figure below compares the minimum distribution time for the client-server and P2P architectures, assuming all peers have the same upload rate U. In the below figure, we have set F / u = 1 hour, Us = 10U, and Dmin ≥ Us. Thus, a peer can transmit the entire file in one hour, the server transmission rate is ten times the peer upload rate, and (for simplicity) the peer download rates are set large enough not to have an effect. We see that for the client-server architecture, the distribution time increases linearly and without bounds as the number of peers increases.

Minimum Distribution Time vs N (peers).

Minimum Distribution Time vs N (peers).

However, for the P2P architecture, the minimal distribution time is not only always less than the distribution time of the client-server architecture; it is also less than one hour for any number of peers N. Thus, applications with the P2P architecture can be self-scaling. This scalability directly results from peers being redistributors and consumers of bits.

Final Thoughts

P2P networks are a fascinating and powerful technology that can enable decentralized, distributed, and resilient applications. They can offer many benefits such as scalability, efficiency, privacy, and freedom. However, they also face some challenges such as security, reliability, and regulation. Therefore, it is important to understand how P2P networks work, what are their advantages and disadvantages, and how they can be used for different purposes.

.   .   .
.   .   .

The 0xkishan Newsletter

Subscribe to the newsletter to learn more about the decentralized web, AI and technology.

© 2024 Kishan Kumar. All rights reserved.