Repository navigation
perf(serving): patch content comes from memory and goes out compressed - #770
Merged
Merged
Conversation
Every scripted patch load read its document from object storage after admission (p50 122 ms in production) and sent it uncompressed (296 KB for a typical tier 2 patch). Now the content route keeps admitted documents in memory, keyed by object key and recorded digest, within new serving.contentCache.* limits, and sends brotli or gzip by Accept-Encoding with Vary. Admission and every refusal still run on every request. Compression runs on the threadpool, so a miss never blocks the event loop. #764's bundle cache becomes the shared Content.makeArtifactCache helper. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review round 1 on #770: cold misses for different documents could start brotli and gzip together with no host-wide limit, and a load whose waiter stopped left native zlib running while a retry started the work again. - The artifact cache's misses now run as jobs in the host's scope: a miss starts one, simultaneous misses and retries join it, and it finishes and keeps its result even when every waiter stops. A job not settled in 30 seconds fails, so a hung read can't hold its identity. This replaces the zero-lifetime Effect Cache, which dropped and interrupted a lookup once its last waiter left. - The host compresses one document at a time, codings one after another, holding the permit until zlib finishes. Reads stay concurrent. - Tests: different cold documents compress one at a time; an abandoned load's retry does one read and one compression; kept documents are charged all three codings; HEAD carries the compressed length. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review round 2 on #770. - Documents waiting for the compression permit had already been read, so a burst of distinct cold documents could hold any number of them. A host now reads and prepares at most four documents at a time, from the read through keeping them; the rest wait without bytes, the wait counting against the job's deadline. - A job forked into a host scope that closed before it ran never installed its onExit, stranding its loads and identity. Jobs now settle from their fiber's exit observer, which sees every exit. - The helper's comment says the deadline stops the read, that native compression finishes first, and that a timed-out job keeps nothing. - Tests: a burst of six abandoned cold loads prepares four at a time and still keeps all six; a host closing before its job runs leaves no load waiting. Co-Authored-By: Claude Opus 5.5 <[email protected]>
allisonmahmood
added a commit
that referenced
this pull request
Oct 19, 2026
…ng Reconnecting (#783) Release 14's What's new entry, written from \`7694bab\`. It covers everything merged since release 13's \`through\` (\`c9b8fc4\`), and sets the \`patchy\` release to \`0.14.0\` (\`packages/patchy/package.json\` and \`CURRENT_RELEASE\`). | Change | PRs | | --- | --- | | Improved: Tools with server code answer faster | #756, #764, #768, #769, #771, #772, #773, #775, #778 | | Improved: Your company's tools and pages open sooner | #759, #761, #766, #770 | | Fixed: Tools stop flashing Reconnecting while they load | #755, #776, #780 | | Improved: Publishing is quicker | #729 | | Improved: New tools start with a tidy layout | #763 | | Improved: Your usual is easier to scan | #721 | | Fixed: A page that isn't there says what to do | #762 | | Fixed: A logo Patchy can't read is refused, not a crash | #728 | | Behind the scenes: richer logs for live data | #758 | #720 is release 13's own entry, so it's left out. Made by Claude Opus 5.5 via Claude Code. Co-authored-by: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every load of a scripted patch fetches its document from
/~content/:patchId/:versionId. The server let the viewer in, then read the version's HTML from object storage: p50 122 ms in production, a large part of the content's 228–375 ms time to first byte. The body then went out uncompressed: 296 KB for a typical tier 2 patch.Now, once the door has let the viewer in, the document comes from the host's memory, and it goes out compressed with brotli or gzip by
Accept-Encoding, withVary: Accept-Encoding. The door, retention, company, lifecycle and wire checks still run on every request, cache hits included. Only the storage read moves to memory. Browser caching is unchanged:private, no-storebehind the door, and a public patch's current version keepspublic, max-age=60.Measured
On the local instance (
pnpm dev up brightline), each patch was opened in Chromium through Playwright as Allison, 11 loads per run after a server restart. The throttled runs used CDP's 100 ms latency and 20 Mbps down, as in #761. The figures are medians of loads 2–11, plus 5 separate cold loads, each the first load after a restart. Wire bytes include response headers.I counted storage reads with
inotifywaiton the stored document files, and they match the newcontentLoadfield: oneread, thencached. The local store is a filesystem, about a millisecond away, so the warm gain here is small.Expected in production: every load after a host's first read of a version skips the 122 ms p50 storage read, and a typical 296 KB document is about 120 KB with brotli. These documents compress 2.4–2.5× (234,517 → 95,556, 213,495 → 89,847, 172,465 → 83,803 bytes), less than the broker's 3.3×. With TLS and TCP slow start, cutting 175 KB saves more than the 57 ms it saved locally. Each host keeps its own copies, so after a deploy every host reads each version once. That first load pays the read plus compression, about 11 ms here and likely two to three times that on a 0.5 vCPU host; see below.
New Business and Kudos both rendered fully as their personas, with compression on and no browser errors. With
curl,br,gzipand identity each decode to main's exact bytes.HEADcarriesContent-Encoding: br,Varyand the brContent-Length. A signed-out request gets its 401 door with noContent-Encodingand noVary.Design
One cache helper for bundles and documents. I generalized #764's bundle cache into
Content.makeArtifactCache. It holds the least-recently-used map with its byte and entry bounds, one job per miss, and thecached/read/joinedlabels.onExitmissed (round 2).Cache(round 1). This replaces the zero-lifetime EffectCachefrom perf(patches): tier 2 calls reuse their server bundle instead of re-reading it #764. ThatCacheremoves a lookup and interrupts it as soon as its last waiter leaves. A retry then started a second read, while the first one's native zlib work kept running.Content, because its compressed bodies come fromcompression.tsin Serving.Content.readstill does the read.Compression is bounded and never on the event loop. Synchronous brotli 9 plus gzip on these documents blocks for 25–31 ms (234 KB: 22 + 9 ms).
compressnow runs on libuv's threadpool; the event loop's longest gap during it was 2–4 ms in the same measurement.negotiateand the response headers.compressonce at startup, and the synchronousprecompressis gone, socompression.tshas one compressor and its header comment names both users.Memory. I added two host-scoped operating limits, following #764's precedent:
serving.contentCache.bytes: 64 MiB, counting the raw, brotli and gzip bytes together.serving.contentCache.entries: 256.A typical 296 KB document holds about 550 KB with both codings, so a host keeps about 120 documents. A document whose stored size is over a quarter of the byte bound (16 MiB) is never kept or compressed. The largest scripted document a publish accepts is 10 MiB, so that only happens if the bound is tuned down.
Peak memory per host is 64 MiB of kept documents beside #764's 64 MiB of bundles, on a 2 GiB task. On top of that come the documents being prepared: at most four, each holding its decoded string (up to twice its size for non-Latin text), its bytes and its compressed outputs: four to five times its size. Worst case that's about 200 MiB with today's 10 MiB publish cap; with typical 300 KB documents it's about 6 MB.
Every other cold load waits without bytes. A document too large to keep goes through the same four places, then its body is held by the requests sending it, as on main. A compressed body under 16 KiB can keep zlib's 16 KiB output chunk alive, which is bounded by the entry count.
What's cached is the same for every viewer. The body is the stored bytes verbatim. The document nonce stays in the URL, and the server never reads it. Everything else is computed per request from the version row and the request, as before: the CSP from the tier only,
Permissions-Policy,Cache-Control, cookies and the newVaryandContent-Encoding. No header depends on the body.The key comes from the version row the door admitted,
objectKey#contentHash, never from the URL. The object key contains the version id, so a viewer can only ever get bytes of a version they were just admitted to. There's no ETag or 304 path. Refusals return before the cache is touched, so they are never cached, compressed or varied.Integrity. A miss checks the bytes against the version's recorded
contentHash, as #764 does for bundles. A mismatch fails asStoredDocumentMismatch, a fault answered with a 500 like a missing object, and is never kept.Scope. Only
/~content. A tier 0 page embeds its HTML in the shell'ssrcdocthroughContent.readand never fetches its content URL. Moving that page onto the cache would need extra code, so it isn't here. A tier 0 version's own content URL goes through the same route, so it is cached too.Observability. A content request's wide event adds
contentLoad(cached/read/joined) andcontentEncoding(br/gzip/identity).responseBytesis now the compressed size. Both fields are in the analytics glossary and OPERATIONS' wide events section.Tests
Pages.test.tsbuilds one host's routes over a store that counts document reads, with each request inside a request event:Three loads read storage once:
readthencached, inbr,gzipand identity.Two simultaneous loads share one read, labelled
readandjoined. A failed read and a digest mismatch are faults, and the next request reads again.With the document kept, a signed-out reader gets 401, another company 404, a deactivated member 403, and an operator-disabled patch 404. None of these is compressed or varied, and the read count stays at 1.
Accept-Encodingpicks br, gzip or identity, including weights, withVary, and each body decodes to the stored bytes.A document over a quarter of the byte bound is sent as stored and read on every load. The entry bound evicts the least recently used document.
Round 1:
joinedand the next loadcached.HEADcarriesContent-Encoding: br,Varyand the brotli body'sContent-Length, with no body.Round 2:
Content.test.ts:The existing UTF-8 and BOM test now records each rewritten document's digest, because stored bytes never change once published.
Round 2:
pnpm check(5/5, 1,923 tests) andpnpm verify(9/9, including tier2-smoke, cli-smoke and browser) both passed in full.Round 1:
pnpm checkpassed format, lint, synth and all 1,890 tests. Typecheck failed on one type annotation in the new test helper; I fixed it, andpnpm typecheckthen passed on its own. OutsidePages.ts,compression.ts,Content.tsand their tests, round 1 only changes the analytics glossary and comments, so I didn't runpnpm verifyagain.pnpm verify(before round 1): 8 of 9 steps passed, including tier2-smoke, cli-smoke and browser. Theteststep had one failure, a 5 s timeout inpackages/sdk/src/SdkApi.test.ts("retains advertised archives after a same-version repack prunes local copies"), while the machine's load average was about 13. That file passed 3 of 3 runs alone, and the full suite passed inpnpm checkan hour earlier. It packs and prunes SDK archives and touches nothing here.Closes #746
Made by Claude Opus 5.5 via Claude Code; reviewed with GPT-6.1-Sol.