Network Development

9 posts
1
P2P Network Core Principles
· 6 min read

Peer-to-Peer (P2P) networking is a decentralized architecture where every node acts as both a provider (Server) and consumer (Client). This architecture is widely used in file distribution (BitTorrent), cryptocurrency (Bitcoin), decentralized storage (IPFS), and many other domains.

P2P vs Client-Server Architecture

Before diving into P2P principles, let’s understand the fundamental differences through comparison:

FeatureClient-ServerP2P Network
CentralizationHighly centralizedDecentralized / Hybrid
Single Point of FailureExistsDoes not exist
ScalabilityLimited by serverLinear with node count
Bandwidth CostBorne by serverShared by nodes
Fault ToleranceLowHigh
Lookup ComplexityO(1)O(log N)

The core advantage of P2P lies in eliminating single points of bottleneck and failure, at the cost of introducing more complex node discovery and data routing mechanisms.

2
Kademlia DHT Protocol Deep Dive
· 6 min read

Kademlia is one of the most influential DHT (Distributed Hash Table) protocols, proposed by Petar Maymounkov and David Mazières in 2002. It is widely used in IPFS, BitTorrent, Ethereum, and many other systems. Kademlia’s revolutionary innovation lies in using XOR (exclusive or) as its distance metric, offering elegant mathematical properties and efficient routing algorithms.

XOR Distance Metric

Kademlia maps nodes and resources to the same 160-bit identifier space and defines the XOR distance function:

3
libp2p Protocol Stack & BitTorrent Protocol Deep Dive
· 6 min read

Building on our understanding of P2P core principles and Kademlia DHT, we now dive into two production-proven P2P protocols — the libp2p protocol stack and BitTorrent. They represent two different design philosophies: a general-purpose P2P framework versus a specialized file distribution protocol.

libp2p Modular Architecture

libp2p is the networking layer behind IPFS and Filecoin, providing a modular toolkit for building P2P applications. Its design philosophy is “pluggable network protocol stack for P2P applications” — developers compose transport, security, multiplexing, and application layers like building blocks.

4
Rust P2P Development: From Ping to Gossipsub
· 6 min read

Rust’s ownership model and zero-cost abstractions make it an ideal language for implementing P2P network protocols. The Rust implementation of libp2p (rust-libp2p) provides a complete protocol stack from transport to application layer.

Environment Setup

Configure dependencies in Cargo.toml:

toml
1
2
3
4
5
6
7
8
9
[dependencies]
libp2p = { version = "0.53", features = [
    "tokio", "tcp", "quic", "noise", "yamux", "mplex",
    "ping", "identify", "kad", "gossipsub", "relay", "dcutr", "macros",
] }
tokio = { version = "1.0", features = ["full"] }
tokio-stream = "0.1"
futures = "0.3"
anyhow = "1.0"

Create the project:

5
Go P2P Development: From Basics to PubSub
· 7 min read

Go’s simple concurrency model and fast compilation make it a popular choice for P2P network development. go-libp2p is one of the most complete libp2p implementations and is widely used in large projects like IPFS (Kubo).

Environment Setup

bash
1
2
3
go get github.com/libp2p/go-libp2p
go get github.com/libp2p/go-libp2p-kad-dht
go get github.com/libp2p/go-libp2p-pubsub

go.mod configuration:

6
Hands-On: Building a Distributed File Sharing System
· 6 min read

Combining theory with practice, we’ll build a real distributed file sharing system. This system will leverage technologies introduced in previous articles — Kademlia DHT for node discovery and metadata distribution, Gossipsub for broadcasting, and a custom file transfer protocol.

System Architecture

mermaid
flowchart TD
    subgraph Application
        CLI["CLI Interface"]
        API["REST API"]
    end

    subgraph Business Logic
        Index["File Index Manager"]
        Scheduler["Piece Download Scheduler"]
        Verify["Verify & Reassemble"]
    end

    subgraph P2P Network
        Discovery["Kad-DHT<br/>Peer Discovery + Metadata"]
        Broadcast["GossipSub<br/>Message Broadcast"]
        Transfer["Custom Protocol<br/>File Transfer"]
    end

    CLI --> Index
    API --> Index
    Index --> Scheduler
    Scheduler --> Verify
    Index --> Discovery
    Scheduler --> Transfer
    Broadcast -->|"New Peer Notification"| Index

Core design principles:

7
P2P Production Best Practices
· 14 min read

Moving a P2P system from prototype to production involves engineering challenges across connection management, security, observability, and deployment. This article covers six critical areas with reusable code snippets and actionable guidance.

Connection Management and Resource Limits

P2P nodes must maintain a large number of simultaneous connections. Without resource caps, a node can suffer OOM crashes or file descriptor exhaustion. Production environments require strict control over three dimensions.

Three-Layer Resource Control Model

mermaid
flowchart LR
    subgraph Transport Layer
        A1["Max Inbound<br/>Connections: 1024"] --> B1["Connection Queue"]
        A2["Max Outbound<br/>Connections: 512"] --> B1
    end

    subgraph Stream Layer
        C1["Inbound Concurrent<br/>Streams: 128"] --> D1["Stream Scheduler"]
        C2["Outbound Concurrent<br/>Streams: 256"] --> D1
    end

    subgraph Idle Management
        E1["Connection Idle<br/>Timeout: 30s"] --> F1{"Health Check"}
        E2["Stream Idle<br/>Timeout: 60s"] --> F1
        F1 -->|"Pass"| G1["Update Active Timestamp"]
        F1 -->|"Fail"| G2["Close Stream/Conn<br/>Release FD & Memory"]
    end

    B1 --> C1
    B1 --> C2
    D1 --> E1
    D1 --> E2

    style A1 fill:#4CAF50,color:#fff
    style A2 fill:#4CAF50,color:#fff
    style C1 fill:#2196F3,color:#fff
    style C2 fill:#2196F3,color:#fff
    style E1 fill:#FF9800,color:#fff
    style E2 fill:#FF9800,color:#fff

Rust Connection Management

Rust’s SwarmBuilder provides a fluent API for connection configuration:

8
TCP Congestion Control Evolution: From Reno to BBR
· 4 min read

The previous articles in this series focused on the architecture and implementation of P2P networks, but the performance bottleneck of P2P applications often lies not in the protocol layer itself, but in the underlying transport protocol — TCP congestion control. A single BitTorrent node may maintain hundreds of concurrent TCP connections, each independently performing congestion control. Choosing the wrong congestion control algorithm can severely degrade bandwidth utilization, especially on high-latency links or links with random packet loss. Understanding the evolution of congestion control not only helps P2P developers optimize transport performance but is also essential for gaining a deep understanding of how the internet transport layer operates.

9
BBR Congestion Control Algorithm Deep Dive
· 13 min read

BBR (Bottleneck Bandwidth and Round-trip propagation time), developed by Neal Cardwell, Yuchung Cheng, and others at Google, is one of the most advanced model-based congestion control algorithms available today. Unlike traditional loss-based algorithms (Reno, CUBIC), BBR explicitly models the network path by directly measuring bottleneck bandwidth and propagation delay, sending data at the BDP (Bandwidth-Delay Product) rate at the bottleneck point.

BBR’s core insight is that packet loss does not equal congestion. On deep-buffered (Bufferbloat) or wireless links, packet loss can be caused by channel noise or excessive buffer queuing rather than genuine link saturation. BBR actively measures bandwidth and latency to precisely control the sending rate, rather than passively waiting for loss signals.