Design Google Docs (Collaborative Document Editor)

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-12-29

Problem Statement

Design the Low-Level Design (LLD) for a simplified version of Google Docs, a real-time collaborative document editing system.


The system should allow multiple users to view and edit the same document simultaneously, with changes being reflected in real time. The design should focus on document representation, user sessions, version control, and concurrency handling, rather than network or UI implementation.


🎯 Core Requirements


1. Document Management


•Users should be able to:
•Create a new document.
•Open an existing document.
•Edit and save changes.
•View edit history (version tracking).
•Each document should maintain a unique ID, title, content, and metadata (createdAt, modifiedAt, owner, etc.).


2. Real-Time Collaboration


•Multiple users can simultaneously edit the same document.
•System should handle:
•Concurrent edits (two users typing in the same region).
•Conflict resolution (using techniques like Operational Transformation or CRDT).
•Edits must be propagated to all active sessions in real time.


3. Version Control


•The system should maintain versions of the document.
•Allow undo/redo functionality.
•Optionally support viewing or reverting to a previous version.


4. User Sessions & Permissions


•Users can be:
•Owner – full access.
•Editor – can modify.
•Viewer – read-only.
•Track active users currently editing a document.


5. Change Tracking


•Maintain an edit log — what user made which change and when.
•Support commenting and suggestion mode (optional extension).


⚙️ Key Operations (APIs)


APIDescription


createDocument(User user, String title)


Creates a new document.


openDocument(String docId, User user)


Opens a document for viewing/editing.


editDocument(String docId, User user, EditOperation edit)


Applies an edit (insert, delete, replace).


getDocumentContent(String docId)


Retrieves current content.


getEditHistory(String docId)


Returns version/change history.


addCollaborator(String docId, User user, Role role)


Grants access to other users.


🧱 Suggested Class Design


ClassResponsibility


Document


Represents a text document with content, metadata, and edit history.


User


Represents a system user with name, ID, and permissions.


Session


Represents an active editing/viewing session by a user.


EditOperation


Represents an atomic change (insert, delete, replace) with metadata.


CollaborationManager


Coordinates real-time edits across sessions.


VersionManager


Handles document versions, undo/redo.


PermissionManager


Controls access (view/edit/share).


ChangeNotifier


Sends updates to all connected clients/sessions.


🔄 Example Flow


Example:


1.User A creates a document → “Design LLD for Google Docs”.
2.User A shares it with User B as an editor.
3.Both users open the document:
•User A types “Hello”.
•User B types “World”.
4.Collaboration manager merges updates → Document content becomes:
5.Hello World
6.Version manager creates a new version.
7.Both see updates in real time.


🧠 Design Considerations


1. Concurrency Handling


•Implement Operational Transformation (OT) or CRDT (Conflict-free Replicated Data Types) for resolving concurrent edits.


2. Synchronization


•Each edit is converted into an operation and broadcast to all connected clients.
•Edits are applied in order with conflict resolution.


3. Scalability


•Document edits should be small, atomic operations.
•Maintain minimal state transfer for real-time updates.


4. Persistence


•Store document versions and operations for durability.
•Maintain current active state in memory for speed.


🚀 Possible Extensions


•Commenting & Suggestions mode.
•Offline Editing (sync when reconnected).
•Presence Indicators (show where others are editing).
•Document Locking for large structured edits.
•Formatting support (bold, italic, headings).
•Export (PDF, DOCX).


🧩 Example Entities


Document

1 2id: "doc123" 3title: "System Design Notes" 4content: "Google Docs LLD" 5createdBy: "Sahil" 6collaborators: [UserA(Owner), UserB(Editor)] 7versions: [v1, v2, ...]


EditOperation

1 2operationType: INSERT 3position: 5 4text: "World" 5user: UserB 6timestamp: 2025-10-24T13:00


✅ Goal:


Design the classes, interactions, and core logic to support real-time collaborative editing, version management, and access control for a Google Docs–like application.

Solution

Comments