improvement(performance): use redis for session data#934
improvement(performance): use redis for session data#934icecrasher321 merged 1 commit intostagingfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Skipped Deployment
|
There was a problem hiding this comment.
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
setexorset - 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
| 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, |
There was a problem hiding this comment.
style: Consider extracting these magic numbers (5 * 60, 60 * 60, etc.) to named constants for better maintainability
This reverts commit 3c7b3e1.
…ioai#934)" (simstudioai#947) This reverts commit 3c7b3e1.
Summary
conditionally use redis for session data if it is available, otherwise proceed as normal, massive performance improvement
Type of Change
Testing
Tested manually, fixes infinite getSession() calls when client side polls since with redis there is no polling
Checklist