Overview
GenosDB’s synchronization engine is designed to handle the reality of distributed networks where peers can have vastly different states. The system intelligently switches between two modes—high-efficiency delta updates and guaranteed full-state fallback—to ensure eventual consistency with optimal performance.How P2P Sync Works
GenosDB uses a hybrid synchronization protocol that combines real-time delta updates with a full-state fallback mechanism. This dual-mode architecture offers the performance of delta-syncing with the absolute reliability of full-state reconciliation.P2P networking is enabled by initializing the database with
{ rtc: true }. The database name serves as the room identifier for peer discovery.Peer Discovery with Nostr
GenosDB uses the Nostr protocol for decentralized peer discovery and WebRTC signaling:- Decentralized Relays: Nostr relays facilitate peer discovery without centralized servers
- WebRTC Signaling: SDP offers and ICE candidates are exchanged through Nostr messages
- Room-Based Scoping: The database name serves as the room ID for grouping peers
- End-to-End Encryption: All signaling messages are cryptographically secured
Find public and private Nostr relays at nostr.watch/relays/find
WebRTC Connections
Once peers discover each other via Nostr, they establish direct WebRTC connections for data transfer:- Direct Peer-to-Peer: Data flows directly between browsers without intermediary servers
- NAT Traversal: TURN servers can be configured for peers behind restrictive firewalls
- Data Channels: Named channels carry database operations and custom application data
- Media Streams: Audio and video can be shared alongside data
Delta Sync: High-Efficiency Updates
For peers that are frequently communicating, transmitting the entire graph for minor changes would be inefficient. GenosDB’s primary synchronization method uses delta updates powered by a persistent Operation Log (Oplog).How Delta Sync Works
Operation Logging
Every local mutation (
put, remove, link) is recorded in a capped, sliding-window log persisted in localStorage. Each entry contains:- Operation type (
upsert,remove,link) - Affected node ID
- Hybrid Logical Clock (HLC) timestamp for causal ordering
Sync Handshake
When a peer connects or needs to catch up, it broadcasts a
sync request containing the HLC timestamp of the last operation it processed (globalTimestamp).A brand-new peer with no history sends globalTimestamp: null.Delta Calculation
Upon receiving a sync request, a peer:
- Consults its Oplog
- Filters operations with timestamps greater than the requesting peer’s
globalTimestamp - Hydrates
upsertoperations by fetching full node values from the graph - Creates a self-contained delta set
Compressed Transfer
The delta array is:
- Serialized using MessagePack for compact binary format
- Compressed with pako (deflate) for minimal bandwidth
- Sent in a
deltaSyncmessage over WebRTC data channel
Oplog Configuration
Full-State Fallback: Guaranteed Consistency
A delta update is only possible if a peer’s history overlaps with the Oplog of its peers. GenosDB gracefully handles scenarios where this isn’t the case by automatically triggering a Full-State Fallback.Fallback Triggers
Full-state sync is initiated when:- Out-of-Range Timestamp: A peer’s
globalTimestampis older than the oldest operation in the Oplog - New Peer: A peer sends
globalTimestamp: null, indicating no previous state
The Fallback Process
Full-State Transmission
The up-to-date peer serializes and compresses its entire current graph state and sends it in a
syncReceive message.State Reconciliation
The desynchronized peer:
- Discards its outdated local graph state
- Replaces it with the new full state
- Clears its own Oplog (previous history is now invalid)
Synchronization Modes Compared
Delta Sync
Best For: Active peers with overlapping historyPros:
- Minimal bandwidth usage
- Near-instant updates
- Efficient for frequent changes
- Requires overlapping Oplog history
- Limited by oplogSize window
Full-State Fallback
Best For: New peers or long-disconnected clientsPros:
- Guaranteed consistency
- Works for any state difference
- Resets peer to known-good state
- Larger initial transfer
- Discards local conflicting state
Cross-Tab Synchronization
In addition to P2P sync between devices, GenosDB synchronizes instantly between browser tabs on the same machine usingBroadcastChannel:
Cross-tab sync works even without
rtc: true enabled, providing local-first synchronization within a single browser.Data Pipeline
Here’s how data flows through GenosDB’s sync pipeline:Cellular Mesh for Large-Scale Networks
For applications with 100+ concurrent peers, GenosDB offers a Cellular Mesh topology that reduces connection complexity from O(N²) to O(N):Standard Mesh
Recommended for: < 50 peers
- Full mesh topology
- Single-hop message delivery
- Lower latency
- O(N²) connections
Cellular Mesh
Recommended for: 100+ peers
- Organized into cells with bridges
- Multi-hop message routing
- Slight latency increase
- O(N) connections
Learn more about Cellular Mesh architecture in the P2P Setup Guide.
Best Practices
Optimize Oplog Size
Handle Peer Events
Monitor Sync Status
While GenosDB doesn’t expose a direct “sync status” API, you can monitor peer connections:Related Resources
P2P Setup Guide
Configure relays, TURN servers, and cellular mesh
Conflict Resolution
Learn how GenosDB resolves concurrent updates
Security Model
Understand RBAC and cryptographic verification
Real-Time Subscriptions
Set up reactive queries with live updates