Skip to content

fix: improve Cloudflare cache hit rate#1373

Open
crowlKats wants to merge 2 commits intomainfrom
cf-cache
Open

fix: improve Cloudflare cache hit rate#1373
crowlKats wants to merge 2 commits intomainfrom
cf-cache

Conversation

@crowlKats
Copy link
Copy Markdown
Collaborator

Summary

  • Use URL-only cache keys in cachedFetch: The previous implementation used the full request (including per-client headers like Cookie, CF-Connecting-IP, X-Forwarded-For) as the cache key, making every request effectively unique. Now uses a stripped URL-only GET key, matching the pattern already used by proxyToR2.
  • Only set Vary: Cookie, Authorization on non-cacheable responses: Previously set on ALL responses including cacheable anonymous ones, which fragmented the cache since every unique cookie/auth value created a separate cache entry.
  • Additional safety checks: Only cache GET/HEAD requests, respect backend Cache-Control directives (private/no-store), only cache 2xx responses, serve HEAD from cached GET.

Closes #1371

Test plan

  • Added 9 new tests covering: URL-only cache key, cache reuse across clients, auth bypass, cookie auth bypass, POST bypass, private Cache-Control bypass, non-2xx bypass, HEAD-from-cache, login path bypass
  • All 14 tests pass (5 existing R2 + 9 new Cloud Run)
  • deno check lb/main.ts passes

🤖 Generated with Claude Code

crowlKats and others added 2 commits April 4, 2026 14:49
…hit rate

The Cloudflare cached request rate was ~3.3% due to two issues:

1. cachedFetch used the full request (including per-client headers like
   Cookie, CF-Connecting-IP, X-Forwarded-For) as the cache key, making
   every request effectively unique. Now uses a URL-only GET cache key,
   matching the pattern already used by proxyToR2.

2. Vary: Cookie,Authorization was set on ALL responses including
   cacheable anonymous ones, fragmenting the cache. Now only set on
   non-cacheable (authenticated/mutating) responses.

Additional improvements:
- Only cache GET/HEAD requests (skip POST/PUT/DELETE)
- Respect backend Cache-Control (don't cache private/no-store responses)
- Only cache 2xx responses
- Serve HEAD requests from cached GET responses

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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