Course contentsShow
← System Design
Lesson 1579 of 1,91938. Real-World System: PastebinFree lesson

Collaborative Editing and Real-Time Updates

Implementing real-time collaborative editing with operational transforms or CRDTs for multi-user paste sessions.

Collaborative Editing and Real-Time Updates

What you'll learn: How to enable multiple users to edit the same paste simultaneously without conflicts, using operational transforms or CRDTs.

When multiple users edit a paste at the same time (like Google Docs), their changes must merge gracefully without overwriting each other's work. This is fundamentally different from the single-user paste operations we've handled so far.

The Core Challenge

Imagine two users editing the same document. User A types "Hello" at position 0 while User B simultaneously deletes character 5. If we simply send these operations to a server and apply them in order, User B's delete might target the wrong character after User A's insertion shifts all positions. This is the concurrent editing problem.

Two Main Approaches

Operational Transformation (OT): Transforms operations based on concurrent changes. If User A inserts at position 2 and User B inserts at position 5, OT adjusts B's operation to position 6 to account for A's insertion. The server acts as the central authority, transforming and broadcasting operations to all clients.

Conflict-Free Replicated Data Types (CRDTs): Each character gets a unique, immutable identifier rather than a position index. When User A inserts "H" and User B inserts "i" concurrently, both changes carry globally unique IDs that allow automatic, deterministic merging without a central coordinator. CRDTs guarantee eventual consistency—all clients converge to the same state.

Real-Time Delivery

Changes propagate via WebSocket connections, pushing updates to all active collaborators instantly. The paste system needs to track active sessions, broadcast edit operations, and handle reconnection gracefully. Each operation includes metadata: user ID, timestamp, and the transformation logic.

Key Takeaway: Collaborative editing requires conflict resolution algorithms (OT or CRDTs) and real-time communication channels to synchronize concurrent changes across multiple users without data loss or corruption.