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:- 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:How Mesh Networking Works
The mesh networking logic lives insrc/mesh.js. Here’s how it works:
1. Peer Discovery
When a peer starts, it connects to configured relay peers:2. Message Routing
Fromsrc/mesh.js:24, when a peer receives data:
3. Deduplication
GUN prevents message loops using deduplication (src/dup.js):
DAM: Data At Mesh Protocol
GUN’s mesh protocol is called DAM. It handles:Message Format
Put Operation Across Mesh
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:- Peer A writes data
- Peer A tells its neighbors
- Those neighbors tell their neighbors
- 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
Connecting to Relays
Peer Communication
Fromsrc/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
Next Steps
Realtime Sync
Learn how GUN syncs data in realtime
Offline-First
Discover how GUN works offline