Skip to content

Commit 3c7b3e1

Browse files
authored
improvement(performance): use redis for session data (#934)
1 parent bc455d5 commit 3c7b3e1

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

apps/sim/lib/auth.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { quickValidateEmail } from '@/lib/email/validation'
2525
import { env, isTruthy } from '@/lib/env'
2626
import { isBillingEnabled, isProd } from '@/lib/environment'
2727
import { createLogger } from '@/lib/logs/console/logger'
28+
import { getRedisClient } from '@/lib/redis'
2829
import { getEmailDomain } from '@/lib/urls/utils'
2930
import { db } from '@/db'
3031
import * as schema from '@/db/schema'
@@ -72,14 +73,55 @@ export const auth = betterAuth({
7273
provider: 'pg',
7374
schema,
7475
}),
76+
// Conditionally use Redis for session storage only if Redis is available
77+
...(env.REDIS_URL
78+
? {
79+
secondaryStorage: {
80+
get: async (key: string) => {
81+
try {
82+
const redis = getRedisClient()
83+
if (!redis) return null
84+
const value = await redis.get(`auth:${key}`)
85+
return value || null
86+
} catch (error) {
87+
logger.error('Redis get error:', error)
88+
return null
89+
}
90+
},
91+
set: async (key: string, value: string, ttl?: number) => {
92+
try {
93+
const redis = getRedisClient()
94+
if (!redis) return
95+
if (ttl) {
96+
await redis.setex(`auth:${key}`, ttl, value)
97+
} else {
98+
await redis.set(`auth:${key}`, value)
99+
}
100+
} catch (error) {
101+
logger.error('Redis set error:', error)
102+
}
103+
},
104+
delete: async (key: string) => {
105+
try {
106+
const redis = getRedisClient()
107+
if (!redis) return
108+
await redis.del(`auth:${key}`)
109+
} catch (error) {
110+
logger.error('Redis delete error:', error)
111+
}
112+
},
113+
},
114+
}
115+
: {}),
75116
session: {
76117
cookieCache: {
77118
enabled: true,
78-
maxAge: 24 * 60 * 60, // 24 hours in seconds
119+
// Use shorter cache with Redis (5 min), longer without (1 hour)
120+
maxAge: env.REDIS_URL ? 5 * 60 : 60 * 60,
79121
},
80122
expiresIn: 30 * 24 * 60 * 60, // 30 days (how long a session can last overall)
81123
updateAge: 24 * 60 * 60, // 24 hours (how often to refresh the expiry)
82-
freshAge: 60 * 60, // 1 hour (or set to 0 to disable completely)
124+
freshAge: env.REDIS_URL ? 0 : 6 * 60 * 60, // 0 with Redis, 6 hours without Redis
83125
},
84126
databaseHooks: {
85127
session: {

apps/sim/lib/env.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const env = createEnv({
2929

3030
// Database & Storage
3131
POSTGRES_URL: z.string().url().optional(), // Alternative PostgreSQL connection string
32-
REDIS_URL: z.string().url().optional(), // Redis connection string for caching/sessions
32+
REDIS_URL: z.string().url().optional(), // Redis connection string for caching/sessions (optional - improves performance)
3333

3434
// Payment & Billing
3535
BILLING_ENABLED: z.boolean().optional(), // Enable billing enforcement and usage tracking

0 commit comments

Comments
 (0)