#TIL 26 - How to connect to Redis on AWS (Amazon ElastiCache)

May 31, 2022 · Dung Huynh

What

Connect ioredis to AWS ElastiCache Redis with proper TLS and reconnection handling.

Why

ElastiCache requires TLS (rediss://) and needs special handling for connection errors.

How

import Redis from "ioredis";

const redisClient = new Redis(process.env.REDIS_URL, {
  lazyConnect: true,
  connectTimeout: 15000,
  retryStrategy: (times) => Math.min(times * 30, 1000),
  reconnectOnError(err) {
    // Retry on specific errors
    return [/READONLY/, /ETIMEDOUT/].some((re) => re.test(err.message));
  },
});