Realtime collab (CRDT) design
A conflict-free replicated data type architecture for multiplayer editing without operational transforms.
Core idea
Use a sequence CRDT (RGA or YATA) where each character carries a unique Lamport timestamp + site ID. Inserts reference left/right neighbours. Deletes are tombstones. Merges are commutative — no central server needed for correctness.
Data model
Atom— { id: [clock, siteId], value: char, tombstone: bool }Doc— ordered list of atoms, causal graph for partial orderingVersion vector— per-site clock for anti-entropy sync
Sync protocol
Peers exchange version vectors on connect. Missing atoms are pulled via a range query over the causal graph. WebSocket transport with msgpack binary encoding keeps overhead under 2 ms per op. Tombstone GC runs every 500 edits with a 30-second retention window.
Conflict resolution
Concurrent inserts at the same position are ordered by site ID (deterministic tiebreak). No OT transform needed. The CRDT guarantees strong eventual consistency — all peers converge to identical document state without a coordinator.
Stack
yjs / automerge · WebSocket · msgpack · IndexedDB persistence · Next.js edge relay