Basic Concepts
Mesh Networking 101
the mesh is an underlay for data sync within ditto, operating independently of your queries and sync subscriptions data updates propagate through the mesh automatically, provided your sync subscriptions match w hether you have two devices or two thousand devices in your mesh, ditto incorporates several algorithms to maintain an optimal mesh network at all times this article provides an overview of the very basics of ditto's mesh technology, covering key topics such as communication transports like bluetooth low energy (le) the multiplexer that enables ditto's rainbow connection the flood fill design pattern peer to peer authentication and access controls and permissions concurrency conflict resolution strategies introduction upon initiating the sync process by invoking the startsync function in your app, peers with the same app id immediately form a mesh network using a mixture of communication transports, each with its advantages and disadvantages for example, ditto prioritizes wi fi for its high bandwidth and only falls back to bluetooth le when needed to take advantage of its longer range capabilities unlike typical home networks — which represent a star topology where all devices connect directly to a central router, switch, or access point — a peer to peer mesh network offers multiple pathways for communication here's a quick video overview by ditto's network transports team lead, tom karpiniec transports ditto leverages an array of communication transports , supporting a variety of use cases and end user devices communication transports are networking protocols that facilitate data transmission across different environments for instance, using the local area network (lan) transport for data movement in an office like setting which transports ditto supports depends on what kind of device or small peer sdk you are using, for example a mobile phone can use bluetooth le, lan, wi fi direct, and websocket a web app running in a browser can use websocket a raspberry pi can use bluetooth le, lan, and websocket w hile many transports are set up automatically, others, such as websocket, if configured with the big peer, require that you manually configure them in your app big peer and on premise servers in addition to peer to peer transports like bluetooth le, you can opt for decentralized cloud deployment, as well as provide third party on prem servers like a legacy business intelligence system, which you configure by simply specifying its url at ditto, we call a server like this a big peer , while regular sdks are small peers many apps need to sync data with the big peer or an on premise server the ditto sdk uses peer to peer and big peer connections simultaneously, turning your mesh global this is as easy as setting your server's url big peers are especially important for those who want to use ditto in a web browser since web browsers have very limited peer to peer capabilities — you can configure the javascript sdk with the big peer and sync an sdk can sync in any of the following ways di rectly with a big peer locally with small peers with both at the same time, acting as a bridge for devices without internet access bluetooth low energy ditto utilizes both traditional wi fi and bluetooth low energy (le) to maintain a continuous mesh network of transmission control protocol (tcp) connections bluetooth le technology forms low powered and high distance connections between devices, making it highly performant in offline scenarios when replicating small amounts of data in the event of a wi fi network disruption, such as a router getting disconnected, ditto does not automatically switch transports to establish an independent bluetooth le connection as a fallback instead, ditto actively works to maintain the previously established wi fi and bluetooth le connections the following visual overview illustrates how ditto uses bluetooth le to form low powered, high distance connections between peer devices the distances and bandwidth limitations and capacities illustrated in the video are approximations and vary depending on the bluetooth® firmware installed on the device peer to peer wi fi available to most devices, standard peer to peer wi fi enables direct, point to point connections between peers without requiring traditional network infrastructure like a centralized server, router, or access point in addition to standard peer to peer wi fi, ditto incorporates a customized multiplexer to increase frequency, speed, and efficiency of data transmission between peers participating in the mesh the following table provides an overview of the wi fi technologies ditto supports platform ditto supported technology apple apple wireless direct link (awdl) android wi fi aware windows wi fi aware the following video illustrates ditto’s sophisticated and decentralized approach to peer to peer wi fi connections between distributed peers connected in the mesh network the maximum distance of a connection can vary depending on the specific peer to peer transport type being used local area network when devices are connected over the same wi fi access point or through other means like an ethernet cable, they can take advantage of a local area network ( lan ), if available, to communicate directly with each other without requiring internet access a lan is an interconnected network made up of devices that are physically near each other, such as a home, enterprise, or college campus websocket peers use websocket connections to enable realtime, bidirectional sync between small peers and the optional cloud deployment, the big peer before you can establish a connection with the big peer using a websocket, you need to specify an ip/port (see docid\ zuw1dwpowaimbpntcb3z3 ) the defining attributes of a websocket include requires an internet connection to establish peer to peer communication unlike the other peer to peer transport types, does not automatically discover other peers physically nearby websocket connections can be made over a transmission control protocol (tcp) or local area network (lan) multiplexer ditto incorporates a multiplexer inside the peer to peer mesh network to facilitate data sync developed by ditto, the multiplexer is an intelligent sync machine that seamlessly switches between active transport types as needed, without duplicating data in addition, the multiplexer breaks down data packets into small fragments and then, once received on the other side, reassembles them rainbow connection when the transports collaborate in parallel and the multiplexer automatically switches between them to establish the most optimal connection, these diverse transport types collectively form what is known as the rainbow connection — each color of the rainbow symbolizes a different transport type flood fill process when all peers connected in the mesh need to share the same view of the data, ditto initiates the flood fill process to multihop data from one connected peer to another connected peer by way of intermediate “hops” along a given path flood fill, or flooding for short, is a common pattern in a mesh network topology for enhancing efficiency and reliability by ensuring data propagates across connected peers efficiently and reliably, regardless of their queries when flooding the mesh, data moves from one connected peer to another through intermediate "hops" along a defined path for example, imagine a daisy chain from point a to point b, as illustrated in the graphic on the right benefits of mesh wide flooding the benefits of flooding the mesh include guaranteeing a consistent offline first sync of critical data, ensuring data remains consistent even when connectivity is intermittent improving the overall responsiveness of your app, leading to faster interactions reducing costs by optimizing data operations since, when feasible, you can read and write data to a nearby peer instead of traversing a very fragile, low‑bandwidth data source preconditions to multihop for the multihop communication process to succeed, every peer within the chain, or connecting group , must meet the following preconditions share the same subscription accessible through the transport mechanism for example, if peers a , b , and c in the following graphic are subscribed to the same query, changes are synchronized across them, meaning all peers in the chain are aware of the same dataset multihopping to the big peer if a peer within the mesh network gains internet access at any point, all mesh‑generated data is automatically routed to the big peer through multihop this multihop connection to the big peer enables a small peer to establish a secure and encrypted connection through one or more intermediate peers ditto link when peers establish a mesh network connection, they create multiple encrypted internal tunnels, or links , using the transport layer security (tls) protocol nearby peers then use these tls secured links to transmit data similarly to the virtual private network (vpn), these links create virtual private connections utilizing the noise protocol to establish secure and protected connections the noise protocol is a cryptographic framework for advanced security protections for more information, see the official documentation at http //noiseprotocol org/ authentication and access ditto's security relies on digital identities for peer to peer authentication and access control permissions offline sync prerequisite before participating in a mesh and exchanging data with nearby peers, each instance of ditto running in your app must connect to the internet at least once to establish the necessary infrastructure within the big peer, including generating the access credentials required to participate in the mesh and sync offline peer identity each instance of ditto running in your app is assigned a random universally unique identifier (uuid) this string of alphanumeric characters uniquely identifies your app across the ditto platform and other integrated systems and environments, including change data capture (cdc) in apache kafka, third party databases, business intelligence tools like microsoft bi, and so on establish necessary infrastructure within the big peer, for instance, the big peer uses your app id to identify your app before issuing your access credentials onlineplayground authentication using ditto's onlineplayground identity, you can sync with other peers who share the same app id and read and write to collections without the hassle of setting up your own authentication conflict free sync a challenge arises in offline scenarios when two or more peers make edits independently and the data values stored by each peer diverge over time referred to as conflict resolution , ditto's process of addressing concurrency conflicts involves a combination of a metadata database and guiding principles, as follows metadata database each peer individually maintains its own metadata database the metadata database serves as a storage repository of information essential in resolving conflicts that arise when merging concurrent changes within this metadata database is the version vector the version vector manages the following essential details for a particular peer the current state of sync a record of the data previously sent and received the sequence of updates the timestamp of last write operation guiding principles ditto adheres to the following strategies to ensure that all peers ultimately reach the same value for a particular data item deterministic — as part of the strategy of eventual consistency, regardless of the order in which updates from different peers are received and merged, all peers ultimately converge to the same single value predictable and meaningful — instead of arbitrarily resolving conflicting registers to a predefined value, the resulting merge accurately represents the original input and some rational interpretation of that input query subscription system the multiple replicas that result from a single partition are spread across virtual and relevant physical server nodes; that is, the big peer cloud deployment and local ditto stores of subscribing small peers by default, ditto does not automatically replicate data to peers instead, peers subscribe to changes and, using a query that you've defined, indicate the data they're interested in watching for changes once a change is observed somewhere in the mesh, only the delta matching your query replicates to other subscribing peers in simpler terms, replication involves a subscribing peer selectively "pulling" data from other peers, rather than remote peers automatically "pushing" data to it p artitioning and replication are central to ensuring rapid and dependable data access for your end users with ditto's query subscription system, d evices are able to sync both ditto document and attachment objects efficiently and intelligently for a direct explanation of the query subscription system that controls ditto's data sync service, watch the following video on the functionality of subscriptions in this video, the tech lead of ditto's replication team discusses the synchronization process between two peers — one with an empty document set and the other with seven billion documents configurations the ditto sdk features a configuration structure called transportconfig , which is stored within the main ditto object by default, if you don't make any changes to this configuration, ditto will automatically enable all available peer to peer transports if you create a new transportconfig, it has three main sections peertopeer connect listen most apps will only need to configure the peertopeer and websocket settings the listen parameters provide support for unusual scenarios such as web browser clients on fully offline networks please use these only as directed by ditto if misconfigured, the listener features could circumvent ditto’s usual encryption and access control peer discovery https //docs ditto live/ios/common/how it works/mesh network#discovering peers when ditto starts syncing it advertises itself on all enabled peer to peer transports this advertisement is transmitted in different ways depending on the transport but it always involves two small pieces of information app id hash announce all devices running the same app will have the same app id hash so they know they should connect in the rare event of a hash collision, ditto may try to connect to a different app, but verification will fail and the connection will be canceled immediately if you are monitoring network traffic you might come across a ditto announce this is a ditto specific advertisement format that contains basic information about the device this provides hints to ditto so that it can construct a good mesh quickly and efficiently q1racgxmbwogjgsgp771 q1 major protocol version 1 ra platform android cgxmbwog 32 bit network id jgsgp771 device name “sgp771” the network id is randomized each time ditto starts up it enables peers to identify each other with high probability across all transports if peer a connects to peer b over wi fi they know each other’s network ids, so peer b can avoid creating a duplicate connection in the reverse direction