Skip to content

Read cache: invalidate per note instead of per project, and use long TTLs #1747

Closed

Description

@phernandez

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:

  1. One generation per project. RedisReadCache.invalidate_project bumps 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.
  2. 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.
  3. 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, when resolve_relations changes 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 resolve and directory listings.
  • Project content generation: bumped once per edit after indexing finishes. Keys search only.

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 maxmemory with 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.
  • resolve and 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, resolve and 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

  1. added
    bug
    production
    on Oct 11, 2026
  2. claude commented on Oct 11, 2026

    @claude
    Contributor

    Claude finished @phernandez's task in 17s —— View job


    Triage

    • Type: Enhancement (performance). The cache works as designed but its invalidation is too coarse to hit. The existing bug label is the author's, so I left it alone.
    • Component: Cloud (Redis read cache), with changes in core write and index paths (read_cache/policy.py, services/note_content_writes.py, the indexing path, and relation resolution).
    • Priority: Medium. It's a production cost and latency problem (1.4% hit rate on ~37K note reads/day, each miss a ~48 KB Redis write). Nothing is broken or at risk of data loss.
    • Complexity: Complex. It splits one generation into three scopes: per-note version, structure generation and content generation. It also needs a one-round-trip Lua lookup, a fix for double bumps (write path and indexing), and incoming-link fan-out on rename, move and delete.
    • Duplicates: None found.

    Notes for whoever picks this up

    • This fits the repo's consistency model: all of this is derived state. Per-note invalidation must not add SELECT ... FOR UPDATE or wider transactions. Convergence on the next write, reindex or sweeper pass is acceptable.
    • The cached entity payload 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.
    • Raising TTLs to 7 days makes any missed invalidation long-lived. Roll out the per-note versioning before, or together with, the TTL increase.
    • The issue asks for Redis maxmemory and eviction policy to be checked before rollout. That sits with the cloud companion issue.

    No code changes were made as part of triage.

  3. added
    cloud
    enhancement
    and removed
    bug
    on Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cloudenhancementproduction

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions