0701. Doc workers: live follow with journal fallback, byte-accounted LRU
Status: Accepted (2026-11-27). Implements the worker half of 0021 and 0304.
Context
Doc workers hold full pages for queries. Answers must reflect recent edits (read-your-writes via minSeq), stay correct when the realtime tier restarts, and fit a hard memory cap per process.
Decision
- Live follow.
- Each cached page subscribes over realtime as a hidden read-only subscriber: one socket per file, at most 16 rooms per socket (the realtime per-socket limit), service token
{svc:'docworker', fid} minted per connect.
- It subscribes with
sinceSeq = doc.seq, so the tail streams before synced, then live BROADCASTs.
- On close, pages drop to non-live and the link reconnects with full-jitter backoff (1 s doubling to 30 s), resubscribing from each doc’s seq.
not_found, snapshot_stale or seq_ahead evicts the page. file_deleted or access_revoked evicts the whole file.
- Journal fallback.
- A page that is not live is caught up from
page_ops before answering, at most every 250 ms. The DB holds every acknowledged batch, because realtime ACKs only after commit.
- A seq gap on the live path queues the batch and triggers a catch-up; the queued batches are drained afterwards in seq order.
- Catch-up and live application go through one
applyBatch that skips seqs already applied.
- minSeq. Wait for the follower (<= 500 ms), then a journal catch-up, then wait for the rest of the 2 s budget. Still behind -> 503 +
Retry-After: 1.
- Memory.
- The cache accounts
doc.bytes() + change-ring bytes + reservations of loads in flight, and never lets the total pass LINORA_DOCWORKER_MAX_BYTES.
- Admission estimate is 2.5x the raw snapshot bytes: the doc is ~1.47x raw, and the raw buffer lives until decode ends. LRU pages are evicted for it before the load, the real size is measured after.
- Growth from live batches re-measures and evicts other pages. A page bigger than the cap answers 503.
- RSS above 1.5x cap + 256 MiB exits with code 75 so the supervisor restarts the process (heap fragmentation is not in
doc.bytes()).
Alternatives
- Journal polling only: no realtime dependency, but every query pays a DB round trip and staleness grows with the poll interval.
- Follow every page of every file: wastes sockets and memory on pages nobody queries.
- Count pages instead of bytes: page sizes vary by four orders of magnitude.
Consequences
- Queries on live pages need no DB access.
- Without realtime (or
LINORA_REALTIME_* unset) the tier is still correct, with <= 250 ms staleness for calls without minSeq (changes has no minSeq in the contract).
- After a load, RSS runs about 2.3x the cached
doc.bytes() (GC slack from the decode buffers). The RSS guard is sized for that.