<divclass="post__content"><p>A big problem when doing end-to-end data sync between mobile nodes is that most devices are offline most of the time. With a naive approach, you quickly run into issues of ‘ping-pong’ behavior, where messages have to be constantly retransmitted. We saw some basic calculations of what this bandwidth multiplier looks like in a <ahref="https://vac.dev/p2p-data-sync-for-mobile">previous post</a>.</p>
<p>While you could do some background processing, this is really battery-draining, and on iOS these capabilities are limited. A better approach instead is to loosen the constraint that two nodes need to be online at the same time. How do we do this? There are two main approaches, one is the <em>store and forward model</em>, and the other is a <em>remote log</em>.</p>
<p>In the <em>store and forward</em> model, we use an intermediate node that forward messages on behalf of the recipient. In the <em>remote log</em> model, you instead replicate the data onto some decentralized storage, and have a mutable reference to the latest state, similar to DNS. While both work, the latter is somewhat more elegant and “pure”, as it has less strict requirements of an individual node’s uptime. Both act as a highly-available cache to smoothen over non-overlapping connection windows between endpoints.</p>
<p>In this post we are going to describe how such a remote log schema could work. Specifically, how it enhances p2p data sync and takes care of the <ahref="https://vac.dev/p2p-data-sync-for-mobile">following requirements</a>:</p>
<blockquote>
<ol>
<li>MUST allow for mobile-friendly usage. By mobile-friendly we mean devices
that are resource restricted, mostly-offline and often changing network.</li>
</ol>
</blockquote>
<blockquote>
<ol>
<li>MAY use helper services in order to be more mobile-friendly. Examples of
helper services are decentralized file storage solutions such as IPFS and
Swarm. These help with availability and latency of data for mostly-offline
devices.</li>
</ol>
</blockquote>
<h2id="remote-log">Remote log</h2>
<p>A remote log is a replication of a local log. This means a node can read data from a node that is offline.</p>
<p>The spec is in an early draft stage and can be found <ahref="https://github.com/vacp2p/specs/pull/16">here</a>. A very basic <ahref="https://en.wikipedia.org/wiki/Spike_(software_development)">spike</a> / proof-of-concept can be found <ahref="https://github.com/vacp2p/research/tree/master/remote_log">here</a>.</p>
<h3id="definitions">Definitions</h3>
<table>
<thead>
<tr>
<th>Term</th>
<th>Definition</th>
</tr>
</thead>
<tbody>
<tr>
<td>CAS</td>
<td>Content-addressed storage. Stores data that can be addressed by its hash.</td>
</tr>
<tr>
<td>NS</td>
<td>Name system. Associates mutable data to a name.</td>
</tr>
<tr>
<td>Remote log</td>
<td>Replication of a local log at a different location.</td>
</tr>
</tbody>
</table>
<h3id="roles">Roles</h3>
<p>There are four fundamental roles:</p>
<ol>
<li>Alice</li>
<li>Bob</li>
<li>Name system (NS)</li>
<li>Content-addressed storage (CAS)</li>
</ol>
<p>The <em>remote log</em> is the data format of what is stored in the name system.</p>
<p>“Bob” can represent anything from 0 to N participants. Unlike Alice, Bob only needs read-only access to NS and CAS.</p>
<h3id="flow">Flow</h3>
<!-- diagram -->
<palign="center">
<imgsrc="/assets/img/remote-log.png"/>
<br/>
Figure 1: Remote log data synchronization.
</p>
<h3id="data-format">Data format</h3>
<p>The remote log lets receiving nodes know what data they are missing. Depending on the specific requirements and capabilities of the nodes and name system, the information can be referred to differently. We distinguish between three rough modes:</p>
<ol>
<li>Fully replicated log</li>
<li>Normal sized page with CAS mapping</li>
<li>“Linked list” mode - minimally sized page with CAS mapping</li>
</ol>
<p>A remote log is simply a mapping from message identifiers to their corresponding address in a CAS:</p>
<table>
<thead>
<tr>
<th>Message Identifier (H1)</th>
<th>CAS Hash (H2)</th>
</tr>
</thead>
<tbody>
<tr>
<td>H1_3</td>
<td>H2_3</td>
</tr>
<tr>
<td>H1_2</td>
<td>H2_2</td>
</tr>
<tr>
<td>H1_1</td>
<td>H2_1</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td><em>address to next page</em></td>
<td></td>
</tr>
</tbody>
</table>
<p>The numbers here corresponds to messages. Optionally, the content itself can be included, just like it normally would be sent over the wire. This bypasses the need for a dedicated CAS and additional round-trips, with a trade-off in bandwidth usage.</p>
<p>Both patterns can be used in parallel, e,g. by storing the last <codeclass="highlighter-rouge">k</code> messages directly and use CAS pointers for the rest. Together with the <codeclass="highlighter-rouge">next_page</code> page semantics, this gives users flexibility in terms of bandwidth and latency/indirection, all the way from a simple linked list to a fully replicated log. The latter is useful for things like backups on durable storage.</p>
<p><ahref="https://rfc.vac.dev/spec/2/#payloads">vac.mvds.Message</a> payloads are the only payloads that MUST be uploaded. Other messages types MAY be uploaded, depending on the implementation.</p>
<p>The spec is still in an early draft stage, so it is expected to change. Same with the proof of concept. More work is needed on getting a fully featured proof of concept with specific CAS and NAS instances. E.g. Swarm and Swarm Feeds, or IPFS and IPNS, or something else.</p>
<p>For data sync in general:</p>
<ul>
<li>Make consistency guarantees more explicit for app developers with support for sequence numbers and DAGs, as well as the ability to send non-synced messages. E.g. ephemeral typing notifications, linear/sequential history and casual consistency/DAG history</li>
<li>Better semantics and scalability for multi-user sync contexts, e.g. CRDTs and joining multiple logs together</li>
<li>Better usability in terms of application layer usage (data sync clients) and supporting more transports</li>
</ul>
<hr/>
<p>PS1. Thanks everyone who submitted great <ahref="https://explorer.bounties.network/bounty/3389">logo proposals</a> for Vac!</p>
<p>PPS2. Next week on October 10th decanus and I will be presenting Vac at <ahref="https://devcon.org/agenda">Devcon</a>, come say hi :)</p>