(/usr/src/app/node_modules/@redis/client/dist/lib/client/socket.js:194:118) t2-backend-app-1 | at Object.onceWrapper (node:events:632:26) t2-backend-app-1 | at Socket.emit (node:events:517:28) t2-backend-app-1 | at TCP. (node:net:350:12)"> (/usr/src/app/node_modules/@redis/client/dist/lib/client/socket.js:194:118) t2-backend-app-1 | at Object.onceWrapper (node:events:632:26) t2-backend-app-1 | at Socket.emit (node:events:517:28) t2-backend-app-1 | at TCP. (node:net:350:12)"> (/usr/src/app/node_modules/@redis/client/dist/lib/client/socket.js:194:118) t2-backend-app-1 | at Object.onceWrapper (node:events:632:26) t2-backend-app-1 | at Socket.emit (node:events:517:28) t2-backend-app-1 | at TCP. (node:net:350:12)">

Untitled

t2-backend-app-1  | /usr/src/app/node_modules/@redis/client/dist/lib/client/socket.js:194
t2-backend-app-1  |                     __classPrivateFieldGet(this, _RedisSocket_instances, "m", _RedisSocket_onSocketError).call(this, new errors_1.SocketClosedUnexpectedlyError());
t2-backend-app-1  |                                                                                                                      ^
t2-backend-app-1  | Error: Socket closed unexpectedly
t2-backend-app-1  |     at Socket.<anonymous> (/usr/src/app/node_modules/@redis/client/dist/lib/client/socket.js:194:118)
t2-backend-app-1  |     at Object.onceWrapper (node:events:632:26)
t2-backend-app-1  |     at Socket.emit (node:events:517:28)
t2-backend-app-1  |     at TCP.<anonymous> (node:net:350:12)

Reids 를 predixy로 구성하고 NestJS redis 라이브러리를 활용해서 구성하였을 때

predixy 서버가 죽거나 연결이 종료되었을 경우에 정상적으로 체크를 못하는 문제가 있었다.

처음 구성하였을 때

import { Module } from '@nestjs/common';
import { createClient } from 'redis';

@Module({
  providers: [
    {
      provide: 'REDIS_CLIENT',
      useFactory: async () => {
        const client = createClient({
          url: 'redis://predixy:7500',
          socket: {
            keepAlive: 1000,
						reconnectStrategy: 100,
          },
        });
        await client.connect();
        return client;
      },
    },
  ],
  exports: ['REDIS_CLIENT'],
})
export class RedisModule {}

위와같이 소켓 옵션에 reconnectStrategy 를 통해 redis 서버와 연결이 끊겼을 때 재시도 하는 옵션을 넣어주어도 연결을 재시도 하지 못하고 Socket closed 만을 남기고 죽어버렸다…

다른 디스커넥션 상태에서도 동일하게 다시 연결을 시도하지 못해서 삽질을 하다보니, 에러 이벤트가 뭔가 문제가 있는것같아서

아래와 같이 소켓 자체에 error 이벤트를 바인딩 해주어서 테스트를 해보니 정상적으로 동작한다!

import { Logger, Module } from '@nestjs/common';
import { createClient } from 'redis';

@Module({
  providers: [
    {
      provide: 'REDIS_CLIENT',
      useFactory: async () => {
        const client = createClient({
          url: 'redis://predixy:7500',
          socket: {
            keepAlive: 1000,
            reconnectStrategy: (retries: number, cause: Error) => {
              Logger.error(
                `[${retries}] ${cause.name} : ${cause.message}`,
                'Redis Client',
              );
              return 1000;
            },
          },
        });
        client.on('error', function () {});
        client.on('connect', function () {
          Logger.log(`Connect to ${client.options.url}`, 'Redis Client');
        });
        await client.connect();
        return client;
      },
    },
  ],
  exports: ['REDIS_CLIENT'],
})
export class RedisModule {}

client 소켓에 error 이벤트를 바인딩해주니 정상적으로 리커넥팅을 시도하였고, 로깅을 위한 작업도 추가해줬다.