“Operational Transformation (OT) is a concurrency control paradigm where editing intentions are expressed as operations (Insert(pos, char), Delete(pos)). When concurrent operations arrive out of order, transformation functions T(op1, op2) adjust positional indices against previously committed operations to ensure document state convergence across all clients.”
How Google Docs adjusts character indices of concurrent text operations against a central serialization server.
// Operational Transformation Function T(opA, opB)
interface InsertOp { type: 'insert'; pos: number; char: string; }
interface DeleteOp { type: 'delete'; pos: number; }
type Op = InsertOp | DeleteOp;
function transform(opA: Op, opB: Op): Op {
if (opA.type === 'insert' && opB.type === 'insert') {
if (opA.pos < opB.pos) return opA;
// Shift position right if opB inserted before opA
return { ...opA, pos: opA.pos + 1 };
}
if (opA.type === 'insert' && opB.type === 'delete') {
if (opA.pos <= opB.pos) return opA;
// Shift position left if opB deleted before opA
return { ...opA, pos: Math.max(0, opA.pos - 1) };
}
return opA;
}Client A types "X" at index 3: generates Insert(3, "X")
Concurrently, Client B deletes character at index 1: generates Delete(1)
Client A sends operation to central server; server commits Insert(3, "X")
Server transforms Client B Delete(1) against Insert(3, "X"): index shifts by +1 -> Delete(1)
Server broadcasts transformed operations to all clients; all editors reach identical document text
Jupiter architecture uses a 2-way state graph on the central server to transform operations in O(1) time per revision.