Skip to content

fix(api): serve read-cache hits without raising through the app - #2006

Merged
phernandez merged 1 commit into
mainfrom
read-cache-hit-no-exception
Nov 1, 2026
Merged

phernandez merged 1 commit into
mainfrom
read-cache-hit-no-exception

Conversation

@phernandez

Copy link
Copy Markdown
Member

Why

fastapi-redis-sdk (0.8.1) signals a cache hit by raising CacheHitException from its cache() dependency, and an exception handler turns it into the cached response. The raise happens while FastAPI solves dependencies, and Logfire's FastAPI instrumentation records any exception that leaves dependency solving on the request span. So every read-cache hit was recorded as an exception. In Basic Memory Cloud production that is about 76k spans a day with exception_type=redis_fastapi.cache.CacheHitException on GET knowledge/entities/{id}, directory/structure, directory/list, directory/tree, QUERY knowledge/resolve, search and memory/recent.

A cache hit is not an error. The earlier change that raised the hit after the read_cache.read_through span closed kept it off that span, but not off the request span.

The SDK has no non-exception hit mode: a FastAPI dependency cannot return a response, so the only way out of dependency solving is to raise.

What

  • New ReadCacheRoute (an APIRoute subclass) in read_cache/routes.py. For a route with a cached_read dependency, its handler runs the SDK lookup before FastAPI solves dependencies. On a hit it catches CacheHitException right there and returns the cached response. On a miss it runs the normal handler and the SDK capture middleware stores the response as before.
  • cached_read(...) is still declared as dependencies=[cached_read(...)]. It now carries the route's cache settings, and when FastAPI calls it, it only checks that ReadCacheRoute served the request. It raises TypeError if a router forgot route_class=ReadCacheRoute, so a route cannot silently run uncached.
  • The four routers with cached reads (knowledge, directory, search, memory) use route_class=ReadCacheRoute.
  • Unchanged: keys and versions, QUERY support, hit headers, ETag/304, X-Redis-Cache, capture, entry tracking, invalidation, and the read_cache.read_through span with cache.lookup.outcome. A hit now also returns before any dependency runs at all, not just before database dependencies.
  • Dev dependency logfire becomes logfire[fastapi] so tests can instrument the app the way production does (adds opentelemetry-instrumentation-fastapi to the dev lock only).

No monkeypatching of the SDK. The SDK's exception handler is still registered by add_redis_caching but is no longer reached.

Testing

  • New test_a_cache_hit_is_not_recorded_as_an_exception_on_the_request_span (test-int, real Redis): instruments the app with logfire.instrument_fastapi, does a miss then a hit, checks the hit returns the cached body and no span carries an exception event.
    • Load-bearing: with the src/ change reverted, it fails with ['redis_fastapi.cache.CacheHitException'] == []. With the fix it passes.
  • New tests/test_read_cache_route_class.py: a cached_read route works under ReadCacheRoute and fails fast without it.
  • tests/test_read_cache_*.py and test-int/read_cache/: 102 passed.
  • tests/api/v2: 460 passed, 1 skipped.
  • just fast-check (ruff, format, ty) clean; pyright clean on touched files.

🤖 Generated with Claude Code

https://claude.ai/code/session_016N9c6ouiT4Wid3arGR41Dv

fastapi-redis-sdk signals a cache hit by raising CacheHitException from
its dependency. Raised while FastAPI solves dependencies, Logfire's
FastAPI instrumentation records it on the request span, so every cache
hit showed up as an exception (about 76k spans a day in Cloud).

ReadCacheRoute now runs the SDK lookup in the route handler, before
dependency solving, catches the hit there and returns the cached
response. cached_read stays the route-level marker and fails fast if
its router does not use ReadCacheRoute. Keys, ETag/304, hit headers,
capture, invalidation and the read_cache.read_through span are
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016N9c6ouiT4Wid3arGR41Dv
Signed-off-by: phernandez <paul@basicmachines.co>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-01T15:41:58.589048Z 07cf9b8 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@phernandez
phernandez merged commit 9cdd688 into main Nov 1, 2026
52 checks passed
@phernandez
phernandez deleted the read-cache-hit-no-exception branch November 1, 2026 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant