CRDT vs OT: Architecture of Collaborative Code
Consider the core problem of a collaborative code editor: User A and User B are typing on the exact same line, at the exact same millisecond, from different continents. How do you prevent their text from scrambling?
What is CRDT vs OT?
CRDT (Conflict-free Replicated Data Type) and OT (Operational Transformation) are competing mathematical algorithms used to achieve real-time synchronization in multi-user environments. While OT relies on a centralized server to resolve text conflicts sequentially, CRDTs use fractional indexing allowing clients to resolve edits locally without an authoritative server.
For the last decade, the industry standard solution was OT. However, when architecting the backend for LiveCodeShare, we made a radical decision: abandoning OT entirely in favor of CRDTs.
The Fundamental Flaw of OT
Operational Transformation represents edits as "mutations" triggered by an index. For example: Insert("X", at_position: 10)
If two users execute disjointed inserts simultaneously, a centralized server must receive both operations. It then has to run complex math to "transform" one operation against the other, ensuring that indices don't overlap, before broadcasting the corrected operations back to the clients.
The Mathematical Elegance of CRDTs
CRDTs discard the necessity for an authoritative central timeline. Instead, they use mathematical guarantees to ensure that if node A and node B receive the exact same set of state updates, they will deterministically converge on the precise same end structure.
Fractional Indexing
Unlike OT which utilizes whole numbers (index 10), text CRDTs give every single character typed a globally unique, fractional ID.
- User A types "A" (Assigned ID: 0.1)
- User B types "B" (Assigned ID: 0.2)
If a user types "C" between them, it's assigned an ID halfway between them: 0.15. Because 0.1 < 0.15 < 0.2, the character "C" will consistently render exactly in the middle across all connected clients, without any central server having to mediate.
Why LiveCodeShare Feels Instantaneous
By leveraging CRDTs, our WebSocket servers were downgraded from "Complex Mathematical Authority Nodes" into completely "Dumb Relays".
Our backend only exists to bounce heavily encrypted binary packets between clients as rapidly as possible. The heavy lifting of sequence resolution is handled natively in your browser's local memory footprint. This decentralized approach is what allows you to share code online with zero latency, making remote coding interviews feel exactly like sitting next to the candidate.