Repository navigation
perf(auth): collapse the session lookup from seven round trips to one - #6743
Conversation
Every authenticated request in the product resolves a session, and each resolution was seven separate HTTP round trips to Neon. The database is in us-west-2 while every function runs in iad1, so a single query costs 73-100ms rather than a few. api's `GET /api/trpc/[trpc]` alone accounts for 360M such spans at 73ms average. The seven were: the session row, the user row, a session refresh, the jwks table, then customSession's memberships, subscription and a second read of the user row it had already been handed. That last one was pure waste — the same row, read twice. It existed because additionalFields were not surfacing on the passed-in user. The memberships and subscription reads were sequential only because the active-organization fallback made the order matter, and that fallback is expressible in SQL. All three collapse into one statement. The jwks read is cached behind the plugin's adapter.getJwks hook rather than disabled outright: `disableSettingJwtHeader` would drop the round trip entirely, but desktop and mobile both read `set-auth-jwt` off response headers to refresh their token opportunistically. createJwk writes through and invalidates, so a newly created key is never masked. cookieCache already covers the session and user rows two thirds of the time, but customSession re-ran regardless, so the remaining reads were paid on every call including cache hits. Net: seven round trips become one on a cookie-cache hit and three on a miss. Verified against a real database branch with a differential harness comparing old and new across 64 cases — multi-membership users at every active-organization permutation, paid and unpaid, stale organization ids, and no active organization — 0 mismatches, 104ms to 36ms per call. That harness caught a real bug worth noting for review: these are `timestamp` columns, so Postgres returns a naive string with no zone, and `new Date()` reads it as local time while drizzle treats it as UTC. Every onboardedAt came back 8 hours off. Fixed in toDate(). Claude-Session: https://claude.ai/code/session_014D6wif8HDRCXEFf5Ku4w7V
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (4)
Included review availability: Your plan provides up to 10 included reviews per hour; 3 remain after this review. 📝 WalkthroughWalkthroughThe authentication package adds cached JWKS retrieval, consolidates custom session data into one query, updates session organization resolution dependencies, and uses the shared data loader in the custom session handler. ChangesAuthentication updates
Estimated code review effort: 3 (Moderate) | ~25 minutes Merge Risk: ⚪ Minimal · up to This change reduces authenticated-request database round trips and adds JWKS caching; no concrete merge-blocking correctness or availability risk is identified at the current head, so it is merge-ready after normal checks. Sequence Diagram(s)sequenceDiagram
participant CustomSessionHandler
participant loadCustomSessionData
participant Database
participant resolveSessionOrganizationState
CustomSessionHandler->>loadCustomSessionData: load data for user and active organization
loadCustomSessionData->>Database: query memberships, plan, and lifecycle timestamps
Database-->>loadCustomSessionData: return session data
loadCustomSessionData-->>CustomSessionHandler: return mapped session data
CustomSessionHandler->>resolveSessionOrganizationState: resolve organization using loaded memberships
resolveSessionOrganizationState-->>CustomSessionHandler: return organization state
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🧹 Preview Cleanup CompleteThe following preview resources have been cleaned up:
Thank you for your contribution! 🎉 |
The problem
Every authenticated request resolves a session, and each resolution was seven HTTP round trips to Neon. The database is in
us-west-2; every Vercel function is iniad1. So one query costs 73-100ms, not a few.Scale:
api'sGET /api/trpc/[trpc]alone has 360M Neon spans at 73ms average.findOne sessionsfindOne usersupdate sessionsupdateAgerefreshfindMany jwkssmembers.findManysubscriptions.findFirstusers.findFirstcookieCachewas already hitting on 40 of 60 sampled requests — butcustomSessionre-ran its callback regardless, so 4-7 were paid even on a cache hit.The changes
#5, #6, #7 → one statement. They were sequential only because the active-organization fallback made the order matter, and that fallback is expressible in SQL. #7 was reading a row better-auth had already handed us; the old comment blamed
additionalFieldsnot surfacing on the passed-in user.#4 cached, not disabled.
disableSettingJwtHeaderwould remove the round trip outright, but desktop (auth-client.ts:142) and mobile (client.ts:45) both readset-auth-jwtoff response headers to refresh opportunistically. So the read is cached behind the plugin'sadapter.getJwkshook instead, withcreateJwkwriting through and invalidating.Net: 7 → 1 on a cookie-cache hit, 7 → 3 on a miss.
Verification
A differential harness ran old vs. new against a real database branch across 64 cases — multi-membership users at every active-organization permutation, paid and unpaid, stale organization ids, no active organization, and a user with no memberships.
0 mismatches. 104ms → 36ms per call.
What to look at
The harness caught a genuine bug, which is the best argument for reviewing this carefully:
onboardedAtcame back 8 hours off on every user. These aretimestamp(nottimestamptz) columns, so Postgres returns a naive string with no zone —new Date()reads that as local time, while drizzle's mapping treats it as UTC. Silent, and it would have shifted the onboarding gate by the function's offset. Fixed intoDate(), re-verified.Specific things worth a second opinion:
toDate()inload-custom-session-data.ts— the fix above. If it's wrong, onboarding and pending-deletion gates move.db.executeinpackages/auth, deliberately: the single statement is the point. Worth checking the active-org CTE matchesresolveSessionOrganizationState's TS fallback exactly (it takes memberships orderedcreated_at DESCand picks the session's org if present, else the first).resolveSessionOrganizationStatesettling, the plan fetched belongs to the wrong org. Handled by comparingplanOrganizationIdand re-reading — rare, but confirm the fallback is right./jwksuntil expiry, so tokens signed with it would fail verification for up to a minute.createJwkinvalidates locally. Keys effectively never rotate here (no expiry configured), but the window is real.Testing
createJwkinvalidatesadapteroption (106 endpoints)https://claude.ai/code/session_014D6wif8HDRCXEFf5Ku4w7V
Summary by cubic
Collapses session resolution from seven DB round trips per request to one on a cookie-cache hit (three on a miss) by combining membership/subscription/user reads into a single SQL statement and caching JWKS reads. This reduces per-request latency and fixes timestamp parsing that could shift onboarding/deletion gates.
Review and rollout
created_at DESC).toDate()treats Postgrestimestamp(no tz) as UTC; a mismatch moves onboarding/deletion gates by the server offset.planOrganizationIddiffers from the resolved active org, we re-read the subscription; this avoids serializing the common path.createJwkinvalidates locally. We keep theset-auth-jwtheader for desktop/mobile refresh behavior.Written for commit e5167e6. Summary will update on new commits.
Summary by CodeRabbit
New Features
Bug Fixes