diff --git a/app/backend/src/redis.ts b/app/backend/src/redis.ts new file mode 100644 index 0000000..83fefa4 --- /dev/null +++ b/app/backend/src/redis.ts @@ -0,0 +1,22 @@ +// Single Redis client for the whole backend (sessions, rate-limit, view buffer). +// Lazy-connected so test suites can swap it out before connection. +import { Redis } from 'ioredis'; +import { config } from './config.js'; + +export const redis = new Redis(config.REDIS_URL, { + lazyConnect: true, + // Reconnect with exponential backoff up to 5s; ioredis handles command queuing while reconnecting. + retryStrategy: (times: number) => Math.min(times * 200, 5000), + enableReadyCheck: true, +}); + +redis.on('error', (err: Error) => { + // Don't crash on transient errors — Fastify will surface 503s via /health. + console.error('[redis]', err.message); +}); + +export async function connectRedis() { + if (redis.status === 'wait' || redis.status === 'end') { + await redis.connect(); + } +}