One of the big challenges while working with distributed databases is to sync/replicate data between the peers. There are multiple things that make replication challenging in distributed systems: There should be a way to control the access to your data, so that you can decide which peer can access your data with what permissions (read, write, etc.) Access Control: The replication should work across different devices: your PC, mobile, browser, etc. But all these devices don't have comparable of networking, storage & compute resources. Different Runtimes: Firewalls, don't allow all the peers talk to each other. So, we have to come up with new ways to connect peers that live behind these restrictive networks. Restrictive Networks: NATs In the next couple of posts, we will discuss how we at are working to make data replication so that you as a developer don't have to think about all these challenges. Dappkit developer friendly This post goes through an example of how today we replicate data between the peers using AvionDB, and our plans to make it much easier than what it is today. A peer-to-peer to-do list Well, a to-do list is not an app that will make you go nuts, but it does show the process of data replication with an easy to understand application. The app can be divided into 2 parts: Creating, listing & updating to-do(s).Syncing the to-do list across different peers. Let's get right into it. You can find the . working code & the final to-do app demo here Creating, listing & updating to-do(s) This is the easy part of the app. We create a . simple interface using bootstrap to create, list & update to-do(s) You can see a QR code too, which we will use for scanning the URL so that you can sync the to-do list on your mobile. You can also use a link that you can directly put in your browser or share it with anyone who wants to sync the to-do list. Now, we need to use AvionDB to store, list & update the to-do(s) that will be stored locally in your browser. AvionDB uses IPFS internally to store its data, so we need to import AvionDB & IPFS. We use the CDN links and add them to the so that we can access them across the whole application. public/index.html If you want to use npm modules, here are the modules for & . AvionDB IPFS Now, we need to initialize AvionDB & IPFS. This creates a Database named "TodoList", and a Collection named "todos" in it. Now, we need to add, list & update todo in the "todos" collection. Add To-do(s) You can use functions like or to add todo(s). insert insertOne Note that each record that is added in the collection will have an field added to it automatically, which serves as a unique identifier of the record. You can override the default by adding an field with your record. _id _id _id You can find more information on the functions . here Get To-do(s) You can fetch the to-do(s) using functions like & . find findOne This query returns all the to-do(s) in the "todos" collection. You can find more information on the functions . here Update To-do(s) You can update the to-do(s) using functions like , , , etc. update updateMany findOneAndUpdate This updates the isDone status of a to-do. You can find more information on the functions . here Now, as we have created a simple todo app to store, fetch & update the to-do(s), let's see how we can sync/replicate our to-do(s) with multiple devices. Syncing the to-do list across different peers Now before going into the code, let's first understand a few concepts that will help you understand peer-to-peer syncing. Let's take a real world example. As we are going through COVID pandemic, Ross & Rachel are attending their classes over the online lectures. Now, Ross & Rachel want to exchange their notes & do a group study to prepare for their test. In order to do so, here is what to need to do: Exchange their email addresses. Find a common platform to use for communication (Skype, Hangouts, Zoom, etc.). Maybe they also need to decide which language (English, French, Chinese, etc.) they would use to discuss over the call. Send an invite to their fellow participant. Join the call, exchange notes & do a group study. This is very similar to what we are going to do to sync data between the AvionDB peers. In order to sync the data between any 2 (or more) peers, we need to: First, find each-others addresses by which we can connect. Find a common protocol (websocket, webrtc) that both the peers can use to communicate. Establish a connection using the addresses & the common protocol. And at last sync the data (to-do list). Let's go through each of these points and see how this works under the hood. Finding Addresses Each peer in the network has a unique address that can used to uniquely identify the peer in the network, known as . peerIds In case of AvionDB, we use (an internal part of IPFS) , which look something like this: . Libp2p peerIds Qme499EjQog7UjvuJiduzw1UKMe6hZ1rZ1wQrxz7qpq7TS These are generated using a project called , which is used to create interoperable, future-proof protocols. peerIds Multiformats You don't need to go too deep into Libp2p or Multiformats. You just need to know that we use the as addresses (email Id, or a mobile number) to identify & communicate with other peers in the network. peerIds Common Protocol for Communication As we talked about in the beginning of this post, different devices have different networking, storage & compute resources. Different Runtimes This means that a browser does not have as much networking, storage & compute resources as a PC. As we talked about the Ross & Rachel example, we saw that we needed to decide on a common platform & language that they could use to communicate. Similarly, a browser, a PC and a mobile need to decide what common protocols they use so that they can communicate with each other, despite of their differences in networking, storage & compute resources. A fact that you should know here is that all these platforms support protocol over TCP. So, here websocket is the "common platform or language" that all the devices can use to communicate with each other. websocket Now, we have the common protocol, but there are restrictive environments such as firewalls & NATs that restrict the peers from discovering each other. Here is where circuit-relay comes into the picture. Circuit Relay In p2p networks there are many cases where two nodes can't talk to each other directly. That may happen because of network topology, i.e. NATs, or execution environments - for example browser nodes can't connect to each other directly because they lack any sort of socket functionality and relaying on specialized rendezvous nodes introduces an undesirable centralization point to the network. A is a way to solve this problem - it is a node that allows two other nodes that can't otherwise talk to each other, use a third node, a relay to do so. circuit-relay How does circuit relay work? Here is a simple diagram depicting how a typical circuit-relay connection might look: tries to connect to but, UH-OH! There is a firewall in between that's preventing it from happening. If both and know about a relay, they can use it to establish the connection. Peer A Peer B Peer A Peer B This is what it looks like, in simplified steps: tries to connect to over one of its known addresses. Peer A Peer B Connection fails because of firewall/NAT/incompatible transports/etc... Both and know of the same relay - Peer A Peer B Peer C falls back to dialing over to using its address, which involves: 1. sends a request to 2. extracts the destination address, figures out that a circuit to is being requested 3. sends a request to 4. responds with a message 5. Relay proceed to create a circuit over the two nodes Peer A Peer C Peer B '/p2p-circuit' Peer A HOP Peer C Relay Peer B Relay STOP Peer B Peer B SUCCESS and are now connected over Peer C Peer A Peer B That's it! What's up with this and ? HOP STOP Circuit relay consists of two logical parts — dialer/listener and relay ( ). The listener is also known as the node. Each of these — dial, listen, and relay — happen on a different node. If we use the nodes from the above example, it looks something like this: HOP STOP The knows how to dial a ( ) - dialer relay HOP Node A The ( ) knows how to contact a destination node ( ) and create a circuit - node relay HOP STOP Relay The ( ) knows how to process relay requests that come from the relay ( ) node - listener STOP HOP Node B Fun fact - the HOP and STOP names are also used internally by circuit to identify the network message types. A few caveats (and features) There are a couple of caveats and features to be aware of: A will only work if it already has a connection to the node Relay STOP No dialing is supported. It's a feature planed for upcoming releases from Libp2p (no date on this one) multihop dialing is when several relays are used to establish the connection. multihop It is possible to use explicit relay addresses to connect to a node, or even to listen for connections on. We will talk more about this below. A word on circuit relay addresses A circuit relay address is a that describes how to either connect to a peer over a relay (or relays), or allow a peer to announce it is reachable over a particular relay or any relay it is already connected to. multiaddress Circuit relay addresses are very flexible and can describe many different aspects of how to establish the relayed connection. In its simplest form, it looks something like this: /p2p-circuit/ipfs/QmPeer If we want to be specific as to which transport we want to use to establish the relay, we can encode that in the address as well: /ip4/ /tcp/ /ipfs/QmRelay/p2p-circuit/ipfs/QmPeer 127.0 .0 .1 65000 This tells us that we want to use located at address 127.0.0.1 and port 65000. QmRelay /ip4/ /tcp/ /ipfs/QmRelay/p2p-circuit/ip4/ /tcp/ /ws/ipfs/QmPeer 127.0 .0 .1 65000 127.0 .0 .1 8080 We can take it a step further and encode the same information for the destination peer. In this case, we have it located at 127.0.0.1 on port 8080 and using a Web sockets transport! /ip4/127.0.0.1/tcp/65000/ipfs/QmRelay/p2p-circuit If a node is configured with this address, it will use the specified host ( ) as a relay and it will be reachable over this relay. /ip4/127.0.0.1/tcp/65000/ipfs/QmRelay There could multiple addresses of this sort specified in the config, in which case the node will be reachable over all of them.This is useful if, for example, the node is behind a firewall but wants to be reachable from the outside over a specific relay. Other use-cases are also supported by this scheme, e.g. we can have multiple hops (circuit-relay nodes) encoded in the address, something planed for future releases. In our to-do list example we created a relay peer with the following multiaddress: /dnsaddr/node1.dappkit.io/tcp/ /wss/p2p/QmfLwmXF25u1n5pD8yXcbmZew3td66qPU1FroWNrkxS4bt 6969 You can use this relay to connect your peers too! Now as we have discussed how to get through & let's now move on to see how to establish a connection. different runtimes restrictive networks, Establish a connection In order to connect to any peer using IPFS, we have a function where is the address of any peer that a peer wants to connect to. ipfs.swarm.connect(multiaddress) multiaddress So, for example if wants to connect to the relay – then we will do something like this: Peer A Peer C After, we have connected & with the relay – we can now use the address of to connect to or vice-versa. Peer A Peer B Peer C /p2p-circuit Peer B Peer A Voila! You have now connected the peers 🎉 Now we just need to replicate the data between the 2 peers. Sync data between the peers In order to sync data between the peers, we need the database address of the “TodoList” database & the name of the collection that we created above. To get this information we need to communicate this data between the peers. For this, we use something called Publish/Subscribe Publish/Subscribe is a system where peers congregate around topics they are interested in. Peers interested in a topic are said to be subscribed to that topic: Peers can send messages to topics. Each message gets delivered to all peers subscribed to the topic: So, if we create a topic: of so that anyone who is subscribed to of can get the messages from . This way can publish the database address of the "TodoList" database & the name of the collection to all its subscribers in one go! peerId Peer A peerId Peer A Peer A Peer A Here is the example code showing subscribed to using its and using the published message ( ) to open the database & the collection. Peer B Peer A peerId msg Here is an example code showing publishing the message ( ) to the topic . Peer A msg peerIdA And that’s it! This is how we currently sync data between peers in AvionDB. Here is a working demo of the to-do list app. Here are some useful links: Check out the live app . here Check out the code . here Check out AvionDB . here But this is not something we want you to go through when you are creating your application. We want to give developers a simple way to sync data that abstracts away all this complexity of , etc. different runtimes restrictive networks, So we are working on making this a better experience for the developers so that they can focus on building applications rather than tackling these core issues. If you want to keep a close track of our progress then . feel free to reach us out on our Discord server NOTE: We have not talked about Access Control in this post, but we have a ton of examples already built for different Web 2.0 & Web 3.0 based access control. Feel free to check them out here . We will be back with posts on access controller too. Meanwhile, if you have any questions, feel free to reach us out on our Discord server .