perf(web): cache verified sessions to eliminate repeated auth round-trips - #1644
Closed
Sravanjangam wants to merge 1 commit into
Closed
perf(web): cache verified sessions to eliminate repeated auth round-trips#1644Sravanjangam wants to merge 1 commit into
Sravanjangam wants to merge 1 commit into
Conversation
…rips Fixes supermemoryai#1623 Co-authored-by: Sravanjangam <163002695+Sravanjangam@users.noreply.github.com>
Contributor
Author
|
Successor to #1624 — original PR was auto-closed by |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Caches
hasVerifiedSessioninapps/web/lib/verify-session.tsto avoid a full HTTPS round-trip toapi.supermemory.aion every metered/proxy request. Small, web-only change — no new dependencies, no cross-package refactor.Fixes #1623
Problem
hasVerifiedSessioncalledfetch(${NEXT_PUBLIC_BACKEND_URL}/api/auth/get-session)on every invocation with no memoization. After #1596 only the OG route (apps/web/app/api/og/route.ts:289) still calls it, but link-preview bursts (e.g., 20 URLs in one chat message) pay 20 concurrent backend round-trips for the same cookie.apps/web/lib/verify-session.ts:18—hasVerifiedSessiondid a freshfetchevery time.Impact
Solution
In-process cache in
apps/web/lib/verify-session.ts:SHA-256(cookie)— fixed-length, no session decoding.userpresent), 5 s for negative (no_user). Short enough to respect revocation, long enough for burst absorption.fetch.unauthorized: delete the entry, returnfalse(no caching — next caller retries).transient: stale-while-error (return last cached value if any, elsefalse).touch()on hit to keep hot keys alive.invalidateSession(cookie)for logout/server-side revocation (web-only, single-file).No MCP / extension / SDK changes — those are a follow-up (
fix/session-verification-cache.1) once this pattern is validated.Benchmark
Measured locally before and after introducing the cache (isolated
bunharness with mockedfetch;delay: 50 mssimulates RTT toapi.supermemory.ai; real RTT is 80–250 ms per the issue):Net: burst of 20 previews goes from ~20 × 120 ms of serialized backend load to ~120 ms + 2 ms × 19.
Failure Handling
401/403invalidates cache immediately — sign-out or revocation observed on the very next request.false; the cache entry is not overwritten with the failure.SHA-256(cookie)means a different cookie never collides.Memory Footprint
The cache is bounded to 1,000 entries with LRU eviction to prevent unbounded memory growth in long-lived web processes (Cloudflare Workers / Next.js server). Each entry is
{ value: boolean, expiresAt: number }(~40 bytes) + Map overhead — worst-case ~50–100 KB total, negligible vs. the per-request fetch it saves.touch()on hit keeps hot sessions alive.Testing
Manual verification via isolated parallel harness — 10 scenarios, each in its own module instance (fresh
Mapcache), run concurrently as 10bunprocesses:All 10 passed in ~11 s wall time (parallel isolated processes). Harness at
/tmp/prs/run-parallel.shreplays against the committed module; not committed to repo becauseapps/webhas no test framework (per #1579 maintainer feedback).Biome:
bun x biome check apps/web/lib/verify-session.ts— clean (auto-formatted).Typecheck:
apps/webpre-existing ~20 errors unrelated to this file (verified viagit stashbaseline); no new errors introduced.Environment
fix/session-verification-cache@3212eadc(pushed toSravanjangam/supermemory)supermemoryai/supermemory@main d436792e