Skip to content

Redis Sentinel Configuration #424

@engmsilva

Description

@engmsilva

My application works correctly accessing only one instance of Redis, but I am not able to configure my application with Redis Sentinel.

I tried to follow the example of the project's repositories, but it seems that nothing happens.

My module looks like this:

import { Module } from '@nestjs/common';
import { RedisModule } from '@liaoliaots/nestjs-redis';
import { NestRedisService } from './nestjs-redis.service';
import { RedisConfigService } from './nestjs-redis.config.service';

@Module({
  imports: [
    RedisModule.forRootAsync({
      useClass: RedisConfigService,
    }),
  ],
  providers: [NestRedisService],
  exports: [NestRedisService],
})
export class NestjsRedisModule {}

My connection configuration file looks like this:

import { Injectable } from '@nestjs/common';
import {
  RedisOptionsFactory,
  RedisModuleOptions,
} from '@liaoliaots/nestjs-redis';
import { InjectPinoLogger } from 'nestjs-pino/InjectPinoLogger';
import { PinoLogger } from 'nestjs-pino';

export interface ErrnoException extends Error {
  errno?: number;
  code?: string;
  path?: string;
  syscall?: string;
  stack?: string;
}

@Injectable()
export class RedisConfigService implements RedisOptionsFactory {
  constructor(
    @InjectPinoLogger(RedisConfigService.name)
    private readonly logger: PinoLogger,
  ) {}

  async createRedisOptions(): Promise<RedisModuleOptions> {
    const logger = this.logger;
    return {
      config: {
        role: 'master',
        host: process.env.REDIS_HOST,
        port: parseInt(process.env.REDIS_PORT),
        password: process.env.REDIS_PASSWORD,
        maxRetriesPerRequest: 0,
        onClientCreated(client) {
          client.on('error', (error: ErrnoException) => {
            logger.info(
              { description: '[Create Conection][Error]' },
              JSON.stringify(error),
            );
          });
        },
        retryStrategy(times) {
          const delay = Math.min(times * 50, 2000);
          return delay;
        },
      },
    };
  }
}

Are these my Redis and Sentinel servers?

Master - 10.1.2.3 - 6379 - has password
Slave - 10.1.2.4 - 6379 - has password
Slave - 10.1.2.5 - 6379 - has password
Sentinel - 10.1.2.3 - 26379 - has no password
Sentinel - 10.1.2.4 - 26379 - has no password

What would be the best way to adapt the code above to connect with Redis sentinel?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions