DEV Community

Tim Nguyen
Tim Nguyen

Posted on

Node.js + ioredis + elasticache

For who is struggling with ioredis connection to elasticache (cluster enable)

const REDIS_COMMAND_TIMEOUT = 10000; const MAX_REDIS_ATTEMPS = 3; const REDIS_BACKOFF_RETRY = 2000; const REDIS_HOST = process.env.REDIS_HOST || '127.0.0.1'; const REDIS_PORT = Number(process.env.REDIS_PORT) || 6379; // https://github.com/redis/lettuce/wiki/Redis-URI-and-connection-details const REDIS_SCHEME = REDIS_CLUSTER_ENABLED ? `rediss` : `redis`; const redis = new Redis(`${REDIS_SCHEME}://${REDIS_HOST}:${REDIS_PORT}`, { commandTimeout: REDIS_COMMAND_TIMEOUT, retryStrategy: (times: number): number | null => { if (times > MAX_REDIS_ATTEMPS) { return null; // Stop retrying } return REDIS_BACKOFF_RETRY; // Retry after some time }, tls: REDIS_CLUSTER_ENABLED ? {} : undefined, }); 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)