What is a P2P (Peer to Peer) Network?
Tags: p2p, web3, beginners, learning
Published: 2024-04-02 17:29:12

Hi all!

![Hi Gif](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExNGU5azJoeTM0Nng4MTgzYmVrcTI0dDltMTRjcXJ5Mm02emoyNTg1aSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/QLKSt3wQqlj7a/giphy.gif)

I started learning about P2P networks weeks ago and want to share with you what I understand.
So, here we go... :D 

---

## What is ✨P2P✨?
  
P2P is a computer network where all computers have the same privileges and functions. In a client-server model, the clients make the requests, the server responds, and the network needs a main server.
  However, in a P2P network, **all parties could work as clients and servers**. The network is organized in a _decentralized_ manner (it does not need a main server), where all the peers are connected.


![Example P2P and Client-Server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qkje6r81eyze6zjcqpig.jpg)

---
###How does P2P work? 🤓☝️

First, we need to understand how a **client-server structure** works to know how a P2P network works. 

When you watch a video on a streaming platform like YouTube, your device (your phone, TV, or computer) works as a client. So, you open the app, select a video to watch, and the app sends a request to the server. The server will receive the request and provide you with the content you want to watch on your device.

But if you want to make the same request in a **P2P network** instead of sending it to a central server, your device will simultaneously send requests to multiple computers (also called _peers_) within the network. 

Then, each peer will respond with a part of the video. This means that instead of downloading the entire video from a single source (a main server), you download separate parts from multiple sources within the network. 

At the same time, as your device receives the parts of the video, it will start sharing those parts with other clients asking for the same content. Unlike in the client-server structure, where the data transfers occur in one direction, these actions in a P2P network go in many different directions simultaneously among the interconnected devices. 

![Image description](https://tukuimg.bdstatic.com/scrop/d2842788a8217d421f3c1b814d42c84e.gif)

 



>⭐️ Fun fact ⭐️ In the beginning, Spotify used a peer-to-peer structure to avoid overwhelming its servers.

---

###What type of P2P architecture exists? 


![There's three actually](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49x5om5pspq2wsafbrkr.png)

There are three P2P structures, depending on how peers manage their connections.

![Resume of P2P structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0v6t1ymu3fbkx5c3234p.jpg)

In **Structured P2P networks**, the network uses a DHT (Distributed Has Table) to store the link information. This makes it easy to know where the information you need is stored and in which peer it is stored, which means that when you need to search, it will return the information faster.

In **Unstructured P2P networks**, the other peers don't know the link information, so when they need to get the data, they have to use the flooding technique to look for the peer that contains the information. 

> With flooding, a node searching for a resource contacts its neighbors in the system, which in turn contact their own neighbors and so on until a node that shares the requested resource is located.

In **Hybrid P2P networks**, we use a main server that contains all the information of the peers. So, the request goes directly to the main server, and then the server redirects the request to the peers with the information you need. 




---

### The advantages of P2P: 


####_Security_
 Since P2P networks don't have a main server (in most cases), they cannot be easily hacked. Even if one of the peers is attacked, the impact on the entire system is minimal.


####_Scalability_
 As more peers interact with the data, more peers work as servers. If we consider this, the capacity of a P2P network is unlimited. 


####_Flexibility_
  As mentioned earlier, various types of P2P networks are available. Depending on our requirements, we can choose and implement the structure that best suits our needs for managing and securing our application's information.

---

### The disadvantages of P2P: 


####_Copyright_
Since we don't have a main server controlling the uploaded data, it's hard to ensure that it does not violate copyright laws, which could result in legal issues.

####_Dependencies_
Changes in one computer on the network can affect all other systems, as they are interconnected. The entire system could be at risk if someone deletes information or adds bad-intentioned data. 

####_Unavailable information_
A P2P network works by having peers connected to it. If the peers that contain the data that you need are turned off, those data will be unavailable until the peers are connected to the network again.

---

And... I think that is all for now. 

I hope this will help you understand a little more about P2P networks. And, if there's something you don't understand or think I could add something else, please let me know. 

![patito uwu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g9r2g23s45hu2vxrktww.jpg)

Thank you!