Peer-to-Peer

P2P

Escrito por Darjus Kazakevic · revisado el

No central server: every node serves and consumes, so you own discovery and trust.

In peer-to-peer (P2P), every node is both a client and a server. Nodes share data and work directly, with no central authority. In BitTorrent, a file is cut into pieces, and each downloader also uploads the pieces it already has, so a popular file spreads faster as more people fetch it. Local-first apps and blockchains use the same shape.

What a server used to do does not disappear; it becomes your problem. Discovery: how does a new node find peers? Consistency: two peers edit offline, and nobody decides who wins. Trust: any peer may lie, so you verify everything it sends.

En esencia — Let every node act as both client and server and share data directly, trading a central authority for resilience and the hard problems it used to solve.

Cuándo recurrir a él

Recurre a él cuando

  • No single party can or should be trusted to run a central server
  • Nodes must work offline and sync when they reconnect
  • Large files go to many users, and each user can help share them

Reconsidéralo cuando

  • One organisation owns the data and can run a server; it is far simpler
  • Rules need one global order: unique usernames, no overdrawn balance
  • Most nodes are phones on batteries or behind strict firewalls

The hard parts

Peers
Each node both requests and serves. Total capacity grows with the number of peers, because every new peer also brings upload bandwidth and storage.
Discovery
A new node must find others. It starts from a few known bootstrap nodes, then uses a tracker or a distributed hash table (DHT): a lookup table split across all peers.
Content addressing
Data is named by its hash. A peer checks every piece it receives against that hash, so a lying peer cannot slip in bad data.
Convergence without an authority
Peers apply updates in different orders. A CRDT (conflict-free replicated data type) has a merge that gives the same result in any order, so all peers end up equal.

Reglas de diseño

Make every merge order-independent
Peers see the same updates in different orders and must still agree. Make merge commutative and idempotent, with a tiebreak both sides can compute alone.
Verify every peer's claim
No one can vouch for anyone, so an unverified update is just an attacker's input. Check the hash or signature before you merge, and drop what fails.
Plan for peers that disappear
Peers go offline all the time. Store each piece on several peers, and treat any single peer as gone at any moment.
Use a server where you need one global order
Unique usernames or account balances need one authority. Many real systems mix the two: P2P for the data, a small server for sign-in and discovery.

Errores habituales

  • Merging by arrival order, so peers end up with different data
  • Trusting data from a peer without checking its hash or signature
  • Storing each item on one peer, so it vanishes when that peer goes offline
  • Using P2P for a feature that needs a global rule, like unique names