Skip to main content
Version: 1.0 (Latest)

Peer-to-peer setup

One database is great, but it quickly becomes useless if hamsters eat its disks or a tsunami floods its fans. Humanity has been adding redundancy to its setups ever since polygamy was a thing.

DefraDB uses peer-to-peer (P2P) networking for data exchange, synchronization, and replication of documents and commits. P2P communication does not require a central authority. There's two types of peer-to-peer relationships:

  • Publisher-Subscriber - When a node receives an update for documents on a selected collection, it broadcasts it to listening peers. The state of the collection is shared across peers.
  • Replicator - One node unilaterally pushes updates on a specific collection to a target peer.

Setup

The following P2P pages assume that you have two instances of DefraDB running on the same network. To the horror of mathematicians, we show the setup for two nodes and claim that it scales for an arbitrary number of nodes (the proof is left as an exercise to the reader).

You can start multiple DefraDB nodes, each with its root directory and port bindings, by tweaking the CLI options --rootdir, --p2paddr, and --url when calling defradb start:

Start Node1 - Ports 9171 and 9181, root dir ~/.defradb-node1
defradb start --rootdir ~/.defradb-node1 --p2paddr /ip4/127.0.0.1/tcp/9171 --url localhost:9181
Startup log for Node1
INF cli Starting DefraDB
INF p2p Created LibP2P host PeerID=12D3KooWDy7z9Y6qANCUXADpwYn7cnHoHBAL4MrAuYeWpwA9UePt Address=[/ip4/127.0.0.1/tcp/9171]
...
Start Node2 - Ports 9172 and 9182, root dir ~/.defradb-node2
defradb start --rootdir ~/.defradb-node2 --p2paddr /ip4/127.0.0.1/tcp/9172 --url localhost:9182
Startup log for Node2
INF cli Starting DefraDB
INF p2p Created LibP2P host PeerID=12D3KooWHwpvkxhfFtX7kPZSj9XJ5wvgitqZ5mt2uWVpV5kkzQX4 Address=[/ip4/127.0.0.1/tcp/9172]
...