3 min read

Peer-to-Peer (P2P) Architecture

Peer-to-Peer (P2P) architecture is a distributed application architecture that partitions tasks or workloads between peers. Peers are equally privileged, equipotent participants in the application. They are said to form a peer-to-peer network of nodes.

1. Core Concept

In a P2P network, there is no central server that holds all the data or coordinates all the actions. Instead, every node (peer) acts as both a client and a server.

  • Client-Server: Clients request resources; Servers provide them.
  • P2P: Peers request resources from other peers and provide resources to other peers.

2. Types of P2P Networks

Unstructured P2P

The network is formed by nodes establishing connections randomly to each other.

  • Search: To find data, a node floods the network with queries (Gossip Protocol).
  • Pros: Easy to build, robust against high churn (nodes joining/leaving).
  • Cons: Search is inefficient; no guarantee that data will be found even if it exists.
  • Example: Early Gnutella, Bitcoin (for transaction propagation).

Structured P2P

The network topology is tightly controlled, and data is placed at specific locations. This is usually achieved using a Distributed Hash Table (DHT).

  • Search: Efficient search (typically $O(\log N)$) using keys.
  • Pros: Guaranteed data retrieval.
  • Cons: Higher overhead to maintain the structure when nodes join/leave.
  • Example: BitTorrent (DHT), IPFS, Kademlia.

Hybrid P2P

Combines P2P with a central server. The server keeps an index of where files are located (or handles login), but the actual file transfer happens directly between peers.

  • Example: Spotify (early versions), Skype (early versions).

3. Key Characteristics

  • Decentralization: No single point of failure. If one peer goes down, the network survives.
  • Scalability: As more peers join, the total capacity (bandwidth, storage, processing power) of the system increases. In client-server, more clients usually degrade performance.
  • Resilience: Highly resistant to censorship and attacks.

4. Use Cases

  • File Sharing: BitTorrent is the most famous example. Large files are split into chunks and shared across thousands of peers.
  • Blockchain: Bitcoin and Ethereum rely on P2P networks to propagate transactions and blocks without a central bank.
  • Content Delivery: Some CDNs use P2P to offload traffic from their main servers (e.g., delivering software updates).

5. Challenges

  • Security: Since there is no central authority, it is hard to trust peers. Malicious peers can distribute malware or fake data (Sybil Attack).
  • Churn: Nodes are constantly joining and leaving, making the network unstable.
  • Freeriding: Peers who download data but don't upload (leeches) reduce the network's effectiveness.

6. Gossip Protocol

The Gossip Protocol (or Epidemic Protocol) is a communication protocol used in P2P networks to disseminate information in a manner similar to the way a virus or rumor spreads in a biological or social community.

  • Mechanism: Periodically, a node selects a random peer from its list of neighbors and exchanges information (state, data updates, or membership lists).
  • Properties:
    • Robust: Even if some nodes fail or messages are lost, the information eventually reaches everyone.
    • Scalable: The load on any single node is constant, regardless of network size.
    • Eventually Consistent: It takes time for an update to propagate to all nodes ($O(\log N)$ rounds).
  • Use Cases: Failure detection (Cassandra), membership management (Consul), blockchain transaction propagation.

programming/client-server-architecture programming/distributed-systems programming/consistent-hashing