Ever wondered how two people can type in the same Google Doc at the same instant, and nothing gets lost or garbled? These are my notes from studying the system design of a real-time collaborative editor: what it must do, how big it gets, how the architecture fits together, and the algorithm at its heart, Operational Transformation (OT).

1. What are we building?

A real-time collaborative document editing system that allows multiple users to:

  • Create, edit, and share documents
  • See changes instantly (real-time)
  • Work simultaneously without conflicts
  • Access from anywhere (web / mobile)

2. Functional Requirements

  1. Create / Read / Update Doc
  2. Real-time collaborative editing (multi-user)
  3. Conflict resolution (OT)
  4. Cursor & presence (see others typing)
  5. Version history / undo-redo

3. Capacity Estimation

Assumptions

Metric Value
Daily Active Users (DAU) 100M
Peak concurrent users (~1%) 1M

Write throughput

  • Average typing speed ≈ 5 chars/sec/user
  • 1M users × 5 ops/sec = 5M ops/sec
  • This means the system must handle ~5M operations per second

Size per operation ≈ 50 bytes (compressed JSON / binary).

Each operation contains:

  • type (insert / delete)
  • position
  • value
  • length
  • version

Total write bandwidth:

5M ops/sec × 50 bytes ≈ 250 MB/sec (incoming) 

Document size & storage

Metric Value
Average doc size 100 KB
Docs per user (assumed) 2
Total docs 100M users × 2 = 200M docs
Storage required (now) 200M × 100 KB = 20 TB
With version history (~5× growth) ~100 TB

Database (persistent storage)

  • Write pattern: snapshot every N ops (say every 100 ops)
  • Write rate: 5M ops/sec ÷ 100 → ~50K snapshot writes/sec
  • Storage: ~100 TB (including history)

Takeaway: the hot path is the operation stream (5M ops/sec), while durable storage only sees a small fraction of it (50K snapshots/sec). That is why the design separates in-memory sequencing from persistence.

4. High-Level Architecture

The write path, step by step

  1. Client (User A / User B). The editor applies the change optimistically to the local UI (e.g. Insert('A', index 10, v10)). The op goes into a Pending Queue and a client-side OT engine keeps local state consistent with what the server sends back.
  2. Authentication Service. Validates the auth token and establishes the session.
  3. Secure WebSocket Gateway. A persistent, bi-directional connection carries the realtime ops stream up and live updates down.
  4. Global Load Balancer & Routing. Routes by hash(docId) → shard/cluster, so every operation for a given document lands on the same shard. This is what makes a single ordering of ops per document possible.
  5. Collaboration Cluster (sharded).
    • In-memory document store: current version, ops log, active users and cursors.
    • Global Sequencer: imposes one total order on concurrent operations (e.g. C → A → B).
    • Operational Transformation Engine: transforms each op against the ops it missed (e.g. Insert(B, 12, v10) becomes Insert(B, 9, v15)).
    • Broadcast Manager: fans the transformed op out to everyone.
  6. Hot Cache (Redis). Active docs and recent ops for fast reads.
  7. Kafka (partitioned by docId). Carries the operation event stream and the state replication stream to Collab Server replicas, which also persist to the DB.
  8. Persistent DB. Snapshots, ops, and history.
  9. Live updates. Collab server → WebSocket gateway → pushed to all clients.

5. Conflict Resolution with Operational Transformation (OT)

The idea

The server maintains a versioned log of operations. Each incoming operation carries a base version (the document version the client was looking at when it made the edit). The server transforms it against all operations applied after that version using pairwise transformation rules (insert–insert, delete–insert, insert–delete, delete–delete). After transformation, the operation is applied, the version is incremented, and the op is broadcast to all clients.

Core concept: OT ensures consistency by transforming incoming operations against the server's version log when multiple users edit simultaneously.

Operation format

Operation: { type: "i" | "d", // insert or delete pos: int, // index in the document value: string, // text for inserts len: int, // length for deletes baseVersion: int // version the client based this on } 

Server state

currentVersion = N operationsLog = [op1, op2, ..., opN] // an incoming op must be transformed against all ops from baseVersion..N 

handleIncomingOperation(op)

function handleIncomingOperation(op) { // Step 1: Validate if (op.baseVersion > currentVersion) { reject(op); // client is ahead of server return; } // Step 2: Find missed operations const missedOps = operationsLog.slice(op.baseVersion, currentVersion); // Step 3: Transform for (const oldOp of missedOps) { op = transform(op, oldOp); } // Step 4: Apply apply(op); // Step 5: Update version currentVersion++; // Step 6: Store op with its new version op.version = currentVersion; operationsLog.append(op); // Step 7: Broadcast to clients broadcast(op); } 

The "magic" is Step 2 and Step 3: any op that was made against an older version gets replayed through everything it didn't know about, so its position is corrected before it is applied.

6. Worked Examples

All examples start with Doc = "ABCDE" at Version = 0 (indexes: A=0, B=1, C=2, D=3, E=4), with two users, A and B, editing concurrently. The six cases:

  1. Insert & Insert
  2. Delete & Insert
  3. Insert & Delete
  4. Delete & Delete
  5. Delete Range & Insert
  6. Insert Inside Delete Range

Case 1: Insert & Insert

  • A → insert(x, pos 2, v0)
  • B → insert(y, pos 2, v0)
  • A reaches the server first → doc becomes ABxCDE (v1)
  • B missed 1 op (v1 − v0). A's insert landed at the same position or earlier, so B's position shifts: 2 + 1 = 3 → insert(y, 3, v1)
  • Result: ABxyCDE (v2)

Case 2: Delete & Insert

  • A → delete(B, pos 1, v0)
  • B → insert(x, pos 3, v0)
  • A first → ACDE (v1)
  • B missed 1 op. A deleted a character before B's insert position, so B's position shifts left: insert(x, pos 2)
  • Result: ACxDE (v2)

Case 3: Insert & Delete

  • A → insert(x, pos 2, v0)
  • B → delete(pos 3, v0) (deletes D)
  • A first → ABxCDE (v1)
  • B missed 1 op. A inserted before the delete position, so the delete shifts right: delete(pos 4, v1)
  • Result: ABxCE (v2)

Case 4: Delete & Delete

  • A → delete(pos 1, v0) (deletes B)
  • B → delete(pos 3, v0) (deletes D)
  • A first → ACDE (v1)
  • OT transforms B's op to delete(pos 2, v1) (still D)
  • Result: ACE (v2)

Case 5: Delete Range & Insert

Start: ABCDEFG (v0)

  • A → delete(pos 2, len 3, v0) (removes CDE)
  • B → insert(x, pos 6, v0) (between F and G)
  • A first → ABFG (v1)
  • B's insert is after the deleted range, so it shifts left by the range length: insert(x, pos 3, v1)
  • Result: ABFxG (v2)

Case 6: Insert Inside a Delete Range

Start: ABCDEFG (v0)

  • A → delete(pos 2, len 3, v0) → range [2-4] removed
  • C → insert(x, pos 3, v0), which falls inside the deleted range
  • The text around the insert point no longer exists, so the insert is pushed to the start of the range: insert(x, pos 2, v1)
  • Result: ABxFG (v2)

Rule of thumb for transformations: calculate the new index based on the missed operations. Earlier inserts push positions right, earlier deletes pull positions left, and inserts that land inside a deleted range snap to the range start.

7. Cursors & Presence

Cursor positions are also transformed using OT, so everyone's cursor stays correctly synced with other users' edits.

Example: if your cursor sits right after Hello and someone else inserts text, your cursor index is shifted by the same transformation rules so it still points at the same logical place in the text.

Presence data (active users and cursor positions) lives in the in-memory document store on the collaboration shard, and is fanned out through the same broadcast path as edits.

8. Key Takeaways

  • Optimistic UI + pending queue makes typing feel instant; the server remains the source of truth.
  • Shard by docId so all ops for a doc hit one sequencer, giving a single total order.
  • OT transforms each incoming op against the ops it missed, using the version log.
  • Separate hot and cold paths: 5M ops/sec go through memory, Redis and Kafka; the DB only sees periodic snapshots (~50K writes/sec).
  • Version history comes for free from the ops log plus snapshots, which also powers undo/redo.

9. What's next

Things I want to explore from here: OT vs CRDTs (Yjs, Automerge), snapshot/compaction strategies for the ops log, and offline editing and reconnect handling.

Thanks for reading! If you spot a mistake or have a better way to explain OT, I'd love to hear it in the comments.