Skip to content

⚡ Bolt: Memoize core auth and DB queries#75

Open
RAbuseedo wants to merge 1 commit into
mainfrom
bolt/memoize-auth-queries-5229460848385358780
Open

⚡ Bolt: Memoize core auth and DB queries#75
RAbuseedo wants to merge 1 commit into
mainfrom
bolt/memoize-auth-queries-5229460848385358780

Conversation

@RAbuseedo

Copy link
Copy Markdown
Owner

💡 What

Implemented request-scoped memoization for core authentication and database query functions using React's cache utility. I also added a conditional memoize helper in lib/utils.ts to ensure the application remains compatible with the Edge Runtime (used in Next.js Middleware), where React's cache is not available.

🎯 Why

In Next.js App Router, foundational functions like getUser() are often called multiple times across different segments of a single request (e.g., in RootLayout for the UserProvider, in page components for authorization, and in server actions). Without memoization, each call triggers a new database query and a JWT verification, leading to unnecessary latency and resource consumption.

📊 Impact

  • Reduces Database Load: Eliminates redundant "select user" and "select team" queries within the same request.
  • Reduces CPU Usage: Prevents multiple JWT decodes/verifications for the same session token.
  • Improved Performance: Faster server-side rendering and lower TTFB for all authenticated routes.

🔬 Measurement

The optimization can be verified by monitoring database query logs or adding temporary console logs to the memoized functions. You will see that for a single page load (e.g., the dashboard), these functions now execute their logic only once, even if called 3-4 times across the component tree. Type safety was verified with pnpm exec tsc --noEmit.


PR created automatically by Jules for task 5229460848385358780 started by @RAbuseedo

Implement request-scoped memoization using React 'cache' for high-frequency
authentication and database functions. This optimization reduces redundant
JWT verifications and database roundtrips within a single request lifecycle.

Key changes:
- Added a conditional `memoize` helper in `lib/utils.ts` for Edge compatibility.
- Memoized `verifyToken` in `lib/auth/session.ts`.
- Memoized `getUser`, `getUserWithTeam`, and `getTeamForUser` in `lib/db/queries.ts`.
- Added performance comments explaining the impact.

Expected impact:
- 100% reduction in redundant database calls for user/team data per request.
- 100% reduction in redundant JWT decodes per request.
- Improved Time to First Byte (TTFB) for authenticated routes.

Co-authored-by: RAbuseedo <97478862+RAbuseedo@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

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.

1 participant