skip to content

A two-year-old team wiki page opens slowly because the browser replays its whole operation log; how would snapshots fix that, and what happens to a laptop offline longer than the log is kept?

level: seniorimportance: should knowfreq 30%

answer

  1. load state, then the tail
  2. readers rarely need the editor
  3. compact on a cadence
  4. retention is a promise to offline users
  5. base too old: keep the user's edits

basics

~20 s

Open from a recent snapshot plus only the operations after it, cache that snapshot locally, and compact the log on a schedule. A laptop whose base predates the kept log cannot merge automatically: keep its edits and offer a guided reapply.

solid answer

~50 s

Stop replaying history on open. The server writes a snapshot — the document state at a known version — every so many operations or after an editing session, and the browser fetches the latest snapshot plus only the operations after it, then joins the live stream. The local replica keeps that snapshot and any unsynced edits, so reopening starts at once; readers who never edit get rendered content and load the editor on demand. Operations older than a retention window are dropped, and the history panel shows named checkpoints rather than raw operations. A laptop whose base version predates the retained log cannot merge automatically: an OT server cannot transform across operations it no longer has, and a CRDT cannot place operations whose identities were compacted away. The server answers 'base too old'; the client loads the new snapshot, keeps its edits, reapplies what still fits and lists the rest. Size the window to the offline promise.

code

http · 9 lines
http
POST /pages/481/ops HTTP/1.1
Content-Type: application/json

{ "baseVersion": 12040, "ops": [ ... ] }

HTTP/1.1 409 Conflict
Content-Type: application/json

{ "error": "base_too_old", "oldestRetained": 15800, "snapshotVersion": 18200 }

go deeper

for a junior

Recall that a snapshot saves the document's state at a version, so the editor does not replay every past edit when it opens.

for a middle

Explain the open sequence — latest snapshot, then the tail of operations, then the live stream — and what the local replica keeps for a fast reopen.

for a senior

Handle the client whose base is older than the retained log: refuse the merge, keep its edits, reapply what fits and show the rest, never dropping work.

for a principal

Set the retention window as a product promise tied to the offline limit, weighing server storage against how often real clients return late.

## Why opening is slow A **team wiki page** edited for two years has accumulated hundreds of thousands of small operations — every batch of keystrokes, every formatting change. If the browser opens the page by downloading that log and replaying it onto an empty document, load time grows with the page's age rather than its size. With a sequence CRDT it is heavier still: the replica also carries identities for characters typed long ago and history the merge may still need. The authors of a well-known study of CRDT-based local-first prototypes reported exactly this: performance and memory or disk use became a problem because character-by-character history piles up and cannot easily be truncated. ## Snapshot plus tail A **snapshot** is the document's state at a known version, written by the server or a background job so that nobody has to replay what came before it. 1. The server writes a new snapshot every so many operations, or when a page goes quiet after an editing session. 2. On open, the browser fetches the **latest snapshot plus the operations after it** (the tail), applies the tail and joins the live stream. 3. The browser keeps that snapshot and any unsynced operations in its local replica, so reopening starts from local state and fetches only the newer tail. 4. Readers who are not editing need no editor at all: serve the page's rendered content, and load the collaborative machinery when someone starts editing. Write snapshots off the editing path — a background job reading the log — so a busy page's editors never wait on one. The same idea keeps the browser's own replica small: when it grows, the page can replace its stored history with the latest snapshot plus its own unsynced operations. For a CRDT, the snapshot is the compacted replica, not just the text — it must keep whatever identity information later operations still refer to, or they cannot be applied. What the merge can safely forget belongs to the question on CRDT costs at scale; this design decides when snapshots are written and what the client loads. ## Compaction and the history panel - Keep operations newer than a **retention window**; older ones can be dropped once a snapshot covers them. - Build the version history panel from **named checkpoints** (per editing session or per hour), not raw operations: people want "yesterday's version", not keystroke 41,822. - Keep the two dials separate: snapshot cadence bounds load time, while the retention window bounds how far back an offline client can still merge. ## The laptop that was offline too long A client reconnects with operations based on a version older than anything the server still keeps. | Merge approach | What goes wrong | |---|---| | OT with a central server | the client's operations cannot be transformed without the operations in between, so the server must refuse them | | Sequence CRDT | merging still works if the identities the operations name still exist; if compaction dropped them, the operations cannot be placed | The fallback must never drop work silently: 1. The server answers "base too old" and names the current snapshot. 2. The client loads that snapshot but **keeps its offline edits**: it can work out what it changed, block by block, by comparing its local state with its own base. 3. Changes to blocks that still exist and that nobody else touched are reapplied as new operations, automatically or after a quick review. 4. The rest go to a side panel ("Your offline changes from 3 March") with copy and reapply actions. ## Choosing the window The retention window is a promise: it decides how long someone may stay offline and still merge automatically. Nobody can know when a laptop will come back — someone may reconnect after six months away — so tie the window to the product's stated offline limit, measure how long real clients actually stay away, and make the "too old" path good enough that the rare case is an inconvenience rather than a loss. Server storage for operations is the price of a longer window; snapshots keep that price from also landing on page load.

  • How often should the server write a snapshot?
    Often enough that the tail a client replays stays small — every few thousand operations, or when an editing session goes quiet — but not on every edit, since each snapshot costs a full serialisation and storage. Measure open time against tail length and tune the threshold instead of guessing.
  • Does version history break when old operations are dropped?
    Not if history is built from checkpoints: keep named snapshots per session or per hour for as long as the product promises history, and drop only the fine-grained operations between them. People browse versions, not keystrokes.

  • Replay the full operation log on every open, since only that gives the exact state.
  • Truncate the log whenever it grows large; returning clients will catch up somehow.
  • A client whose base is too old should overwrite the page with its own copy.
  • Silently drop an offline user's edits when their base version is no longer kept.
  • Snapshot cadence and retention window are the same setting.