1

I've setup a Redis caching and for that I've a Redis-server running on wsl2 and client on NodeJS platform. As soon as the redis-server went down I've to make a few connections that too with a waiting time, but it's making reconnection requests continuously even if I'm using this package(node-redis-retry-strategy).

Here's my code

const redis = require('redis');
var retryStrategy = require("node-redis-retry-strategy");

var client = redis.createClient({
    host: "localhost",
    port: 6379,
    retry_strategy: retryStrategy()
});

client.connect()

client.on("connect", function(){
   console.log("connected to redis server !!!")

client.on("reconnecting",function(){
    console.log("inside reconnecting")
})

client.on("error",function(error ){ 
    console.log(error)
})

And also I've tried sending option argument

const redis = require('redis');
var retryStrategy = require("node-redis-retry-strategy");

var client = redis.createClient({
    host: "localhost",
    port: 6379,
    retry_strategy: retryStrategy({
        allow_to_start_without_connection: true,
        number_of_retry_attempts: 7,
        delay_of_retry_attempts: 1000
    })
});

client.connect()

client.on("connect", function(){
   console.log("connected to redis server !!!")

client.on("reconnecting",function(){
    console.log("inside reconnecting")
})

client.on("error",function(error ){ 
    console.log(error)
})

And the versions for redis and the retrystrategy package I'm using is

"redis": "^4.3.1",
"node-redis-retry-strategy": "^2.1.1",

additional data

node version v16.17.1

for setting redis-server locally I've used : v=7.0.5 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=68bf11aad5b039df

And I'm using WSL2

1 Answers1

0

I'm using 4.4.0 and was looking for auto reconnect solution. And it looks like there is no such option, so it appears to be not supported. redis options

So what I've done is just setting up reconnection by hand

protected handleRedisConnected = () => {
    this.log("redis connection established");
    this.reconnectionAttempt = 0;
};

protected handleRedisDisconnected = () => {
    this.log(`redis connection is down, reconnecting #${this.reconnectionAttempt}`);
    setTimeout(this.initRedis, this.reconnectionAttempt++ > 11 ? 11 : this.reconnectionAttempt * 5000 + 5000);
};

private initRedis = async () => {
    this.log("initializing redis client");
    try {
        this.redis = createClient({url: this.redisUrl});

        this.redis.on("connect", this.handleRedisConnected);
        this.redis.on("error", this.handleRedisDisconnected);

        await this.redis.connect();
        this.log("initializing redis client success");
    } catch (error) {
        this.logError(`initializing redis client failure ${error}`);
        this.handleRedisDisconnected();
    }
};
Bender
  • 617
  • 6
  • 17