Skip to main content
HS
HiSETSuccess
LeetCode Prep

System Design

Comprehensive interview guide: scalability, databases, distributed systems, cloud architecture, and full case-study walkthroughs — plus adaptive quizzes.

Case study

Design Google Docs Collaborative Editing

Enable real-time multi-user document editing with conflict-free merging and version history.

OTCRDTWebSocket

Requirements

  • Functional: create/edit documents; real-time sync; cursor presence; version history
  • Support 50 concurrent editors per doc; offline edit with merge on reconnect
  • Non-functional: operation propagation p99 < 200ms; no lost keystrokes
  • Consistency: eventual convergence — all clients see same final document
  • Scale: 100M docs; 10M DAU; avg 10 KB doc size

Back-of-envelope Estimation

MetricCalculationResult
Ops/sec global10M users × 2 ops/sec active fraction 10%~2M ops/sec
Hot doc ops50 editors × 2 ops/sec100 ops/sec per hot doc
Storage100M × 10 KB snapshots~1 TB (+ revision history ~5×)
WebSocket connections10M × 5% editing500K concurrent

API Design

InterfaceDescription
WS /v1/docs/{docId}/syncSend/receive operations: insert, delete, retain
GET /v1/docs/{docId}Snapshot + revision vector for catch-up
GET /v1/docs/{docId}/historyList revisions with timestamps
POST /v1/docs/{docId}/snapshotPeriodic compaction checkpoint

Data Model

EntityContent
operations_logdoc_id, op_id, user_id, op_type, position, char, ts — append-only
snapshotsdoc_id, revision, content_blob — S3 every N ops or 5 min
presencedoc_id, user_id, cursor_pos, color — Redis ephemeral
revision_vectordoc_id → { user_id: seq } — for OT ordering

High-level Design

Collaboration architecture
Clients ◄──WebSocket──► Doc Server (per-doc shard routing)
                              │
                    Apply OT/CRDT ──► append op log ──► broadcast to peers
                              │
                    Snapshot Worker (compact log periodically)
                              │
                    Object Store (snapshots + op log segments)

Deep Dive: Operational Transform

Each edit is an operation (insert 'x' at index 5). Server assigns global sequence; transforms concurrent ops against each other so insert indices remain consistent. Client applies remote ops locally; local buffer holds unacknowledged ops.

OT vs CRDT

OT (Google Docs classic) needs central server for ordering. CRDTs (Yjs, Automerge) allow P2P but use more metadata. Server-authoritative OT is simpler to reason about in interviews.

Deep Dive: Doc Sharding & Hot Documents

  • Route doc_id to dedicated server via consistent hash — single writer per doc
  • Hot doc (100 editors): shard not needed — 100 ops/sec is trivial for one server
  • Very hot (company-wide): read-only broadcast mode + comment-only for viewers
  • Catch-up: new client fetches snapshot + ops since snapshot revision

Failure Modes & Monitoring

SLOTarget
Op propagation p99< 200ms
Convergence100% within 1 sec of last edit
Zero data loss on server crashOps persisted before ack

Persist op to log before ACK to client. On server crash, replay log from last snapshot. Monitor op log growth, snapshot lag, client divergence reports.