Skip to content

improvement(performance): use redis for session data#934

Merged
icecrasher321 merged 1 commit intostagingfrom
feat/redis-session
Aug 12, 2025
Merged

improvement(performance): use redis for session data#934
icecrasher321 merged 1 commit intostagingfrom
feat/redis-session

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

conditionally use redis for session data if it is available, otherwise proceed as normal, massive performance improvement

Type of Change

  • New feature

Testing

Tested manually, fixes infinite getSession() calls when client side polls since with redis there is no polling

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link

vercel bot commented Aug 11, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
sim ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 11, 2025 4:28am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
docs ⬜️ Skipped (Inspect) Aug 11, 2025 4:28am

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Summary

This PR introduces conditional Redis-based session storage to the authentication system, providing a significant performance improvement by reducing database queries for session data. The implementation leverages the existing Redis infrastructure already established in the codebase for caching and locking mechanisms.

The core change modifies the better-auth configuration in apps/sim/lib/auth.ts to conditionally add a secondaryStorage layer when the REDIS_URL environment variable is available. This secondary storage uses the existing getRedisClient() function from the Redis utilities to cache session data with proper error handling and graceful fallbacks. The implementation includes three key operations:

  • get: Retrieves session data from Redis with the auth: key prefix
  • set: Stores session data with optional TTL support using setex or set
  • delete: Removes session data from Redis cache

The PR also optimizes session cache timing based on Redis availability. When Redis is present, the system uses shorter cache durations (5-minute cookie cache with 0 freshAge) since Redis can serve data instantly. Without Redis, it maintains longer cache times (1-hour cookie cache with 6-hour freshAge) to reduce database load. This dual approach addresses the root cause of infinite getSession() calls on the client side by eliminating the need for constant polling when Redis is available.

The environment variable documentation in apps/sim/lib/env.ts has been updated to clarify that Redis is optional but provides performance benefits, maintaining backward compatibility for deployments without Redis infrastructure.

Confidence score: 4/5

  • This PR is safe to merge with good performance benefits and proper fallback handling
  • Score reflects solid implementation with existing patterns and comprehensive error handling
  • Pay close attention to the session timing configurations to ensure they align with your application's requirements

2 files reviewed, 1 comment

Edit Code Review Bot Settings | Greptile

enabled: true,
maxAge: 24 * 60 * 60, // 24 hours in seconds
// Use shorter cache with Redis (5 min), longer without (1 hour)
maxAge: env.REDIS_URL ? 5 * 60 : 60 * 60,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider extracting these magic numbers (5 * 60, 60 * 60, etc.) to named constants for better maintainability

@icecrasher321 icecrasher321 merged commit 3c7b3e1 into staging Aug 12, 2025
5 checks passed
icecrasher321 added a commit that referenced this pull request Aug 12, 2025
@waleedlatif1 waleedlatif1 deleted the feat/redis-session branch August 15, 2025 17:03
arenadeveloper02 pushed a commit to arenadeveloper02/p2-sim that referenced this pull request Sep 19, 2025
arenadeveloper02 pushed a commit to arenadeveloper02/p2-sim that referenced this pull request Sep 19, 2025
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