01

Requirements & scope

Problem Statement & Requirements

Functional Requirements

  • Concurrent editing — multiple users edit the same document simultaneously
  • Real-time sync — edits appear on all collaborators' screens within ~100 ms
  • Conflict resolution — concurrent edits to the same section must merge correctly without data loss
  • Cursor & selection sync — see where other users' cursors are and what they've selected
  • Presence — show who is currently viewing/editing the document
  • Undo/redo — per-user undo that doesn't revert other users' changes
  • Version history — view and restore previous versions of the document
  • Comments & suggestions — inline annotations anchored to document positions
  • Offline editing — queue local changes and sync when reconnected
  • Rich text — bold, italic, headings, lists, tables, images

Non-Functional Requirements

  • Low latency — local edits appear instantly (< 16 ms); remote edits visible within 100-200 ms
  • Consistency — all users converge to the same document state (eventual consistency)
  • High availability — documents always editable, even during partial outages
  • Scalability — support millions of documents, thousands with 50+ concurrent editors
  • Durability — no data loss; every accepted edit is persisted
  • Ordering — causal ordering of operations preserved

Out of Scope

  • Spreadsheet-specific features (cell formulas, recalculation)
  • Presentation/slides editing
  • Drawing/whiteboard (requires different data model)
  • Real-time voice/video (that's WebRTC)
02

Scale estimations

Scale Estimations

Users & Documents

MetricValue
Total users500M
Daily Active Users (DAU)50M
Total documents5B
Documents edited per day100M
Avg concurrent editors per document3-5
Max concurrent editors (edge case)100+
Avg edits per user per minute30 (typing)

Operations

MetricValue
Edits per second (global)50M DAU × 30 edits/min × (10 min active/session) / 86400 = ~175K ops/sec
Peak edits per second~500K ops/sec
Cursor position updates per second~350K/sec (2× edit rate, includes mouse moves)
WebSocket connections (concurrent)~10M

Storage

MetricValue
Average document size50 KB (text only)
Average operation size100 bytes
Operations per document per day~1,000
Operation log storage per day100M docs × 1,000 ops × 100 bytes = ~10 TB/day
Document snapshots5B × 50 KB = ~250 TB
Operation log (30-day retention before compaction)~300 TB

Network

MetricValue
Inbound (edits)175K × 100 bytes = ~17.5 MB/s
Outbound (broadcast to collaborators)17.5 MB/s × avg 3 collaborators = ~52.5 MB/s
WebSocket server count (20K conn/server)~500 servers
03

Layered architecture

Phase 1 of 4 · Client & edge

Start with the entry points

First, define who uses the system and where requests enter. This is the boundary of the design, not yet a complete request path.

Editor clients
Apply local edits immediately and track operations awaiting server acknowledgement.
WebSocket gateway
Authenticate users and route each connection to its active document session.
Architecture after phase 1. Includes Editor clients, WebSocket gateway. Editor clients connects to WebSocket gateway.
Phase 1 — Start with Editor clients, WebSocket gateway.SVGSVGEditable diagramEditable diagram

This first layer defines the system boundary. The next chapters add application logic and data dependencies. After examining the request paths and tradeoffs, we will add operational safeguards and bring the complete architecture together.

04

API & contracts

Phase 2 of 4 · Application & compute

Add the application logic

Keep the entry points and add the services that do the work. Give each service a clear responsibility before defining its contracts. Storage and background dependencies come next.

Collaboration engine
Transform concurrent operations against accepted history so clients converge on a shared document.
Presence service
Broadcast transient cursor state separately from durable document changes.
Architecture after phase 2. Includes Editor clients, WebSocket gateway, Collaboration engine, Presence service. Editor clients connects to WebSocket gateway. WebSocket gateway connects to Collaboration engine. WebSocket gateway connects to Presence service.
Phase 2 — Added Collaboration engine, Presence service. Earlier layers remain in place.SVGSVGEditable diagramEditable diagram

Detailed OT Client-Server Protocol

Client State Machine

Client State Machine
Client State MachineSVGSVGEditable diagramEditable diagram

Server-Side OT Processing

Server-Side OT Processing
Server-Side OT ProcessingSVGSVGEditable diagramEditable diagram
05

Data model

Phase 3 of 4 · Data & async work

Connect data and background work

The services now need their dependencies: durable state, fast lookups, or work that continues outside a request. Separate these responsibilities rather than treating every component as a database.

Operation log
Append accepted operations for recovery, replay, and reconnect catch-up.
Document store
Periodically snapshot the document so replay cost stays bounded.
Offline buffer
Reconcile buffered edits against the current document revision instead of overwriting remote changes.
Architecture after phase 3. Includes Editor clients, WebSocket gateway, Collaboration engine, Presence service, Operation log, Document store, Offline buffer. Editor clients connects to WebSocket gateway. WebSocket gateway connects to Collaboration engine. WebSocket gateway connects to Presence service. Collaboration engine connects to Operation log. Collaboration engine connects to Document store. Collaboration engine connects to Offline buffer.
Phase 3 — Added Operation log, Document store, Offline buffer. Earlier layers remain in place.SVGSVGEditable diagramEditable diagram

Document Storage & Versioning

Snapshot + Operation Log Model

Snapshot + Operation Log Model
Snapshot + Operation Log ModelSVGSVGEditable diagramEditable diagram

Data Model

Data Model
Data ModelSVGSVGEditable diagramEditable diagram
06

Core design decisions

The Core Problem: Concurrent Editing

The Core Problem: Concurrent Editing
The Core Problem: Concurrent EditingSVGSVGEditable diagramEditable diagram

Algorithm Deep Dive: OT vs CRDTs

Option 1: Operational Transformation (OT)

Option 1: Operational Transformation (OT)
Option 1: Operational Transformation (OT)SVGSVGEditable diagramEditable diagram

OT Transform Rules

OT Transform Rules
OT Transform RulesSVGSVGEditable diagramEditable diagram

OT Architecture (Google Docs Approach)

OT Architecture (Google Docs Approach)
OT Architecture (Google Docs Approach)SVGSVGEditable diagramEditable diagram

Option 2: CRDTs (Conflict-free Replicated Data Types)

Option 2: CRDTs (Conflict-free Replicated Data Types)
Option 2: CRDTs (Conflict-free Replicated Data Types)SVGSVGEditable diagramEditable diagram

OT vs CRDT Comparison

OT vs CRDT Comparison
OT vs CRDT ComparisonSVGSVGEditable diagramEditable diagram
07

Request flows

An editor applies an operation locally, sends it with a stable ID, and receives acknowledgement of its accepted position or version. Other editors apply the corresponding remote operation under the chosen OT or CRDT rules. A reconnecting client exchanges missing operations or loads a snapshot before replaying outstanding local changes.

08

Performance & caching

Batch nearby edits when that does not make typing feel delayed. Cursor movement and presence can be throttled or dropped; document operations cannot be discarded on the same basis. Periodic snapshots bound how much history a new participant must replay, but retain enough operation history to reconcile supported offline sessions.

09

Advanced design

Cursor & Selection Sync

Cursor & Selection Sync
Cursor & Selection SyncSVGSVGEditable diagramEditable diagram

Presence System

Presence System
Presence SystemSVGSVGEditable diagramEditable diagram

Rich Text Document Model

Rich Text Document Model
Rich Text Document ModelSVGSVGEditable diagramEditable diagram

Comments & Suggestions

Comments & Suggestions
Comments & SuggestionsSVGSVGEditable diagramEditable diagram

Document Session Management

Document Session Management
Document Session ManagementSVGSVGEditable diagramEditable diagram
10

Edge cases

Offline Editing

Offline Editing
Offline EditingSVGSVGEditable diagramEditable diagram

Undo/Redo in Collaborative Context

Undo/Redo in Collaborative Context
Undo/Redo in Collaborative ContextSVGSVGEditable diagramEditable diagram
11

Tradeoffs

Tradeoffs & Design Decisions Summary

DecisionOption AOption BChosenWhy
Sync algorithmOT (Operational Transformation)CRDT (Conflict-free Replicated Data Types)OT (server-centric)Lower memory overhead, battle-tested (Google Docs), simpler architecture with central server
ArchitecturePeer-to-peerCentral serverCentral serverSimpler OT (single serialization point), easier persistence, permissions enforcement
Document modelFlat text (positions)Tree-based (ProseMirror)Tree-basedRich text requires structure (paragraphs, lists, tables)
StorageFull document per editSnapshot + operation logSnapshot + op logSpace-efficient, enables version history, supports audit trail
Session routingRandom (any node)Sticky by doc_idStickySingle-threaded OT per doc — all ops for a doc must go to same node
Cursor syncReliable (guaranteed delivery)Unreliable (fire-and-forget)UnreliableCursors are ephemeral; next update corrects any missed one
Snapshot frequencyEvery operationEvery 1,000 opsEvery 1,000 opsBalance between recovery speed and storage cost
OfflineDisallowOT transform chainOT + fallback to 3-way mergeOT works for short offline; 3-way merge for extended offline
Undo modelGlobal undo stackPer-user undo stackPer-userUser's undo should only revert their own operations
CommentsPosition indexMarker-based anchoringMarkersMarkers move with text as document changes
Op log storageMySQLCassandraCassandraAppend-heavy, partition by doc_id, linear scaling
12

Reliability & fault tolerance

Phase 4 of 4 · Operations & resilience

Add operational safeguards

With the request paths and tradeoffs established, add the controls that help detect trouble and keep the system operating. The final diagram retains every layer introduced so far.

Session control
Use explicit session ownership and recovery rules to avoid concurrent authoritative editors.
Architecture after phase 4. Includes Editor clients, WebSocket gateway, Collaboration engine, Presence service, Operation log, Document store, Offline buffer, Session control. Editor clients connects to WebSocket gateway. WebSocket gateway connects to Collaboration engine. WebSocket gateway connects to Presence service. Collaboration engine connects to Operation log. Collaboration engine connects to Document store. Collaboration engine connects to Offline buffer. Collaboration engine connects to Session control.
Phase 4 — Added Session control. Earlier layers remain in place.SVGSVGEditable diagramEditable diagram

Persist accepted document operations before acknowledging durable acceptance. If a collaboration server fails, reconnecting clients use stable operation IDs to avoid applying retries twice. Separate ephemeral presence from document state, and test recovery with two editors who kept working offline from different revisions.

13

Production architecture

High-Level Architecture

High-Level Architecture
High-Level ArchitectureSVGSVGEditable diagramEditable diagram

Detailed Component Architecture

Detailed Component Architecture
Detailed Component ArchitectureWide diagram — scroll horizontally.SVGSVGEditable diagramEditable diagram

Production Architecture

Production Architecture
Production ArchitectureWide diagram — scroll horizontally.SVGSVGEditable diagramEditable diagram
14

Further exploration

Have one editor delete a paragraph while another inserts a sentence inside it. Describe the intended result before picking an algorithm. Then undo the deletion. This small exercise exposes whether the product’s editing semantics match the conflict-resolution rules rather than merely producing the same bytes on every device.

15

Interview playbook

Interview Tips

  1. Start by stating the core challenge — "The fundamental problem is: two users edit the same word at the same time. How do we merge without losing either edit?" This frames everything.

  2. Draw the OT diamond — Show state S, two concurrent ops, and how transformation produces the same result S' regardless of order. This visual sells your understanding.

  3. Compare OT vs CRDT — Even if you choose one, show you know the other. "OT requires a central server but uses less memory. CRDTs work peer-to-peer but carry tombstone overhead."

  4. Client state machine is a great deep dive — Show SYNCHRONIZED → AWAITING_ACK → AWAITING_ACK_WITH_BUFFER. This proves you understand the practical implementation, not just theory.

  5. Don't forget cursors — Interviewers love the cursor sync detail. "Cursor positions must be transformed against incoming operations. We throttle cursor updates to 20/sec and use unreliable delivery."

  6. Snapshot + op log for storage — "We don't store the full document on every keystroke. We take snapshots every 1,000 ops and replay the op log to reconstruct any revision."

  7. Session stickiness is essential — "All operations for a document must route to the same server node. This gives us single-threaded OT — no distributed consensus needed for operation ordering."

  8. Mention undo correctness — "Per-user undo stack. When you hit Ctrl+Z, it reverts YOUR last operation, not someone else's. The inverse is transformed against all subsequent ops."

  9. Version history comes for free — "Because we store the operation log, version history is just 'replay ops from snapshot to revision N.' Diff is computed from the op sequence."

  10. Name real implementations — "Google Docs uses server-centric OT. Figma uses CRDTs. Yjs is the most popular open-source CRDT library." This shows industry awareness.