Understanding the Operational Transformation (OT) Algorithm for Real-Time Collaborative Editing
This article explains the core concepts of the Operational Transformation algorithm, including atomic operations, version confirmation, operation transformation, and client state management, and demonstrates how these techniques ensure consistent document content during simultaneous edits in collaborative editing scenarios.
58云视 is a self‑developed audio‑video call system that supports collaborative editing in interview rooms, where multiple participants edit the same document simultaneously and must see a consistent view.
The article outlines three naive approaches—full‑content broadcast, operation locking, and incremental updates—and shows why they fail to guarantee consistency.
It then introduces the Operational Transformation (OT) algorithm as the solution, summarizing its four key technical points: defining atomic operations (Retain, Insert, Delete), a version‑confirmation mechanism, operation transformation, and client‑side state transitions.
Atomic operations are represented as arrays, e.g., ['A'] for inserting "A", [1, 'B'] for retaining one character then inserting "B", and [1, -1, 1] for retain‑delete‑retain.
The communication between editor and server consists of these operation arrays, which are wrapped in a TextOperation object:
function TextOperation () {
this.ops = [];
this.baseLength = 0;
this.targetLength = 0;
}baseLength records the document length before the operation, while targetLength records the length after applying the operation.
An example transformation converts "123456789" to "123A89" using the array [3, "A", -4, 2] , resulting in { ops: [3, "A", -4, 2], baseLength: 9, targetLength: 6 } .
The version‑confirmation mechanism ensures the server knows which version a client’s operation is based on, allowing proper transformation before applying changes.
Operation transformation is expressed by the formula [A', B'] = transform(A, B) , guaranteeing that applying A then B' yields the same result as applying B then A' . The article references the open‑source ot.js implementation and illustrates the process with diamond‑shaped diagrams.
Client state management includes three states: Synchronized (no pending ops), AwaitingConfirm (waiting for server confirmation), and AwaitingWithBuffer (waiting for confirmation while buffering additional ops). State transitions are visualized with additional diagrams.
Finally, the article analyzes several conflict scenarios—single client vs. single server edit, single client vs. multiple server edits, and multiple client edits vs. single server edit—showing how OT resolves each case.
In conclusion, the OT algorithm and its supporting design effectively solve consistency problems in pure‑text collaborative editing, though more complex documents (e.g., rich‑text, images) require extended atomic operations and transformation logic.
58 Tech
Official tech channel of 58, a platform for tech innovation, sharing, and communication.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.