CRDTs & Conflict Resolution

1 / 17

The problem

Two users edit offline. Both changes are valid. Network later reconnects. What should the system do?

Bad answer: choose one whole document and discard the other.

2 / 17

Goal

Build data types that allow local updates and later convergence.

Not magic. Not “eventual consistency” as a slogan. A concrete merge/apply design.

3 / 17

Merge laws

For state-based CRDTs:

  • commutative
  • associative
  • idempotent

These mean order, batching, and duplicate delivery do not change the result.

4 / 17

G-Counter

Problem: max integer loses concurrent increments.

Solution:

  • store one count per replica;
  • merge by element-wise max;
  • value is sum.

Metadata preserves intent.

5 / 17

PN-Counter

Two G-Counters:

  • P for increments;
  • N for decrements;
  • value = sum(P) - sum(N).

Never merge only the final integer.

6 / 17

LWW Register

Higher timestamp wins. Tie-break deterministically.

Useful for low-value overwrites. Dangerous for user-created content.

Question: what work are we willing to lose?

7 / 17

Vector clocks

Wall clocks answer: what time did the machine say? Vector clocks answer: what did this event know?

Outcomes:

  • before;
  • after;
  • equal;
  • concurrent.
8 / 17

MV-Register

Keep all values that are not causally dominated.

This converts hidden data loss into visible conflict resolution.

9 / 17

OR-Set

Adds get unique tags. Removes remove observed tags only.

Concurrent add and remove can preserve the add.

10 / 17

OR-Map

Real structures compose CRDTs.

Example:

ShoppingCart = OR-Map<ProductID, PN-Counter>
11 / 17

Text editing problem

Indexes are unstable.

“Insert at index 5” means different things after concurrent edits.

Use stable character IDs instead.

12 / 17

Sequence CRDT idea

Insert operation:

insert value V with id A#7 after parent B#3

Delete operation:

delete target A#7

Render deterministically.

13 / 17

Tombstones

Deleted characters stay as invisible anchors.

They preserve positions for operations that arrive later.

14 / 17

Sync protocol

Needed pieces:

  • operation IDs;
  • version vector;
  • missing-op query;
  • idempotent apply;
  • snapshot;
  • deterministic render.
15 / 17

Capstone

Build a collaborative browser editor.

Provided:

  • Go HTTP server;
  • browser UI;
  • polling sync shell;
  • textarea diff adapter;
  • tests;
  • reference solution.

Learner implements:

  • CRDT document core;
  • version vectors;
  • missing-op sync;
  • convergence behavior.
16 / 17

Final demo

Open two tabs. Type concurrently. Sync. Both converge. Duplicate delivery does not duplicate characters. Delete-before-insert still works.

17 / 17
Use arrow keys or click edges to navigate. Press H to toggle help, F for fullscreen.