A place to cache linked articles (think custom and personal wayback machine)
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

7 місяці тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. title: A comparison of JavaScript CRDTs
  2. url: https://blog.notmyidea.org/a-comparison-of-javascript-crdts.html
  3. hash_url: 2c0b2588dfcd3a194da4133c7505cd3e
  4. archive_date: 2024-03-25
  5. og_image: https://blog.notmyidea.org/images/umap/crdt-converge.png
  6. description:
  7. favicon: https://blog.notmyidea.org/favicon-32x32.png
  8. language: fr_FR
  9. <p>Collaboration is one of the most requested features on <a href="https://umap-project.org">uMap</a>.
  10. I’ve talked <a href="https://blog.notmyidea.org/tag/umap.html">in previous articles</a> how
  11. we could add real-time features “the simple way”, by:</p>
  12. <ul>
  13. <li>a) catching when changes are done on the interface ;</li>
  14. <li>b) sending messages to the other parties and ;</li>
  15. <li>c) applying the changes on the receiving client.</li>
  16. </ul>
  17. <p>This works well in general, but it doesn’t take care of conflicts handling, especially when a disconnect can happen.</p>
  18. <p>For this reason, I got more into “Conflict-free Resolution Data Types” (CRDTs), with the goal of understanding what they are, how they work, what are the different libraries out there, and which one would be a good fit for us, if any.</p>
  19. <p>As things are changing quickly in this field, note that this article was written in March 2024.</p>
  20. <hr>
  21. <div class="toc">
  22. <ul>
  23. <li><a href="#part-1-what-are-crdts">Part 1 - What are CRDTs?</a><ul>
  24. <li><a href="#why-using-crdts">Why using&nbsp;CRDTs?</a></li>
  25. <li><a href="#traditional-data-synchronization-methods">Traditional data synchronization&nbsp;methods</a></li>
  26. <li><a href="#solving-complex-cases">Solving complex&nbsp;cases</a></li>
  27. <li><a href="#last-write-wins-registers">Last Write Wins&nbsp;Registers</a></li>
  28. <li><a href="#state-based-vs-operation-based">State-based vs Operation&nbsp;based</a></li>
  29. <li><a href="#how-the-server-fits-in-the-picture">How the server fits in the&nbsp;picture</a></li>
  30. <li><a href="#how-offline-is-handled">How offline is&nbsp;handled</a></li>
  31. </ul>
  32. </li>
  33. <li><a href="#part-2-javascript-crdts">Part 2: JavaScript CRDTs</a><ul>
  34. <li><a href="#the-demo-application">The demo&nbsp;application</a></li>
  35. <li><a href="#yjs">Y.js</a></li>
  36. <li><a href="#automerge">Automerge</a></li>
  37. <li><a href="#json-joy"><span class="caps">JSON</span>&nbsp;Joy</a></li>
  38. </ul>
  39. </li>
  40. <li><a href="#part-3-comparison-table">Part 3: Comparison table</a><ul>
  41. <li><a href="#working-with-patches">Working with&nbsp;patches</a></li>
  42. <li><a href="#conclusion">Conclusion</a></li>
  43. </ul>
  44. </li>
  45. <li><a href="#extra-notes">Extra notes</a><ul>
  46. <li><a href="#yata-and-rga"><span class="caps">YATA</span> and <span class="caps">RGA</span></a></li>
  47. <li><a href="#resources">Resources</a></li>
  48. </ul>
  49. </li>
  50. </ul>
  51. </div>
  52. <hr>
  53. <h2 id="part-1-what-are-crdts">Part 1 - What are CRDTs?</h2>
  54. <p>Conflict-free Resolution Data Types are a family of data types able to merge their states with other states without generating conflicts. They handle consistency in distributed systems, making them particularly well-suited for collaborative real-time applications.</p>
  55. <p>CRDTs ensure that multiple participants can make changes without strict coordination, and all replicas converge to the same state upon synchronization, without conflicts.</p>
  56. <p><span class="dquo">“</span>Append-only sets” are probably one of the most common type of <span class="caps">CRDT</span>: you can add the same element again and again, it will only be present once in the set. It’s our old friend <code>Set</code>, as we can find in many programming languages.</p>
  57. <h3 id="why-using-crdts">Why using CRDTs?</h3>
  58. <p>For uMap, CRDTs offer a solution to several challenges:</p>
  59. <ol>
  60. <li>
  61. <p><strong>Simultaneous Editing</strong>: When multiple users interact with the same map, their changes must not only be reflected in real-time but also merged seamlessly without overwriting each other’s contributions. We need all the replicas to converge to the same state.</p>
  62. </li>
  63. <li>
  64. <p><strong>Network Latency and Partition</strong>: uMap operates over networks that can experience delays or temporary outages (think editing on the ground). CRDTs can handle these conditions gracefully, enabling offline editing and eventual consistency.</p>
  65. </li>
  66. <li>
  67. <p><strong>Simplified Conflict Resolution</strong>: Traditional methods often require complex algorithms to resolve conflicts, while CRDTs inherently minimize the occurrence of conflicts altogether.</p>
  68. </li>
  69. <li>
  70. <p><strong>Server load</strong>: uMap currently relies on central servers (one per instance). Adopting CRDTs could help lower the work done on the server, increasing resilience and scalability.</p>
  71. </li>
  72. </ol>
  73. <h3 id="traditional-data-synchronization-methods">Traditional data synchronization methods</h3>
  74. <p>Traditional data synchronization methods typically rely on a central source of truth (the server) to manage and resolve conflicts. When changes are made by different users, these traditional systems require a round-trip to the server for conflict resolution and thus can be slow or inadequate for real-time collaboration.</p>
  75. <p>In contrast, CRDTs leverage mathematical properties (the fact that the data types can converge) to ensure that every replica independently reaches the same state, without the need for a central authority, thus minimizing the amount of coordination and communication needed between nodes.</p>
  76. <p>This ability to maintain consistency sets CRDTs apart from conventional synchronization approaches and makes them particularly valuable for the development of collaborative tools like uMap, where real-time updates and reliability are important.</p>
  77. <h3 id="solving-complex-cases">Solving complex cases</h3>
  78. <p>At first, I found CRDTs somewhat confusing, owing to their role in addressing complex challenges. CRDTs come in various forms, with much of their intricacy tied to resolving content conflicts within textual data or elaborate hierarchical structures.</p>
  79. <p>Fortunately for us, our use case is comparatively straightforward, and we probably only need <span class="caps">LWW</span> registers.</p>
  80. <h3 id="last-write-wins-registers">Last Write Wins Registers</h3>
  81. <p>As you might have guessed from the name, a <span class="caps">LWW</span> register is a specific type of <span class="caps">CRDT</span> which “just” replaces the value with the last write. The main concern is establishing the sequence of updates, to order them together (who is the last one?).</p>
  82. <p>In a single-client scenario or with a central time reference, sequencing is straightforward. However, in a distributed environment, time discrepancies across peers can complicate things, as clocks may drift and lose synchronization.</p>
  83. <p>To address this, CRDTs use vector clocks — a specialized data structure that helps to solve the relative timing of events across distributed systems and pinpoint any inconsistencies.</p>
  84. <blockquote>
  85. <p>A vector clock is a data structure used for determining the partial ordering of events in a distributed system and detecting causality violations.</p>
  86. <p>– <a href="https://en.wikipedia.org/wiki/Vector_clock">Wikipedia</a></p>
  87. </blockquote>
  88. <p>Note that we could also use a library such as <a href="https://github.com/pubkey/rxdb">rxdb</a> — to handle the syncing, offline, etc. — because we have a master: we use the server, and we can use it to handle the merge conflicts.
  89. But by doing so, we would give more responsibility to the server, whereas when using CRDTs it’s possible to do the merge only on the clients (enabling no-master replications).</p>
  90. <h3 id="state-based-vs-operation-based">State-based vs Operation based</h3>
  91. <p>While reading the literature, I found that there are two kinds of CRDTs: state-based and operation-based. It turns out most of the CRDTs implementation I looked at are operation-based, and propose an <span class="caps">API</span> to interact with them as you’re changing the state, so <strong>it doesn’t really matter</strong> in practice.</p>
  92. <blockquote>
  93. <p>The two alternatives are theoretically equivalent, as each can emulate the
  94. other. However, there are practical differences. State-based CRDTs are
  95. often simpler to design and to implement; their only requirement from the
  96. communication substrate is some kind of gossip protocol. <strong>Their drawback is that
  97. the entire state of every <span class="caps">CRDT</span> must be transmitted eventually to every other
  98. replica, which may be costly</strong>. In contrast, operation-based CRDTs transmit only
  99. the update operations, which are typically small.</p>
  100. <p><a href="https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type">Wikipedia on CRDTs</a></p>
  101. </blockquote>
  102. <h3 id="how-the-server-fits-in-the-picture">How the server fits in the picture</h3>
  103. <p>While discussing with the automerge team, I understood that I was expecting the server to pass along the messages to the other parties, and that would be the way the synchronization would be done. It turns out I was mistaken: in this approach, the clients send updates to the server, which merges everything together and only then sends the updates to the other peers. It makes it easy for the server to send back the needed information to the clients (for new peers, or if the peers didn’t cache the data locally).</p>
  104. <p>In order to have peers working with each other, I would need to change the way the provider works, so we can have the server be “brainless” and just relay the messages.</p>
  105. <p>For automerge, it would mean the provider will “just” handle the websocket connection (disconnect and reconnect) and all the peers would be able to talk with each other. The other solution for us would be to have the merge algorithm working on the server side, which comes with upsides (no need to find when the document should be saved by the client to the server) and downsides (it takes some cpu and memory to run the CRDTs on the server)</p>
  106. <h3 id="how-offline-is-handled">How offline is handled</h3>
  107. <p>I was curious about how offline editing might work, and what would happen when going back online. Changes can happen both online and offline, making no difference for the “reconciliation” step. When going back online, a “patch” is computed by the newly reconnected peer, and sent to the other peers.</p>
  108. <hr>
  109. <h2 id="part-2-javascript-crdts">Part 2: JavaScript CRDTs</h2>
  110. <p>Now that we’re familiar with CRDTs and how they can help us, let’s create a map application which syncs marker positions, on different browsers.</p>
  111. <p>We’ll be comparing three JavaScript libraries: <a href="https://yjs.dev/">Y.js</a>, <a href="https://automerge.org/">Automerge</a> and <a href="https://jsonjoy.com"><span class="caps">JSON</span> Joy</a>, considering:</p>
  112. <ol>
  113. <li><strong>Their external <span class="caps">API</span></strong>: is it easy to use in our case? What are the challenging parts?</li>
  114. <li><strong>Community and Support</strong>: What is the size and activity of the developer community / ecosystem?</li>
  115. <li><strong>Size of the <span class="caps">JS</span> library</strong>, because we want to limit the impact on our users browsers.</li>
  116. <li><strong>Efficiency</strong>: Probe the bandwidth when doing edits. What’s being transmitted over the wire? </li>
  117. </ol>
  118. <p>I setup a demo application for each of the libraries. Everything is available <a href="https://gitlab.com/umap-project/leaflet-sync">in a git repository</a> if you want to try it out yourself.</p>
  119. <h3 id="the-demo-application">The demo application</h3>
  120. <p>All the demos are made against the same set of features. It</p>
  121. <ul>
  122. <li>Creates a marker when the map is clicked</li>
  123. <li>Moves the markers on hover.</li>
  124. </ul>
  125. <p>This should probably be enough for us to try out.</p>
  126. <p>Here’s the whole code for this, using <a href="https://leafletjs.com/">Leaflet - a JavaScript library for interactive maps</a>. </p>
  127. <div class="highlight"><pre><span></span><code><span class="k">import</span><span class="w"> </span><span class="nx">L</span><span class="w"> </span><span class="kr">from</span><span class="w"> </span><span class="s2">"leaflet"</span><span class="p">;</span>
  128. <span class="k">import</span><span class="w"> </span><span class="s2">"leaflet/dist/leaflet.css"</span><span class="p">;</span>
  129. <span class="c1">// Create a map with a default tilelayer.</span>
  130. <span class="kd">const</span><span class="w"> </span><span class="nx">map</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">L</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="s2">"map"</span><span class="p">).</span><span class="nx">setView</span><span class="p">([</span><span class="mf">48.1173</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">1.6778</span><span class="p">],</span><span class="w"> </span><span class="mf">13</span><span class="p">);</span>
  131. <span class="nx">L</span><span class="p">.</span><span class="nx">tileLayer</span><span class="p">(</span><span class="s2">"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"</span><span class="p">,</span><span class="w"> </span><span class="p">{</span>
  132. <span class="w"> </span><span class="nx">maxZoom</span><span class="o">:</span><span class="w"> </span><span class="mf">19</span><span class="p">,</span>
  133. <span class="w"> </span><span class="nx">attribution</span><span class="o">:</span><span class="w"> </span><span class="s2">"© OpenStreetMap contributors"</span><span class="p">,</span>
  134. <span class="p">}).</span><span class="nx">addTo</span><span class="p">(</span><span class="nx">map</span><span class="p">);</span>
  135. <span class="c1">// Features contains a reference to the marker objects, mapped by the uuids</span>
  136. <span class="kd">let</span><span class="w"> </span><span class="nx">features</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{};</span>
  137. <span class="c1">// An upsert function, creating a marker at the passed latlng.</span>
  138. <span class="c1">// If an uuid is provided, it changes the coordinates at the given address</span>
  139. <span class="kd">function</span><span class="w"> </span><span class="nx">upsertMarker</span><span class="p">({</span><span class="w"> </span><span class="nx">latlng</span><span class="p">,</span><span class="w"> </span><span class="nx">uuid</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="p">{</span>
  140. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="nx">uuid</span><span class="p">)</span><span class="w"> </span><span class="nx">uuid</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">crypto</span><span class="p">.</span><span class="nx">randomUUID</span><span class="p">();</span>
  141. <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">marker</span><span class="p">;</span><span class="w"> </span>
  142. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">features</span><span class="p">).</span><span class="nx">includes</span><span class="p">(</span><span class="nx">uuid</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
  143. <span class="w"> </span><span class="nx">marker</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">features</span><span class="p">[</span><span class="nx">uuid</span><span class="p">];</span>
  144. <span class="w"> </span><span class="nx">marker</span><span class="p">.</span><span class="nx">setLatLng</span><span class="p">(</span><span class="nx">latlng</span><span class="p">);</span>
  145. <span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
  146. <span class="w"> </span><span class="nx">marker</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">L</span><span class="p">.</span><span class="nx">marker</span><span class="p">(</span><span class="nx">latlng</span><span class="p">,</span><span class="w"> </span><span class="p">{</span>
  147. <span class="w"> </span><span class="nx">draggable</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span>
  148. <span class="w"> </span><span class="nx">autoPan</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span>
  149. <span class="w"> </span><span class="nx">uuid</span><span class="o">:</span><span class="w"> </span><span class="nx">uuid</span><span class="p">,</span>
  150. <span class="w"> </span><span class="p">});</span>
  151. <span class="w"> </span><span class="nx">features</span><span class="p">[</span><span class="nx">uuid</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">marker</span><span class="p">;</span>
  152. <span class="w"> </span><span class="nx">marker</span><span class="p">.</span><span class="nx">addTo</span><span class="p">(</span><span class="nx">map</span><span class="p">);</span>
  153. <span class="w"> </span><span class="nx">marker</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">"dragend"</span><span class="p">,</span><span class="w"> </span><span class="p">({</span><span class="w"> </span><span class="nx">target</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">{</span>
  154. <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"dragend"</span><span class="p">);</span>
  155. <span class="w"> </span><span class="p">});</span>
  156. <span class="w"> </span><span class="p">}</span>
  157. <span class="p">}</span>
  158. <span class="c1">// Add new features to the map with a click</span>
  159. <span class="nx">map</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">"click"</span><span class="p">,</span><span class="w"> </span><span class="p">({</span><span class="w"> </span><span class="nx">latlng</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">{</span>
  160. <span class="w"> </span><span class="nx">upsertMarker</span><span class="p">({</span><span class="w"> </span><span class="nx">latlng</span><span class="w"> </span><span class="p">});</span>
  161. <span class="p">});</span>
  162. </code></pre></div>
  163. <p>It does the following:</p>
  164. <ul>
  165. <li>Creates a map zoomed on Rennes, France</li>
  166. <li>Maintains a <code>features</code> object, referencing the added markers</li>
  167. <li>Provides a <code>upsertMarker</code> function, creating a marker on the map at the given latitude and longitude, and updating its latitude and longitude if it already exists.</li>
  168. <li>It listens to the <code>click</code> event on the map, calling <code>upsertMarker</code> with the appropriate arguments.</li>
  169. </ul>
  170. <p>Note that the data is not “reactive” (in the sense of React apps): there is no central state that’s updated and triggers the rendering of the user interface.</p>
  171. <h3 id="yjs">Y.js</h3>
  172. <p>Y.js is the first library I’ve looked at, probably because it’s the oldest one, and the more commonly referred to.</p>
  173. <p>The <span class="caps">API</span> seem to offer what we look for, and provides a way to <a href="https://docs.yjs.dev/api/shared-types/y.map#observing-changes-y.mapevent">observe changes</a>. Here’s how it works:</p>
  174. <div class="highlight"><pre><span></span><code><span class="k">import</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="kr">as</span><span class="w"> </span><span class="nx">Y</span><span class="w"> </span><span class="kr">from</span><span class="w"> </span><span class="s2">"yjs"</span><span class="p">;</span>
  175. <span class="k">import</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">WebsocketProvider</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="kr">from</span><span class="w"> </span><span class="s2">"y-websocket"</span><span class="p">;</span>
  176. <span class="c1">// Instanciate a document</span>
  177. <span class="kd">const</span><span class="w"> </span><span class="nx">doc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Y</span><span class="p">.</span><span class="nx">Doc</span><span class="p">();</span>
  178. </code></pre></div>
  179. <p>When we add a new marker, we update the <span class="caps">CRDT</span> (<code>markers.set</code>).</p>
  180. <div class="highlight"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="nx">markers</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">doc</span><span class="p">.</span><span class="nx">getMap</span><span class="p">(</span><span class="s2">"markers"</span><span class="p">);</span>
  181. <span class="nx">markers</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">target</span><span class="p">.</span><span class="nx">options</span><span class="p">.</span><span class="nx">uuid</span><span class="p">,</span><span class="w"> </span><span class="nx">target</span><span class="p">.</span><span class="nx">_latlng</span><span class="p">);</span>
  182. </code></pre></div>
  183. <p>Another connected peer can observe the changes, like this:</p>
  184. <div class="highlight"><pre><span></span><code><span class="nx">markers</span><span class="p">.</span><span class="nx">observe</span><span class="p">((</span><span class="nx">event</span><span class="p">,</span><span class="w"> </span><span class="nx">transaction</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">{</span>
  185. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="nx">transaction</span><span class="p">.</span><span class="nx">local</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
  186. <span class="w"> </span><span class="nx">event</span><span class="p">.</span><span class="nx">changes</span><span class="p">.</span><span class="nx">keys</span><span class="p">.</span><span class="nx">forEach</span><span class="p">((</span><span class="nx">change</span><span class="p">,</span><span class="w"> </span><span class="nx">key</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">{</span>
  187. <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">markers</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">key</span><span class="p">);</span>
  188. <span class="w"> </span><span class="k">switch</span><span class="p">(</span><span class="nx">change</span><span class="p">.</span><span class="nx">action</span><span class="p">){</span>
  189. <span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="s1">'add'</span><span class="o">:</span>
  190. <span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="s1">'update'</span><span class="o">:</span>
  191. <span class="w"> </span><span class="nx">upsertMarker</span><span class="p">({</span><span class="w"> </span><span class="nx">latlng</span><span class="o">:</span><span class="w"> </span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">uuid</span><span class="o">:</span><span class="w"> </span><span class="nx">key</span><span class="p">,</span><span class="w"> </span><span class="nx">local</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">});</span>
  192. <span class="w"> </span><span class="k">break</span><span class="p">;</span>
  193. <span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="s1">'delete'</span><span class="o">:</span>
  194. <span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="sb">`Property "</span><span class="si">${</span><span class="nx">key</span><span class="si">}</span><span class="sb">" was deleted. ".`</span><span class="p">);</span>
  195. <span class="w"> </span><span class="p">}</span>
  196. <span class="w"> </span><span class="p">});</span>
  197. <span class="w"> </span><span class="p">}</span>
  198. <span class="p">});</span>
  199. </code></pre></div>
  200. <p>We cycle on the received changes, and then apply them on our map. In the case of an offline peer coming back online after some time, the <code>observe</code> event will be called only once. </p>
  201. <p>I’m not dealing with the case of marker deletions here, but deleted items are also taken into account. The data isn’t really deleted in this case, but a “tombstone” is used, making it possible to resolve conflicts (for instance, if one people deleted a marker and some other updated it during the same time).</p>
  202. <p>Y.js comes with multiple “connection providers”, which make it possible to sync with different protocols (there is even <a href="https://github.com/yousefED/matrix-crdt">a way to sync over the matrix protocol</a> 😇).</p>
  203. <p>More usefully for us, there is <a href="https://github.com/yjs/y-websocket">an implemented protocol for WebSockets</a>. Here is how to use one of these providers:</p>
  204. <div class="highlight"><pre><span></span><code><span class="c1">// Sync clients with the y-websocket provider</span>
  205. <span class="kd">const</span><span class="w"> </span><span class="nx">provider</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">WebsocketProvider</span><span class="p">(</span>
  206. <span class="w"> </span><span class="s2">"ws://localhost:1234"</span><span class="p">,</span>
  207. <span class="w"> </span><span class="s2">"leaflet-sync"</span><span class="p">,</span>
  208. <span class="w"> </span><span class="nx">doc</span>
  209. <span class="p">);</span>
  210. </code></pre></div>
  211. <p>This code setups a WebSocket connection with a server that will maintain a local copy of the <span class="caps">CRDT</span>, as explained above.</p>
  212. <p>It’s also possible to send “awareness” information (some state you don’t want to persist, like the position of the cursor). It contains some useful meta information, such as the number of connected peers, if they’re connected, etc.</p>
  213. <div class="highlight"><pre><span></span><code><span class="nx">map</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">"mousemove"</span><span class="p">,</span><span class="w"> </span><span class="p">({</span><span class="w"> </span><span class="nx">latlng</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">{</span>
  214. <span class="w"> </span><span class="nx">awareness</span><span class="p">.</span><span class="nx">setLocalStateField</span><span class="p">(</span><span class="s2">"user"</span><span class="p">,</span><span class="w"> </span><span class="p">{</span>
  215. <span class="w"> </span><span class="nx">cursor</span><span class="o">:</span><span class="w"> </span><span class="nx">latlng</span><span class="p">,</span>
  216. <span class="w"> </span><span class="p">});</span>
  217. <span class="p">});</span><span class="w"> </span>
  218. </code></pre></div>
  219. <p>I made <a href="https://gitlab.com/umap-project/leaflet-sync/-/tree/yjs">a quick proof of concept with Y.js</a> in a few hours flawlessly. It handles offline and reconnects, and exposes awareness information.</p>
  220. <h4 id="python-bindings">Python bindings</h4>
  221. <p>Y.js has been ported to rust with <a href="https://github.com/y-crdt/y-crdt">the Y.rs project</a>, making it possible to have binding in other languages, like ruby and python (see <a href="https://github.com/y-crdt/ypy">Y.py</a>). The python bindings are currently looking for a maintainer.</p>
  222. <h4 id="library-size">Library size</h4>
  223. <ul>
  224. <li>Y.js is 4,16 Ko</li>
  225. <li>Y-Websocket is 21,14 Ko</li>
  226. </ul>
  227. <h4 id="the-data-being-transmitted">The data being transmitted</h4>
  228. <p>In a scenario where all clients connect to a central server, which handles the <span class="caps">CRDT</span> locally and then transmits back to other parties, adding 20 points on one client, then 20 points in another generates ~5 Ko of data (so approximately 16 bytes per edit).</p>
  229. <p>Pros: </p>
  230. <ul>
  231. <li>The <span class="caps">API</span> was feeling natural to me: it handles plain old JavaScript objects, making it easy to integrate.</li>
  232. <li>It seems to be widely used, and the community seems active.</li>
  233. <li>It is <a href="https://docs.yjs.dev/">well documented</a></li>
  234. <li>There is awareness support</li>
  235. </ul>
  236. <p>Cons:</p>
  237. <ul>
  238. <li>It doesn’t seem to work well <a href="https://github.com/yjs/yjs/issues/325">without a <span class="caps">JS</span> bundler</a> which could be a problem for us.</li>
  239. <li>The Websocket connection provider doesn’t do what I was expecting it to, as it requires the server to run a <span class="caps">CRDT</span> locally.</li>
  240. </ul>
  241. <hr>
  242. <h3 id="automerge">Automerge</h3>
  243. <p><a href="https://automerge.org/">Automerge</a> is another <span class="caps">CRDT</span> library started by the folks at <a href="https://www.inkandswitch.com/">Ink <span class="amp">&amp;</span> Switch</a> with Martin Kleppmann. Automerge is actually the low level interface. There is a higher-level interface named <a href="https://automerge.org/docs/repositories/">Automerge-repo</a>. Here is how to use it:</p>
  244. <div class="highlight"><pre><span></span><code><span class="k">import</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">Repo</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="kr">from</span><span class="w"> </span><span class="s2">"@automerge/automerge-repo"</span><span class="p">;</span>
  245. <span class="kd">const</span><span class="w"> </span><span class="nx">repo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Repo</span><span class="p">();</span>
  246. <span class="kd">let</span><span class="w"> </span><span class="nx">handle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">repo</span><span class="p">.</span><span class="nx">create</span><span class="p">()</span>
  247. <span class="c1">// or repo.find(name)</span>
  248. </code></pre></div>
  249. <p>To change the document, call <code>handle.change</code> and pass it a function that will make changes to the document.</p>
  250. <p>Here, when a new marker is added:</p>
  251. <div class="highlight"><pre><span></span><code><span class="nx">handle</span><span class="p">.</span><span class="nx">change</span><span class="p">((</span><span class="nx">doc</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">{</span>
  252. <span class="w"> </span><span class="nx">doc</span><span class="p">[</span><span class="nx">uuid</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">cleanLatLng</span><span class="p">(</span><span class="nx">target</span><span class="p">.</span><span class="nx">_latlng</span><span class="p">);</span>
  253. <span class="p">});</span>
  254. </code></pre></div>
  255. <p>I had to use a <code>cleanLatLng</code> function in order to not pass the whole object, otherwise it wouldn’t serialize. It’s really just a simple helper taking the properties of interest for us (and letting away all the rest).</p>
  256. <p>Another peer can observe the changes, like this:</p>
  257. <div class="highlight"><pre><span></span><code><span class="nx">handle</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">"change"</span><span class="p">,</span><span class="w"> </span><span class="p">({</span><span class="w"> </span><span class="nx">doc</span><span class="p">,</span><span class="w"> </span><span class="nx">patches</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">{</span>
  258. <span class="w"> </span><span class="nx">patches</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(({</span><span class="w"> </span><span class="nx">action</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">{</span>
  259. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">path</span><span class="p">.</span><span class="nx">length</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="nx">action</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="s2">"insert"</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
  260. <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">doc</span><span class="p">[</span><span class="nx">path</span><span class="p">[</span><span class="mf">0</span><span class="p">]];</span>
  261. <span class="w"> </span><span class="nx">upsertMarker</span><span class="p">({</span><span class="w"> </span><span class="nx">latlng</span><span class="o">:</span><span class="w"> </span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">uuid</span><span class="o">:</span><span class="w"> </span><span class="nx">path</span><span class="p">[</span><span class="mf">0</span><span class="p">],</span><span class="w"> </span><span class="nx">local</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">});</span>
  262. <span class="w"> </span><span class="p">}</span>
  263. <span class="w"> </span><span class="p">});</span>
  264. <span class="p">});</span>
  265. </code></pre></div>
  266. <p>The “patch” interface is a bit less verbose than the one from Y.js. It wasn’t well documented, so I had to play around with the messages to understand exactly what the possible actions were. In the end, it gets me what I’m looking for, the changes that occurred to my data.</p>
  267. <p>Here I’m using <code>path.length == 2 &amp;&amp; action === 'insert'</code> to detect that a change occurred to the marker. Since we don’t make a difference between the creation of a marker and its update (when being moved), it works well for us.</p>
  268. <h4 id="python-bindings_1">Python bindings</h4>
  269. <p>There are <a href="https://github.com/automerge/automerge-py">python bindings</a> for automerge “core”. (It doesn’t — yet — provide ways to interact with “repos”).</p>
  270. <h4 id="library-size_1">Library size</h4>
  271. <ul>
  272. <li>Size of the automerge + automerge repo: 1,64 mb</li>
  273. <li>Size of the WebSocket provider: 0,10 mb </li>
  274. </ul>
  275. <p>This is quite a large bundle size, and the team behind automerge is aware of it and working on a solution.</p>
  276. <h4 id="the-data-being-transmitted_1">The data being transmitted</h4>
  277. <p>In the same scenario, adding 20 points on one client, then 20 points in another generates 90 messages and 24,94 Ko of data transmitted (~12 Ko sent and ~12Ko received), so approximately 75 bytes per edit.</p>
  278. <p>Pros:</p>
  279. <ul>
  280. <li>There is an <span class="caps">API</span> to <a href="https://automerge.org/docs/documents/conflicts/">get informed when a conflict occurred</a></li>
  281. <li>Python bindings are currently being worked on, soon to reach a stable version</li>
  282. <li>The team was overall very responsive and trying to help.</li>
  283. </ul>
  284. <p>Cons:</p>
  285. <ul>
  286. <li>The JavaScript is currently generated via Web Assembly, which could make it harder to debug.</li>
  287. <li>The large bundle size of the generated files.</li>
  288. </ul>
  289. <hr>
  290. <h3 id="json-joy"><span class="caps">JSON</span> Joy</h3>
  291. <p><a href="https://jsonjoy.com"><span class="caps">JSON</span> Joy</a> is the latest to the party.</p>
  292. <p>It takes another stake by providing multiple libraries with a small functional perimeter. It sounds promising, even if still quite new, and would leave us with the hands free in order to implement the protocol that would work for us.</p>
  293. <p>Here is how to use it. On the different peers you start with different forks of the same document:</p>
  294. <div class="highlight"><pre><span></span><code><span class="k">import</span><span class="w"> </span><span class="p">{</span><span class="nx">Model</span><span class="p">}</span><span class="w"> </span><span class="kr">from</span><span class="w"> </span><span class="s1">'json-joy/es2020/json-crdt'</span><span class="p">;</span>
  295. <span class="k">import</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">s</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="kr">from</span><span class="w"> </span><span class="s2">"json-joy/es6/json-crdt-patch"</span><span class="p">;</span>
  296. <span class="c1">// Initiate a model with a custom ID</span>
  297. <span class="kd">const</span><span class="w"> </span><span class="nx">rootModel</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Model</span><span class="p">.</span><span class="nx">withLogicalClock</span><span class="p">(</span><span class="mf">11111111</span><span class="p">);</span>
  298. <span class="c1">// populate it with default data</span>
  299. <span class="nx">rootModel</span><span class="p">.</span><span class="nx">api</span><span class="p">.</span><span class="nx">root</span><span class="p">({</span>
  300. <span class="w"> </span><span class="nx">markers</span><span class="o">:</span><span class="w"> </span><span class="p">{},</span>
  301. <span class="p">});</span>
  302. <span class="c1">// Fork it on each client</span>
  303. <span class="kd">let</span><span class="w"> </span><span class="nx">userModel</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">rootModel</span><span class="p">.</span><span class="nx">fork</span><span class="p">();</span>
  304. </code></pre></div>
  305. <p>When adding a new marker, we can define a new constant, by using <code>s.con</code>…</p>
  306. <div class="highlight"><pre><span></span><code><span class="nx">userModel</span><span class="p">.</span><span class="nx">api</span><span class="p">.</span><span class="nx">obj</span><span class="p">([</span><span class="s2">"markers"</span><span class="p">]).</span><span class="nx">set</span><span class="p">({</span>
  307. <span class="w"> </span><span class="p">[</span><span class="nx">uuid</span><span class="p">]</span><span class="o">:</span><span class="w"> </span><span class="nx">s</span><span class="p">.</span><span class="nx">con</span><span class="p">(</span><span class="nx">target</span><span class="p">.</span><span class="nx">_latlng</span><span class="p">),</span>
  308. <span class="p">});</span>
  309. </code></pre></div>
  310. <p>… and then create a patch and send it to the other peers:</p>
  311. <div class="highlight"><pre><span></span><code><span class="k">import</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">encode</span><span class="p">,</span><span class="w"> </span><span class="nx">decode</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="kr">from</span><span class="w"> </span><span class="s2">"json-joy/es6/json-crdt-patch/codec/verbose"</span><span class="p">;</span>
  312. <span class="kd">let</span><span class="w"> </span><span class="nx">patch</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">userModel</span><span class="p">.</span><span class="nx">api</span><span class="p">.</span><span class="nx">flush</span><span class="p">();</span>
  313. <span class="kd">let</span><span class="w"> </span><span class="nx">payload</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">encode</span><span class="p">(</span><span class="nx">patch</span><span class="p">);</span>
  314. </code></pre></div>
  315. <p>On the other peers, when we receive a patch message, decode it and apply it:</p>
  316. <div class="highlight"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="nx">patch</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">decode</span><span class="p">(</span><span class="nx">payload</span><span class="p">);</span>
  317. <span class="nx">model</span><span class="p">.</span><span class="nx">api</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">patch</span><span class="p">);</span>
  318. </code></pre></div>
  319. <p>We can observe the changes this way:</p>
  320. <div class="highlight"><pre><span></span><code><span class="nx">userModel</span><span class="p">.</span><span class="nx">api</span><span class="p">.</span><span class="nx">onPatch</span><span class="p">.</span><span class="nx">listen</span><span class="p">((</span><span class="nx">patch</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">{</span>
  321. <span class="w"> </span><span class="nx">patch</span><span class="p">.</span><span class="nx">ops</span><span class="p">.</span><span class="nx">forEach</span><span class="p">((</span><span class="nx">op</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">{</span>
  322. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">op</span><span class="p">.</span><span class="nx">name</span><span class="p">()</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="s2">"ins_obj"</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
  323. <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">op</span><span class="p">.</span><span class="nx">data</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="mf">0</span><span class="p">];</span>
  324. <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">userModel</span><span class="p">.</span><span class="nx">view</span><span class="p">().</span><span class="nx">markers</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span>
  325. <span class="w"> </span><span class="nx">upsertMarker</span><span class="p">({</span><span class="w"> </span><span class="nx">latlng</span><span class="o">:</span><span class="w"> </span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">uuid</span><span class="o">:</span><span class="w"> </span><span class="nx">key</span><span class="p">,</span><span class="w"> </span><span class="nx">local</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">});</span>
  326. <span class="w"> </span><span class="p">}</span>
  327. <span class="w"> </span><span class="p">});</span>
  328. <span class="p">});</span>
  329. </code></pre></div>
  330. <p>Similarly to what we did with automerge, we’re having a look into the patch, and filter on the operations of interest for us (<code>ins_obj</code>). The names of the operations aren’t clearly specified by the spec.</p>
  331. <p>Metrics:</p>
  332. <ul>
  333. <li>Size: 143 ko</li>
  334. <li>Data transmitted for 2 peers and 40 edits: (35 bytes per edit)</li>
  335. </ul>
  336. <p>Pros:</p>
  337. <ul>
  338. <li>Small atomic libraries, making it easy to use only the parts we need.</li>
  339. <li>The interface <a href="https://jsonjoy.com/libs/json-joy-js/json-crdt/guide/node-types">proposes to store different type of data</a> (constants, values, arrays, etc.)</li>
  340. <li>Distributed as different type of <span class="caps">JS</span> bundles (modules, wasm, etc.)</li>
  341. <li>Low level, so you know what you’re doing</li>
  342. </ul>
  343. <p>Cons:</p>
  344. <ul>
  345. <li>It doesn’t provide a high level interface for sync</li>
  346. <li>It’s currently a one-person project, without clear community channels to gather with other interested folks.</li>
  347. <li>Quite recent, so probably rough spots are to be found</li>
  348. </ul>
  349. <hr>
  350. <h2 id="part-3-comparison-table">Part 3: Comparison table</h2>
  351. <p>I found Y.js and Automerge quite similar for my use case, while <span class="caps">JSON</span> Joy was taking a different (less “all-included”) approach. Here is a summary table to help read the differences I found.</p>
  352. <table>
  353. <thead>
  354. <tr>
  355. <th></th>
  356. <th>Y.js</th>
  357. <th>Automerge</th>
  358. <th><span class="caps">JSON</span> Joy</th>
  359. </tr>
  360. </thead>
  361. <tbody>
  362. <tr>
  363. <td>Python bindings</td>
  364. <td><a href="https://github.com/y-crdt/ypy">Yes</a></td>
  365. <td><a href="https://github.com/automerge/automerge-py">Yes</a></td>
  366. <td>No</td>
  367. </tr>
  368. <tr>
  369. <td>Syncing</td>
  370. <td>Native <span class="caps">JS</span> structures</td>
  371. <td>Transactional functions</td>
  372. <td>Specific types (bonus points for handling constants)</td>
  373. </tr>
  374. <tr>
  375. <td>Coded in</td>
  376. <td>JavaScript / Rust</td>
  377. <td>TypeScript / Rust</td>
  378. <td>Typescript</td>
  379. </tr>
  380. <tr>
  381. <td>Awareness protocol</td>
  382. <td>Yes, with presence</td>
  383. <td>Yes, without presence</td>
  384. <td>No</td>
  385. </tr>
  386. <tr>
  387. <td>Conflict-detection <span class="caps">API</span></td>
  388. <td>No</td>
  389. <td>Yes</td>
  390. <td>No</td>
  391. </tr>
  392. <tr>
  393. <td>Library size</td>
  394. <td><strong>24.3Ko</strong> §<br></td>
  395. <td><strong>1,74 mb</strong> §</td>
  396. <td><strong>143 ko</strong></td>
  397. </tr>
  398. </tbody>
  399. </table>
  400. <p>§ size of the connectors included</p>
  401. <h3 id="working-with-patches">Working with patches</h3>
  402. <p>In order to observe the changes, we need to inspect the given patches and work on what we find. I found the different libraries expose different sets of APIs. All of these APIs were quite a bit hard to find, and it’s not clear if they are public or private.</p>
  403. <p>One thing to keep in mind is that these “patch” events happen only once per patch received. You can see it as a “diff” of the state between the current and the incoming states.</p>
  404. <ul>
  405. <li>Y.js exposes a utility which is able to tell you what the action on the key is (“delete”, “update” and “add”)</li>
  406. <li>Automerge and <span class="caps">JSON</span> Joy, on the other hand, don’t provide such utility functions, meaning you would need to find that out yourself.</li>
  407. </ul>
  408. <h3 id="conclusion">Conclusion</h3>
  409. <p>The goal here is not to tell which one of these libraries is the best one. They’re all great and have their strenghs. None of them implement the high-level <span class="caps">API</span> I was expecting, where the clients talk with each other and the server just relays messages, but maybe it’s because it’s better in general to have the server have the representation of the data, saving a roundtrip for the clients.</p>
  410. <p>I wasn’t expecting to have a look at patches to understand what changed at the low level. The way it’s currently implemented is very suitable for “reactive” applications, but require more involvement to sync between the CRDTs state and the application state.</p>
  411. <p>In the end, adding CRDTs is made very simple, probably due to the fact all we really need is a sort of distributed key/value store.</p>
  412. <p>I’m not sure yet which library we will end up using for uMap (if any), but my understanding is clearer than it was when I started. I guess that’s what progress looks like 😎</p>
  413. <h3 id="yata-and-rga"><span class="caps">YATA</span> and <span class="caps">RGA</span></h3>
  414. <p>While researching, I found that the two popular CRDTs implementation out there use different approaches for the virtual counter:</p>
  415. <blockquote>
  416. <ul>
  417. <li><strong><span class="caps">RGA</span></strong> [used by Automerge] maintains a single globally incremented counter (which can be ordinary integer value), that’s updated anytime we detect that remote insert has an id
  418. with sequence number higher that local counter. Therefore, every time we produce
  419. a new insert operation, we give it a highest counter value known at the time.</li>
  420. <li><strong><span class="caps">YATA</span></strong> [used by Yjs] also uses a single integer value, however unlike in case of <span class="caps">RGA</span> we
  421. don’t use a single counter shared with other replicas, but rather let each
  422. peer keep its own, which is incremented monotonically only by that peer. Since
  423. increments are monotonic, we can also use them to detect missing operations eg.
  424. updates marked as A:1 and A:3 imply, that there must be another (potentially
  425. missing) update A:2.</li>
  426. </ul>
  427. </blockquote>
  428. <h3 id="resources">Resources</h3>
  429. <ul>
  430. <li><a href="https://www.youtube.com/watch?v=x7drE24geUw">CRDTs: The Hard Parts</a>, a video by Martin Kleppmann where he explains the current state of the art of CRDTs, and why some problems aren’t solved yet.</li>
  431. <li><a href="https://jakelazaroff.com/words/an-interactive-intro-to-crdts/">An Interactive Intro to CRDTs</a> gets you trough different steps to understand what are CRDTs, and how to implement a <span class="caps">LWW</span> Register.</li>
  432. <li><a href="https://www.bartoszsypytkowski.com/the-state-of-a-state-based-crdts/">Bartosz Sypytkowski</a> introduction on CRDTs, with practical examples is very intuitive.</li>
  433. <li><a href="https://jzhao.xyz/thoughts/CRDT-Implementations"><span class="caps">CRDT</span> Implementations</a> are notes by Jacky which were useful to me when understanding CRDTs.</li>
  434. </ul>