# Design Collaborative Document Editing — High-Level & Low-Level Design Interview Problems

Source: https://www.skillbyai.com/en/design-interviews/collab-editing

> Operational transformation versus CRDTs.

## Keeping concurrent edits consistent

Several users edit the same document in real time and must converge on the same content. Clients apply edits locally for instant feedback and send **operations** (insert or delete at a position) over a WebSocket to a collaboration service. Two families of algorithms make concurrent edits converge. **Operational transformation (OT)** transforms an incoming operation against concurrent ones (if someone inserted earlier in the text, shift my position); it is typically run with a central server that orders operations. **CRDTs** (conflict-free replicated data types) give each character a unique, ordered identifier so replicas can merge in any order and still converge, which suits peer-to-peer and offline use at the cost of extra metadata. Around that: document sessions pinned to one server per document, an operation log plus periodic snapshots, presence (cursors) as ephemeral data, and version history.

## Collaboration, scheduling and observability

These problems test real-time sync, reliable background work and high-volume data pipelines.

![Three problems: collaborative editing, a distributed job scheduler and a metrics and logging pipeline.](assets/figures/design-interviews/section-7-map.svg) — Figure 7.1 — Collaborative sync, job scheduling and telemetry pipeline.

## Why transformation is needed

Two concurrent inserts on "abc".

```text
Start: "abc"
Alice: insert("X", pos=0)     -> "Xabc"
Bob:   insert("Y", pos=3)     -> "abcY"   (concurrent, based on "abc")

Without transformation, Alice applies Bob op at pos 3:
  "Xabc" -> "XabYc"   (wrong)
With OT, Bob op is transformed past Alice insert at pos 0 -> pos 4:
  "Xabc" -> "XabcY"   (both converge)

Architecture
clients <-> WebSocket gateway <-> doc session (one owner per doc id)
          session: order ops, transform/merge, append to op log
          storage: op log + snapshot every N ops; presence in memory
```

## Directions that adjust

If a friend inserts a new street before yours on a shared map, "third left" becomes "fourth left". OT rewrites your instruction to account for their change.

**Quiz:** Which property makes CRDTs attractive for offline editing?

- [ ] They forbid concurrent edits
- [ ] They require a single central lock
- [ ] They never store metadata
- [x] Replicas can merge operations in any order and still converge

*Answer:* Replicas can merge operations in any order and still converge. Convergence without central ordering.
