# System Design Interview: Designing Real-Time Collaborative Editing (Google Docs-style) The key question is how several people can type into the same document at once and all end up with the same text. ## Requirements - Multiple users edit one document simultaneously and see each other's changes in under a second. - Offline edits merge when the user reconnects. - Full version history. ## Two approaches to convergence **Operational Transformation (OT).** Each edit is an operation like `insert("x", pos 5)`. When two operations were made concurrently, the server *transforms* one against the other (if Alice inserted 3 characters before position 5, Bob's insert at 5 becomes an insert at 8). A central server orders operations, which keeps the transform logic manageable. Google Docs uses this family. **CRDTs.** Every character gets a unique, ordered ID (for example a position between two fractional keys plus a site ID), so concurrent inserts can be merged in any order and still converge without a central server. Yjs and Automerge are popular libraries. Cost: extra metadata per character and tombstones for deletes. In an interview, pick one, explain why, and mention the other. ## Architecture (OT version) 1. Clients hold a WebSocket to a **collaboration server**; all users of one document are routed to the same server (consistent hashing on document ID). 2. The server keeps the document in memory, assigns each incoming operation a revision number, transforms it against operations the client hadn't seen, applies it, and broadcasts it. 3. Operations are appended to a log in durable storage; periodic **snapshots** let you load a document without replaying its whole history. ## Presence and cursors Cursor positions and "who's here" are ephemeral, so send them over the same socket but don't persist them. ## Trade-offs to mention - Single owner server per document is simple but is a hotspot for documents with hundreds of editors; you can cap editors and make the rest viewers. - Snapshots every N operations trade storage for load time. - Permissions must be checked on every operation, not just on open. --- *Disclosure: I work on [Linkjob AI](https://www.linkjob.ai/), a real-time interview copilot.*