paint-brush
P2P Architecture: How Does It Work?by@0xkishan
227 reads

P2P Architecture: How Does It Work?

by Kishan KumarJuly 4th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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-s

People Mentioned

Mention Thumbnail
featured image -  P2P Architecture: How Does It Work?
Kishan Kumar HackerNoon profile picture

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

This section considers 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. The Server and the peers are connected to the Internet in the figure below.

Peers connected to the Internet trying to download files 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 distributing this file to all the peers (N) take?

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 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

First, determine the client-server architecture's distribution time, which we denote by D_cs_. 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 an example if we have a 1-kilobyte (1kB) file (1024 bytes), the upload rate of our server is 1 Mbps (megabit per second), and there are 100 peers (N=100). 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

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

Unlike in the client-server (P2P) architecture, each peer can help distribute the file in a peer-to-peer architecture. When a peer receives some file data, it can use its upload capacity to share that data with other peers.


Calculating the distribution time in P2P architecture is more complex than in client-server architecture because it depends on how peers distribute portions of the file to each other. However, we can obtain a simple expression for the minimum distribution time based on the following observations:


  • Minimum distribution time based on server upload speed: Only the server has the complete file at the start of distribution. 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 peer (U1, U2, …, UN). A total of NF bits must be delivered to distribute the file to each of the N peers. This delivery cannot happen faster than the total upload capacity (Utotal). Therefore, the minimum distribution time is at least the full 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

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 Figure 2.23, 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). Image by Kishan Kumar

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 enabling 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 essential to understand how P2P networks work, their advantages and disadvantages, and how they can be used for different purposes.


I hope this article gave you a better understanding of what P2P is.

I thank you for taking the time to read my article. Do support me by following me on Hacker Noon so that you get notified whenever I publish an article.


Also published here.