Skip to content

refactor(posthog): cache posthog queries in Redis#2554

Merged
chrarnoldus merged 5 commits intomainfrom
posthog-query-redis-cache
Apr 20, 2026
Merged

refactor(posthog): cache posthog queries in Redis#2554
chrarnoldus merged 5 commits intomainfrom
posthog-query-redis-cache

Conversation

@kilo-code-bot
Copy link
Copy Markdown
Contributor

@kilo-code-bot kilo-code-bot bot commented Apr 17, 2026

Summary

  • Replaces unstable_cache in apps/web/src/lib/posthog-query.ts with Redis-backed caching via redisGet/redisSet.
  • Keeps the same 24h TTL and the same (name, query) => Promise<Output[]> function shape, so call sites (notifications.ts) don't change.
  • Cache key is posthog-query:<sha256(name\0query)>. Cached value is the parsed, schema-validated JSON payload. Redis failures (connect/timeout) are caught and fall through to a fresh PostHog query, so this fails open.

Verification

  • pnpm typecheck
  • pnpm lint
  • pnpm format

Visual Changes

N/A

Reviewer Notes

  • If a cached blob fails schema validation (e.g. after a schema change), we log a warning and refetch rather than throwing.
  • No writes to redisSet when REDIS_URL is unset — redisSet returns false in that case and we swallow nothing extra.

Comment thread apps/web/src/lib/posthog-query.ts
Comment thread apps/web/src/lib/posthog-query.ts Outdated
@kilo-code-bot
Copy link
Copy Markdown
Contributor Author

kilo-code-bot bot commented Apr 17, 2026

Code Review Summary

Status: 2 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 2
SUGGESTION 0
Issue Details (click to expand)

WARNING

File Line Issue
apps/web/src/lib/posthog-query.ts 73 Cache key only uses name, so changing the query text for an existing name can serve stale cached results for up to 24 hours.
apps/web/src/lib/posthog-query.ts 75 Cache read failures or stale cached payloads now throw before the code can fall back to a fresh PostHog fetch.

Fix these issues in Kilo Cloud

Other Observations (not in diff)

Issues found in unchanged code that cannot receive inline comments:

None.

Files Reviewed (1 files)
  • apps/web/src/lib/posthog-query.ts - 2 unresolved issues

Reviewed by gpt-5.4-20260305 · 551,705 tokens

kilo-code-bot bot and others added 3 commits April 17, 2026 15:29
Redis-cached payloads were the post-transform output but were re-validated with
the original schema on cache hits, causing every cached value to fail schema
validation and refetch. Cache the raw pre-transform results instead so the
schema parse runs identically on fresh and cached paths.
Comment thread apps/web/src/lib/posthog-query.ts
@chrarnoldus chrarnoldus merged commit ddc28eb into main Apr 20, 2026
16 checks passed
@chrarnoldus chrarnoldus deleted the posthog-query-redis-cache branch April 20, 2026 10:00
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.

2 participants