Cache key for complex filtered /api/content queries exceeds KV 512-byte key limit (414)

Open Beginner friendly
#1,200 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript

Research direction

Start with packages/core/src/plugins/cache/services/cache.ts and trace how generateKey() builds KV keys; compare this with the filtered query key construction in packages/core/src/routes/api.ts. Reproduce with a multi-clause where filter and confirm the key stays within KV’s 512-byte limit while remaining stable for the same query.

Written by the indexing model from the issue text.

Description

area: api bug priority: P2

Summary

Observed during an E2E run against test.sonicjs.com with wrangler tail attached:

(error) KV cache read error: Error: KV GET failed: 414 UTF-8 encoded length of 667 exceeds key length limit of 512.
(error) KV cache write error: Error: KV PUT failed: 414 UTF-8 encoded length of 667 exceeds key length limit of 512.

Triggered by requests like:

GET /api/content?collection=blog_post&where={"or":[{"field":"status","operator":"not_equals","value":"published"},{"field":"slug","operator":"equals","value":"..."},{"field":"slug","operator":"equals","value":"..."}]}&limit=100

Root cause

packages/core/src/routes/api.ts builds cache keys by JSON-stringifying the parsed filter object together with the compiled SQL, with no length cap or hashing:

const cacheKey = cache.generateKey('content-filtered', JSON.stringify({ filter: normalizedFilter, query: queryResult.sql }))

CacheService.generateKey() (packages/core/src/plugins/cache/services/cache.ts:427) just concatenates namespace + type + identifier as-is — no truncation or hashing:

generateKey(type: string, identifier: string): string {
  return generateCacheKey(this.config.namespace, type, identifier)
}

For a where filter with multiple OR clauses (common in E2E assertions, and plausible in real admin/public API usage with complex filters), the resulting key easily exceeds Cloudflare KV's 512-byte key limit, producing a 414 from the KV API on both read and write.

Impact

  • Silent cache-miss behavior for any sufficiently complex filtered content query — not a crash (caught and logged), but defeats caching entirely for those queries, and spams error-level logs on every request matching the pattern.
  • Likely affects real traffic with complex filters/searches, not just E2E — anyone with a multi-clause OR filter on /api/content (or the collection-scoped equivalent) hits this.

Suggested fix

Hash long identifiers (e.g. SHA-256 truncated, or similar) before building the KV key when the combined key would exceed KV's limit, in generateCacheKey/CacheService.generateKey, rather than relying on the raw JSON-stringified filter+SQL.

Repro

  1. GET /api/content?collection=<any>&where=<a filter with 2+ OR/AND clauses so the stringified filter+SQL exceeds ~480 bytes>
  2. Check wrangler tail for KV cache read/write error: ... 414 ... exceeds key length limit of 512.

Found incidentally while investigating #1194 (plugin wiring cross-request promise drop) — unrelated root cause, filing separately.

Dominant language
TypeScript
Stars
1.8k
Forks
219
Avg merge
2d 21h
Merged PRs (30d)
67

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from SonicJs-Org/sonicjs

All issues in SonicJs-Org/sonicjs

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.