[GH-ISSUE #439] perf(kv): reduce KV round-trips on cache hit via local tag cache #97

Closed
opened 2026-05-06 12:37:12 +02:00 by BreizhHardware · 0 comments

Originally created by @Divkix on GitHub (Mar 11, 2026).
Original GitHub issue: https://github.com/cloudflare/vinext/issues/439

Problem

When KVCacheHandler.get() gets a cache hit, it validates every tag by issuing one kv.get() per tag in parallel (Promise.all). For entries with 20 tags, that's 20 KV round-trips per cache hit. Same pattern in revalidateTag() — N parallel PUTs.

Solution

Add a local in-memory Map<string, { timestamp, fetchedAt }> with a 5-second TTL that caches tag invalidation timestamps. Within the TTL window, tag checks are served from memory with zero I/O. After TTL expiry, the next request re-fetches from KV.

Key behaviors:

  • revalidateTag() updates the local cache immediately so invalidations are reflected without waiting for TTL expiry
  • resetRequestCache() clears the local cache for per-request isolation
  • NaN tag timestamps are cached and correctly treated as invalidation
  • Only uncached/expired tags trigger KV reads (partial cache hits work)

Implementation

PR: #433

Originally created by @Divkix on GitHub (Mar 11, 2026). Original GitHub issue: https://github.com/cloudflare/vinext/issues/439 ## Problem When `KVCacheHandler.get()` gets a cache hit, it validates every tag by issuing one `kv.get()` per tag in parallel (`Promise.all`). For entries with 20 tags, that's 20 KV round-trips per cache hit. Same pattern in `revalidateTag()` — N parallel PUTs. ## Solution Add a local in-memory `Map<string, { timestamp, fetchedAt }>` with a 5-second TTL that caches tag invalidation timestamps. Within the TTL window, tag checks are served from memory with zero I/O. After TTL expiry, the next request re-fetches from KV. Key behaviors: - `revalidateTag()` updates the local cache immediately so invalidations are reflected without waiting for TTL expiry - `resetRequestCache()` clears the local cache for per-request isolation - NaN tag timestamps are cached and correctly treated as invalidation - Only uncached/expired tags trigger KV reads (partial cache hits work) ## Implementation PR: #433
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#97
No description provided.