Collaborative editor
Many people edit one document at the same time, from browsers and phones, some on poor connections. Everyone converges on the same document, edits made while offline are not lost, and cursors move in real time.
Envelope50 concurrent editors per document · sub-100 ms echo · no lost edits
What the design must do
- Edits pushed to every participant without polling
- Concurrent edits merge deterministically instead of overwriting
- The ordered history of operations is the source of truth; the document is derived
- A client reconnecting after an hour offline catches up without a full reload
The worked envelope
Assumed. 50 concurrent editors on a hot document, ~5 operations per second each while actively typing, 100k documents open at once.
| Ops per document | 50 × 5 = 250 ops/s |
| Broadcast amplification | each op to 49 peers → 12,250 msg/s per document |
| Bandwidth per doc | 12,250 × 100 B ≈ 1.2 MB/s |
| Ops per busy day | 250/s × 8 h ≈ 7.2M ops = 720 MB |
| Naive open cost | replay 7.2M ops: seconds to minutes |
| Connections | 100k docs × 50 = 5M concurrent |
What the numbers decide. Fan-out is the multiplier that matters: 250 operations a second becomes twelve thousand messages a second, on one document. Everything about the transport is chosen by that line — it is why a document is a routing group rather than a set of independent connections, and why keeping the payload at a hundred bytes is worth real effort.
The second half is the open path. A busy document accumulates seven million operations in a day, and replaying them on every open is not a thing that can work. Snapshots bound the replay, and once you have snapshots you have a compaction problem, a versioning problem, and a "which snapshot was this client last at" problem — all of which arrive from that one row.
The decisions, and what each one costs
CRDT operations rather than server-ordered transforms
- Because
- Edits originate offline and out of order, and a structure where concurrent operations commute converges without the server having to be the arbiter of order
- The cost
- Metadata per character, which grows the document beyond its visible content and needs periodic compaction
- Rejected
- Operational transform, which is smaller on the wire and requires a central server to order every operation
Periodic snapshots, replay bounded to the tail
- Because
- 7.2M operations cannot be folded on open, and the cost otherwise grows for the lifetime of the document
- The cost
- Snapshots are derived state to store, version and invalidate; a bad snapshot silently corrupts every subsequent open
- Rejected
- Replay from operation one, which makes an actively used document progressively slower to open until it is unusable
The document is the routing unit; one fan-out group per document
- Because
- 12k messages/s per document means delivery must be a group broadcast rather than 49 individual sends decided per operation
- The cost
- A hot document is a hot partition, and it cannot be split across servers without a coordination layer
- Rejected
- Per-connection delivery decisions, which multiplies routing work by the square of the editor count
Local-first: apply immediately, reconcile after
- Because
- Sub-100 ms echo is shorter than a round trip for most users, so the local replica has to be authoritative for the typing experience
- The cost
- The screen can show state the server has not accepted, so rejection and rollback are visible and must be designed
- Rejected
- Server-confirmed edits, where every keystroke waits for a round trip and the product feels broken on a train
Components in the reference design
- Client
- Browser or mobile app
- Load Balancer
- L7 routing, TLS termination, health checks
- WS Gateway
- Holds persistent client connections
- App Service
- Stateless business logic
- Event Log
- Kafka-style, ordered, replayable
- Object Store
- S3-style blobs, media, backups
- Cache
- Redis / Memcached, sub-ms lookups
What not to draw
- Queue — A work queue delivers each message to one consumer and discards it. Every participant must see every operation, in order, and a reconnecting client must replay what it missed — that is an event log, and Lesson 6.04 is the difference.
The reference design
The document is a projection. Store the ordered stream of operations, not the current text. Current state is a fold over that stream, which is Lesson 7.04's event sourcing applied to a paragraph — and it is what makes late-arriving edits, undo, and "who changed this" fall out for free rather than being features.
Snapshots bound the replay. A document with four hundred thousand operations cannot be rebuilt from operation one on every open. Persist a periodic snapshot to object storage and replay only the tail. The snapshot is a cache of the log, so it may be wrong and must be rebuildable — never the source of truth.
Merging is the hard part, and the ladder matters. Last-write-wins on the document is silent data loss and the wrong answer for anything a human typed. Field-level merge handles structured documents cheaply. Concurrent edits to the same paragraph of free text are the genuine case for a sequence CRDT — the one place in this course where the answer really is a CRDT, because that is precisely what they were invented for.
Be honest about what convergence buys. A CRDT guarantees every replica reaches the same value. It does not guarantee that value is what any human wanted: two people rewriting the same sentence converge on a deterministic interleaving of both rewrites. Convergence is a safety property, not a usability one.
Connections are state, so the gateway is separate. A WebSocket tier holds the persistent connections and knows which sockets are on which document; the edit service stays stateless behind it. That separation is what lets you deploy the editing logic without disconnecting fifty people mid-sentence (Lesson 9.05).
Presence is disposable. Cursors and selections are high-frequency, worthless once stale, and must never touch durable storage. Keep them in the cache with a short TTL and let them be lost on reconnect — the failure mode is a cursor that vanishes for a second, which nobody reports.