Repository navigation
Description
Summary
The read cache almost never hits, because every write clears every cached read in the project, and entries expire after 5 minutes anyway. A note that has not changed should stay cached until it changes.
Production, 2026-10-10 (Basic Memory Cloud, Redis read cache):
| Cached read | Lookups | Hit rate |
|---|---|---|
entity (GET /v2/.../knowledge/entities/{id}, used by read_note and the web note view) |
~37K/day, ~48 KB each | 1.4% |
search |
~7.8K/day | 0.1% |
resolve (name or permalink to id, the first step of read_note) |
~40K/day | 22% |
Every miss also writes the full payload back to Redis, so we pay a Redis round trip plus a ~48 KB write per note read for almost no hits.
Why it misses
Tracing each entity lookup to the note it fetched (6 hours, ~11K reads):
| Why it missed | Share |
|---|---|
| First read of that note in the window | 37% |
| Re-read more than 5 minutes later (TTL expired) | 37% |
| Re-read within 5 minutes, but a write elsewhere in the project bumped the generation | 24% |
| Re-read within 5 minutes, same generation | 2% (these mostly hit) |
63% of note reads are re-reads of a note already read that day. Gaps: 41% within 5 minutes, 11% at 5 to 15 minutes, 30% at 15 to 60 minutes, 17% at 1 to 3 hours.
The cache works when its conditions hold. The conditions almost never hold:
- One generation per project.
RedisReadCache.invalidate_projectbumps a single generation key. Every cached read in the project (notes, resolve, search, directory listings) is keyed under it, so any write anywhere hides all of them. - Bumped more than once per edit. The write path bumps it (
services/note_content_writes.py), then indexing bumps it again when it finishes. With agents writing all day, the generation churns constantly. - Short TTLs.
READ_CACHE_TTL_SECONDS = 300,SEARCH_READ_CACHE_TTL_SECONDS = 1800(read_cache/policy.py). With explicit invalidation, the TTL only needs to bound memory, not freshness.
What actually changes each cached read
| Cached read | Changes when |
|---|---|
Note (entity) |
That note is written. One of its relations is re-resolved (a forward reference finds its target). A note it links to is renamed, moved or deleted (the response embeds relation permalinks). |
resolve (name to id) |
A note is created, renamed, moved, deleted, or its title changes. Not on body edits. |
| Directory listings | A note is created, moved, renamed or deleted. Not on body edits. |
search |
Any indexed content changes. |
So a body edit to note A should invalidate note A and search, and nothing else.
Proposal
1. Split the single generation into three scopes
- Per-note version
note:{entity_id}: bumped when that note is written, whenresolve_relationschanges its relations, and when a note it links to is renamed, moved or deleted. The relation index already knows the incoming links, so the affected notes are known. - Project structure generation: bumped on create, rename, move, delete and title change. Keys
resolveand directory listings. - Project content generation: bumped once per edit after indexing finishes. Keys
searchonly.
Cache keys:
- note:
(structure_gen, note_version, entity_id) - resolve, directory:
(structure_gen, request) - search:
(content_gen, request)
Lookup still takes one Redis round trip: read the needed version keys and the data key in the existing Lua script.
2. Bump once per change, at the right time
- The write path bumps the note version (and structure if the title or path changed).
- Indexing bumps the content generation once when the note's search rows are current. It does not bump note or structure versions again unless relations changed.
3. Long TTLs
- Notes, resolve and directory listings: 7 days. Search: 24 hours.
- The TTL becomes a memory bound only. Rely on Redis
maxmemorywith an LRU policy for eviction. Check the Redis instance size and eviction policy before rollout.
4. Measure it
- Keep one span per lookup, and add the scope that missed (
note_version,structure,content,expired,absent) as an attribute, so a regression shows which scope churns. - Add the hashed request digest to the span (never query text) so we can measure exact search repeats.
Expected effect
- Note reads: up to about half could hit (the share of re-reads within an hour or so that are not right after editing that same note). The real number is lower because agents often read, edit, then read the same note.
resolveand directory listings should hit most of the time, because body edits stop clearing them.- Faster
read_note, and far fewer large Redis writes.
Tests
- A body edit to note A leaves note B,
resolveand directory entries cached, and misses only note A and search. - Renaming note B invalidates note A when A links to B.
- A new note that resolves A's forward reference invalidates A.
- Delete and move invalidate structure.
- Run each at the API layer, against Redis, not against a mocked cache.
Related
- Cloud companion issue covers the gateway proxy cache and the worker-side invalidations.
Activity
claude commentedon Oct 11, 2026
Claude finished @phernandez's task in 17s —— View job
Triage
buglabel is the author's, so I left it alone.read_cache/policy.py,services/note_content_writes.py, the indexing path, and relation resolution).Notes for whoever picks this up
SELECT ... FOR UPDATEor wider transactions. Convergence on the next write, reindex or sweeper pass is acceptable.entitypayload embeds relation permalinks, so the incoming-link fan-out on rename, move and delete is the riskiest part. A missed fan-out would leave a note's links stale for up to 7 days under the proposed TTL, and nothing else repairs it. Test that case explicitly.maxmemoryand eviction policy to be checked before rollout. That sits with the cloud companion issue.No code changes were made as part of triage.