2

I've created a NestJS redis cache module with a retry strategy.

@Module({
  imports: [
    CacheModule.registerAsync<RedisClientOptions>({
      isGlobal: true,
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        store: redisStore,
        host: configService.get<string>('redis.host'),
        port: configService.get<number>('redis.port'),
        password: configService.get<string>('redis.password'),
        tls: configService.get<boolean>('redis.tls'),
        retry_strategy: (options) => {
          console.log({ options });
          if (options.attempt > 2) {
            // throw new Error('Retry attemps exhausted');
            return undefined;
          }
          return Math.min(options.attempt * 100, 3000);
        },
      }),
      inject: [ConfigService],
    }),
  ],
  controllers: [],
  providers: [],
})
export class RedisCacheModule {}

I have also configured the AppModule to catch redis connection errors so that these can be handled gracefully not resulting in a fatal error. The caching implementation is not critical to the applications functionality.

export class AppModule {
  constructor(@Inject(CACHE_MANAGER) cacheManager) {
    const client = cacheManager.store.getClient();

    client.on('error', (error) => {
      console.log('Redis error');
      console.log(error);
    });
  }
}

The issue I am experiencing is that when the third and final reconnection attempt fails, it causes a fatal error and the application crashes. Is there a way to catch these errors also?

Example logs:

[service] Redis error
[service] Error: getaddrinfo ENOTFOUND redis-master.redis.svc.cluster.local
[service]     at GetAddrInfoReqWrap.onlookup {
[service]   errno: -3008,
[service]   code: 'ENOTFOUND',
[service]   syscall: 'getaddrinfo',
[service]   hostname: 'redis-master.redis.svc.cluster.local'
[service] }
[service] {
[service]   options: {
[service]     attempt: 3,
[service]     error: Error: getaddrinfo ENOTFOUND redis-master.redis.svc.cluster.local
[service]         at GetAddrInfoReqWrap.onlookup {
[service]       errno: -3008,
[service]       code: 'ENOTFOUND',
[service]       syscall: 'getaddrinfo',
[service]       hostname: 'redis-master.redis.svc.cluster.local'
[service]     },
[service]     total_retry_time: 300,
[service]     times_connected: 0
[service]   }
[service] }
[service] /app/node_modules/nestjs-cache-module/lib/cache-module.js
[service]         throw new Error('Retry attemps exhausted');
[service]         ^
[service]
[service] Error: Retry attemps exhausted
[service]     at Object.retryStrategy [as retry_strategy] (/app/node_modules/nestjs-cache-module/lib/cache-module.js)
[service]     at RedisClient.connection_gone (/app/node_modules/redis/index.js:554:41)
[service]     at RedisClient.on_error (/app/node_modules/redis/index.js:346:10)
[service]     at Socket.<anonymous> (/app/node_modules/redis/index.js:223:14)
[service]     at Socket.emit (node:events:513:28)
[service]     at emitErrorNT (node:internal/streams/destroy:151:8)
[service]     at emitErrorCloseNT (node:internal/streams/destroy:116:3)
[service]     at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
bmd
  • 1,231
  • 3
  • 15
  • 23
  • I think it's also related to what has been raised here: https://github.com/redis/node-redis/issues/1202 – bmd Nov 10 '22 at 17:30
  • Did you find a solution to this? I am facing a similar issue and am wondering how to handle those errors. – eamon0989 Jul 25 '23 at 11:01

0 Answers0