CRDTs, Merkle clocks or a server: syncing a shared whiteboard
· 6 min read
Say you're building a shared whiteboard with sticky notes you can type in. Up to 50 people edit one board at once, every keystroke should show up for everyone within about 100 ms, and people on a train should be able to keep working offline and merge when they're back.
Every multi-user editor has the same core problem. Two people change the same thing at the same time, on different machines, and both copies have to end up identical without anyone's work disappearing. There are three broad ways to solve it, and they're often compared as if they were the same kind of thing. They aren't.
first, WebSockets aren't a sync method
WebSockets are a transport: a long-lived connection that lets the server push changes to clients as they happen. They decide how changes travel, not how conflicting changes are resolved. You still need a rule for what happens when user A moves a note left while user B moves it right.
So the real comparison is between ways of resolving conflicts, each of which can run over WebSockets, WebRTC or something else entirely:
- A central server decides, either by transforming edits (operational transformation) or by simply being the authority.
- CRDTs, data types that merge on their own, so any two copies that have seen the same edits are identical.
- Merkle-CRDTs, CRDTs whose history is a hash-linked graph, so peers can sync without a server, for example over IPFS.
In each picture below, purple marks where conflicts get resolved.
option 1: the server decides
Every edit goes to one server, which puts all edits in a single order and sends the result out. For text, Google Docs uses operational transformation: if user A inserts at position 5 and user B deletes at position 2, the server rewrites user A's edit to position 4 before applying it. For whiteboards it can be simpler. Figma's multiplayer, as they've described it, has the server act as the authority, with the last write to each property of each object winning.
- Good: one source of truth, easy permissions and validation, small payloads, and a mental model every backend engineer already has.
- Bad: nothing merges without the server. Offline editing is limited to queuing edits and hoping the transforms still make sense when you reconnect, and transformation for rich text is notoriously hard to get right.
option 2: CRDTs
A CRDT, a conflict-free replicated data type, is built so that merging is always safe. Merges are commutative, associative and idempotent: apply the same edits in any order, any number of times, and every copy ends up the same. That's called strong eventual consistency, and it means no central authority is needed to decide the outcome.
For the whiteboard, each note is an entry in a map, and each property, like position, colour or size, is a last-writer-wins register. If two people drag the same note, one position wins, but nothing is lost or corrupted. Text inside a note uses a sequence CRDT. Instead of positions, every character gets a unique ID and sits relative to its neighbours, so concurrent inserts can't collide. Yjs (based on an algorithm called YATA), Automerge and newer designs like Fugue all work this way, and Fugue specifically avoids interleaving, where two people typing at the same spot get their words shuffled together.
- Good: edits apply locally and instantly, offline works for free since you just merge later, and any transport will do.
- Bad: metadata. Every character carries an ID, deleted text usually leaves a tombstone, and history grows. Modern libraries compress this well, but it's never zero. Permissions are also harder, because a merge rule can't tell a valid edit from a malicious one.
In practice, most CRDT apps still use a server, just a dumb one: a WebSocket relay that forwards updates and stores the document so new clients can load it. The server never decides anything, so it can be simple and cheap.
option 3: Merkle clocks and IPFS
CRDTs need to know which edits happened before which. The classic tool is a vector clock: a counter per replica. With 50 editors that's 50 counters on every update, and it grows with every device that ever joined.
A Merkle clock replaces that with a graph. Each update is a node that contains its change plus the hashes of the updates it built on, like a git commit. Because each node is named by the hash of its contents, two peers can compare just their latest hashes, the heads. If the heads match, the whole history matches. If not, each peer walks back from the other's heads and fetches only the nodes it's missing. Causality comes from the graph itself, so the clock doesn't grow with the number of peers.
Put a CRDT's updates in that graph and you get a Merkle-CRDT, the design described by Sanjuán and others at Protocol Labs. IPFS fits it naturally. IPFS stores content by its hash, so every node is a block anyone can fetch and verify, and peers announce new heads over pubsub. OrbitDB is built this way.
- Good: no server at all, every piece of history is tamper-evident, peers can sync from anyone who has the blocks, and a missed message doesn't matter because the next head brings the history with it.
- Bad: for a real-time editor, latency. Finding and fetching blocks over a peer-to-peer network can take seconds, not the 100 ms a keystroke needs. Connections between peers behind home routers often need relays anyway. And a hash-linked history is append-only, so cleaning up old history is hard.
where Merkle ideas show up anyway
You don't need IPFS to use the core idea. Automerge identifies every change by its hash and records which changes it depends on, so its history is already a hash-linked graph, and its sync protocol exchanges heads plus a compact summary of known changes so peers only send what's missing. Databases like Cassandra use Merkle trees to compare replicas cheaply. Comparing hashes instead of full state is useful well beyond peer-to-peer.
what I'd pick
For this whiteboard: a CRDT library like Yjs or Automerge, synced over a WebSocket relay that stores the document.
- It meets the latency target. Edits apply locally first, and the relay is one hop away.
- Offline works without special cases. Reconnecting is just another merge.
- The server stays simple. It relays and stores, but never resolves conflicts, so there's no transformation logic to get wrong.
- Permissions can still live on the server. The relay can refuse updates from users who can only view, even though it doesn't decide how edits merge.
- Cursors and selections go on a separate channel that isn't saved, since nobody needs a history of where your mouse was.
I'd pick option 1 instead if the app needs strict validation on every edit, like a spreadsheet where some cells must obey business rules, or if offline editing doesn't matter. A single authority is simpler to reason about when everyone's online.
And I'd pick Merkle-CRDTs over IPFS when the requirement isn't speed but independence: no company should be able to switch the document off, history must be verifiable, or users must sync directly with each other. For a shared whiteboard that's rarely the case. For something like a public record or an archive that has to outlive whoever hosts it, it could be exactly right.
references
- Marc Shapiro, Nuno Preguiça, Carlos Baquero and Marek Zawirski. Conflict-free Replicated Data Types. SSS 2011. The paper that defined CRDTs.
- Héctor Sanjuán, Samuli Pöyhtäri, Pedro Teixeira and Ioannis Psaras. Merkle-CRDTs: Merkle-DAGs meet CRDTs. 2020.
- Matthew Weidner and Martin Kleppmann. The Art of the Fugue: Minimizing Interleaving in Collaborative Text Editing. 2023.
- Evan Wallace. How Figma's multiplayer technology works. Figma, 2019.
- Yjs and Automerge, two widely used CRDT libraries, and Automerge's sync protocol.
- OrbitDB, a peer-to-peer database built on Merkle-CRDTs and IPFS, and IPFS's guide to content addressing.