Skip to main content

What is Decentralization?

GUN is decentralized by default, meaning there’s no single master server or central authority controlling the data. Instead, every peer (browser, server, mobile app) can read, write, and sync data directly with other peers in a mesh network.
Traditional databases use a master-slave architecture where all data flows through a central server. GUN eliminates this bottleneck entirely.

Peer-to-Peer Architecture

No Master, No Slave

In GUN’s architecture, all peers are equal:
Each peer can:
  • Write data locally
  • Read data from any peer
  • Relay data to other peers
  • Sync automatically when online
  • Work offline and sync later

Mesh Network Topology

GUN uses a mesh network where peers connect to multiple other peers:
Each connection is bidirectional, and data can flow through any path.

How Mesh Networking Works

The mesh networking logic lives in src/mesh.js. Here’s how it works:

1. Peer Discovery

When a peer starts, it connects to configured relay peers:
Peers automatically discover each other through relays.

2. Message Routing

From src/mesh.js:24, when a peer receives data:
Messages are routed using the DAM (Data At Mesh) protocol.

3. Deduplication

GUN prevents message loops using deduplication (src/dup.js):
Each message gets a unique ID. Peers track which messages they’ve seen and which peers have already received them.

DAM: Data At Mesh Protocol

GUN’s mesh protocol is called DAM. It handles:

Message Format

Put Operation Across Mesh

From src/mesh.js:127-147, the say function handles broadcasting:

Get Operation Across Mesh

Synchronization Strategies

Eventual Consistency

GUN uses eventual consistency - all peers will eventually have the same data:
GUN uses CRDTs (Conflict-free Replicated Data Types) to automatically resolve conflicts. Learn more in CRDT Concepts.

Gossip Protocol

Peers “gossip” data to each other:
  1. Peer A writes data
  2. Peer A tells its neighbors
  3. Those neighbors tell their neighbors
  4. Data spreads through the entire mesh

Relay Peers

While GUN is peer-to-peer, relay peers help browsers connect to each other:

Why Relays?

Browsers can’t directly connect to other browsers due to NAT/firewall restrictions. Relays bridge this gap:

Setting Up a Relay

You can run multiple relays for redundancy. If one goes down, peers automatically use the others.

Connecting to Relays

Peer Communication

From src/mesh.js, peers communicate over various transports:

WebSocket (Browser ↔ Server)

HTTP Long-Polling (Fallback)

If WebSocket fails, GUN falls back to HTTP polling.

In-Memory (Same Process)

Network Partitions

GUN handles network splits gracefully:
GUN is partition tolerant (the ‘P’ in CAP theorem). It continues working even when the network splits.

Decentralized by Design

Benefits

No Single Point of Failure

If one peer goes down, others keep working

Scalability

More peers = more capacity, no bottleneck

Low Latency

Read from nearest peer, no round trip to central server

Censorship Resistant

No central authority can block or delete data

Trade-offs

  • Eventual consistency instead of immediate consistency
  • Higher complexity in conflict resolution
  • More bandwidth usage (gossiping data)
  • Storage on multiple peers

Example: Decentralized Chat

Best Practices

1

Run multiple relays

Don’t rely on a single relay. Run 2-3 for redundancy.
2

Use relay arrays

3

Handle offline gracefully

GUN works offline automatically, but inform users when disconnected.
4

Monitor mesh health

Track connected peers and relay status in production apps.

Performance

From the GUN source, mesh performance characteristics:
  • Message deduplication is O(1) using hash lookups
  • Routing is O(peers) but bounded by connection limits
  • Bandwidth scales with number of peers
  • CPU usage is minimal thanks to async event-driven architecture
Broadcasting to thousands of direct peers can be expensive. Use relay peers to form a hierarchical mesh topology for large networks.

Next Steps

Realtime Sync

Learn how GUN syncs data in realtime

Offline-First

Discover how GUN works offline