Redis as secondaryStorage generate a lot of client connection to my redis server

Hello, i have setup my redis client inside auth.ts to use it as a secondaryStorage, i have added inside my code a small redis.on("connect",... to log when a redis client is created, and i have see that at each new page who use auth there is a dedicated redis client. (I have 6 pages, in my redis i can see 8 connection (next -> 6, me -> 1, backend -> 1) Does i have this because i have a wrong better auth setup or something like that ? My code :
/* Some dependancies */
const prisma = new PrismaClient();
const redis = createClient();
redis.connect();
redis.on("connect", () => {
console.log("Redis client connected");
});
redis.on("error", (err) => {
console.error("Redis client error", err);
});
redis.on("disconnect", () => {
console.log("Redis client disconnected");
});

export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
rateLimiting: {
enabled: true,
maxRequests: 60,
window: 60,
// storage: "secondary-storage",
customRules: {
"/sign-in/email": {
window: 10,
max: 3,
},
},
},
session: {
storeSessionInDatabase: true,
cookieCache: {
enabled: true,
maxAge: 60
}
},
secondaryStorage: {
get: async (key) => {
const value = await redis.get(key);
return value ? value : null;
},
set: async (key, value, ttl) => {
console.log("Setting key in Redis:", key, "with value:", value, "and ttl:", ttl);
if (ttl) await redis.set(key, value, { EX: ttl });
else await redis.set(key, value);
},
delete: async (key) => {
await redis.del(key);
}
}
});
/* Some dependancies */
const prisma = new PrismaClient();
const redis = createClient();
redis.connect();
redis.on("connect", () => {
console.log("Redis client connected");
});
redis.on("error", (err) => {
console.error("Redis client error", err);
});
redis.on("disconnect", () => {
console.log("Redis client disconnected");
});

export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
rateLimiting: {
enabled: true,
maxRequests: 60,
window: 60,
// storage: "secondary-storage",
customRules: {
"/sign-in/email": {
window: 10,
max: 3,
},
},
},
session: {
storeSessionInDatabase: true,
cookieCache: {
enabled: true,
maxAge: 60
}
},
secondaryStorage: {
get: async (key) => {
const value = await redis.get(key);
return value ? value : null;
},
set: async (key, value, ttl) => {
console.log("Setting key in Redis:", key, "with value:", value, "and ttl:", ttl);
if (ttl) await redis.set(key, value, { EX: ttl });
else await redis.set(key, value);
},
delete: async (key) => {
await redis.del(key);
}
}
});
1 Reply
🍋 Pandeo 🍋
🍋 Pandeo 🍋OP2mo ago
My logs :
✓ Starting...
✓ Ready in 1910ms
○ Compiling /middleware ...
✓ Compiled /middleware in 575ms (155 modules)
GET /camera - ::1
○ Compiling /api/health-check ...
✓ Compiled /api/health-check in 1809ms (423 modules)
GET /api/health-check 200 in 1995ms
○ Compiling /api/auth/session ...
✓ Compiled /api/auth/session in 3.7s (1423 modules)
Redis client connected
GET /api/auth/session 200 in 4153ms
○ Compiling /camera ...
✓ Compiled /camera in 8s (3453 modules)
GET /camera 200 in 8478ms
✓ Compiled in 1543ms (563 modules)
GET /camera - ::1
GET /api/health-check 200 in 30ms
Redis client connected
GET /api/auth/session 200 in 229ms
GET /camera 200 in 279ms
GET /api/auth/session 200 in 16ms
POST /camera - ::1
GET /api/health-check 200 in 8ms
GET /api/auth/session 200 in 10ms
Redis client connected
POST /camera 200 in 268ms
✓ Starting...
✓ Ready in 1910ms
○ Compiling /middleware ...
✓ Compiled /middleware in 575ms (155 modules)
GET /camera - ::1
○ Compiling /api/health-check ...
✓ Compiled /api/health-check in 1809ms (423 modules)
GET /api/health-check 200 in 1995ms
○ Compiling /api/auth/session ...
✓ Compiled /api/auth/session in 3.7s (1423 modules)
Redis client connected
GET /api/auth/session 200 in 4153ms
○ Compiling /camera ...
✓ Compiled /camera in 8s (3453 modules)
GET /camera 200 in 8478ms
✓ Compiled in 1543ms (563 modules)
GET /camera - ::1
GET /api/health-check 200 in 30ms
Redis client connected
GET /api/auth/session 200 in 229ms
GET /camera 200 in 279ms
GET /api/auth/session 200 in 16ms
POST /camera - ::1
GET /api/health-check 200 in 8ms
GET /api/auth/session 200 in 10ms
Redis client connected
POST /camera 200 in 268ms
And if i have this issue with redis, does i have also a lot of db connection with prisma ?

Did you find this page helpful?