KatInterview

Design a Real-Time Collaborative Document Editing Service

System DesignModel answer
HardModel answerSystem Designreal-timecollaborationwebsockets

Last updated

Design the backend for a multi-user document editor supporting concurrent edits, offline sync, conflict resolution, history, and per-note scaling.

Scenario

You are building the backend for a lightweight collaborative writing tool — think shared notes that multiple teammates can edit at the same time from different devices or locations.

Your focus is entirely on backend architecture and data flow. Assume the frontend is handled separately.

Functional Requirements

  • Users can create, read, update, delete, and share documents.
  • Multiple users may have a document open and actively typing at the same time.
  • A user who loses connectivity briefly should be able to resume editing and have their changes merged cleanly when they reconnect.
  • Users can view previous versions of a document and recover from unwanted changes.
  • Documents can be shared with specific people or teams, with different permission levels.

Constraints and Assumptions

  • Plain text or simple rich text is sufficient; you do not need to support spreadsheet-like structures.
  • Assume tens of millions of documents and thousands of concurrent live sessions at peak.
  • History retention and version recovery are required, but you can define a reasonable retention window.
  • A simple last-write-wins approach at the full-document level is explicitly off the table — it loses edits under concurrent use.

Clarifying Questions to Consider

Before diving in, think about how you would clarify scope with an interviewer:

  • Is rich-text formatting (bold, links, embedded media) required in v1, or just plain text?
  • What collaboration latency is acceptable — under 500 ms, or closer to a few seconds?
  • Do we need true offline editing (edits made with no connectivity, merged later), or just short reconnect tolerance?
  • How long should version history be retained — 30 days, 90 days, indefinitely?
  • Are permissions granted per user, per team, or via shareable links?

What to Cover

Part 1 — Architecture and Data Model

Describe the major services, how they interact, and the key data entities.

  • What services exist and what does each own?
  • How are documents, users, permissions, edit operations, and snapshots represented in storage?
  • What APIs expose CRUD operations, and what protocol handles live editing?

Part 2 — Synchronization and Conflict Resolution

Explain how the system keeps all clients consistent when edits happen simultaneously or when a client reconnects after going offline.

  • How are individual edits tracked and ordered?
  • What strategy — Operational Transformation (OT) or CRDTs — would you use, and why?
  • How does a reconnecting client catch up to the current document state?
  • What makes full-document overwrites dangerous here, and how do you avoid it?

Part 3 — Scalability, Reliability, and Security

Describe how the system handles growth and protects data.

  • How do you route and scale the real-time collaboration layer?
  • How do you ensure an edit is not lost if a server crashes right after receiving it?
  • How does pub-sub fit into the architecture?
  • How do you manage history storage without it growing unbounded?
  • What security controls (permissions, encryption, rate limiting, audit trails) are needed?

Hints

Hint 1

Start by separating concerns into at least three distinct services: one for document metadata and CRUD, one for managing live editing sessions and WebSocket connections, and one (or a component) for persistence of the operation log. Keeping these boundaries clear makes the rest of the design much easier to reason about.

Hint 2

For conflict resolution, compare OT and CRDTs on two axes: implementation complexity and suitability for your text model. OT requires a central server to sequence operations and transform them, which is simpler to reason about for plain text but adds a coordination bottleneck. CRDTs push the merge logic into the data structure itself and work better for fully peer-to-peer or heavily offline scenar…

Hint 3

Think about durability in two layers: the append-only operation log (every keystroke-level change, ordered by document version) and periodic snapshots (a full document state at a checkpoint). A client catching up after an offline gap replays the operation log from the last snapshot it holds — this is cheaper than replaying from the beginning of time.