KatInterview

Build the Backend for a Multi-User Live Text Editor

System DesignModel answer
MediumModel answerSystem Designsystem-designreal-time-collaborationoperational-transformation

Last updated

Design a backend that lets many people edit one text document at once, keeps every replica converged, persists edits with history, and recovers cleanly from disconnects and server crashes.

You are asked to design the server side of a shared online word processor. Multiple people can open the same document together, type at the same time, and see one another's changes and cursor positions within a short delay. When everyone stops typing, all copies must show identical text, and the document must be stored durably with a browsable revision history.

The discussion should go deep on live collaboration and the backend architecture behind it. Account management, document creation and sharing settings are secondary and need only a short mention.

Assumptions

  • The document is plain text. Formatting can be treated as attributes attached to ranges of characters, so the core design can reason about a sequence of characters.
  • A user's own keystrokes must show on their screen immediately, without waiting for a server round trip.

Questions you should settle (or state assumptions for)

  • How many people edit one document at the same time, typically and at the extreme? How many documents are open concurrently?
  • What delay is acceptable before a collaborator sees an edit?
  • Must editing work offline, with changes merged when the user reconnects?
  • Are comments, suggestions and revision history in scope?
  • If a server crashes, may a user lose the last instant of typing, or must every acknowledged edit survive?

What to cover

  1. The concurrency-control model, with a worked example showing two conflicting edits converging.
  2. Where each document's live state lives, how clients reach it, and how a single order of edits is established.
  3. What is persisted per edit, how a long-lived document still opens quickly, and how history is served.
  4. Behavior on reconnect, duplicate or missing edits, and a crash of the server holding the live state.
  5. Scaling across many documents and for one document with many collaborators.
  6. Presence and cursors, plus access checks on connect and on each edit (brief).

Hints

Hint 1

Start from the conflict: two users insert at the same index at the same time. If each applies the other's raw edit, the copies diverge. Decide whether you will transform operations against each other (OT) or give characters stable identities (CRDT), and pick one.

Hint 2

Make one server the single owner of each document at a time, route all clients of that document to it, and let it stamp edits with a monotonically increasing revision number. A total order makes the transform logic and recovery much simpler.

Hint 3

Treat the persisted operation log as the source of truth, and periodically write snapshots so opening a document means loading the latest snapshot plus a short tail of operations. Keep cursors and presence in an ephemeral channel that never touches the log.