sangwon seo
sangwon seo

Reputation: 13

node-redis Does retry_strategy have a default value?

Redis connection was valid even when redis-server was stopped and restarted without the retry_strategy option.

const conn = redis.createClient({
    host: 'redisUrl',
    port: 'redisPort',
    ...
    socket_keepalive : true   
});

redisClient.on('connect', () => {
    console.log(`connect`);
}).on('error', () => {
    console.log(`error`);
});

Why?

Upvotes: 1

Views: 5986

Answers (2)

Chiuri C-kay
Chiuri C-kay

Reputation: 119

Since its the year 2022. A lot has changed when connecting to a REDIS Client(Or let me say I have my preferred method of connecting to a REDIS Client).

I found a really helpful Github link that talks about the various options that are required by the REDIS createClient method.

To begin with, retry_strategy didn't work in my code so I researched and came across the socket.reconnectStrategy.

The reconnectStrategy option takes a function as its value. This function either return a number or an Error. And this function takes in one argument(number_of_retries). Since I'm using TypeScript, My function has to return either a number or an error.

This is how I implemented it in my code.

let client: redis.RedisClientType = redis.createClient({
      socket: {
        host: "127.0.0.1",
        port: 6379,
        reconnectStrategy: (retries: number): number | Error => {
          if (retries > 10) {
            console.log("Too many retries on REDIS. Connection Terminated");
            return new Error("Too many retries.");
          } else {
            return retries;
          }
        },
      },
      password: "",
      disableOfflineQueue: false,
    });

Upvotes: 3

Rash
Rash

Reputation: 8227

Yes, node-redis did have a default strategy as outlined in their README. However, all of them are now deprecated in favor of retry_strategy function.

Specifically, look for retry_max_delay, connect_timeout, and max_attempts.

retry_max_delay null Deprecated Please use retry_strategy instead. By default, every time the client tries to connect and fails, the reconnection delay almost doubles. This delay normally grows infinitely, but setting retry_max_delay limits it to the maximum value provided in milliseconds.

connect_timeout 3600000 Deprecated Please use retry_strategy instead. Setting connect_timeout limits the total time for the client to connect and reconnect. The value is provided in milliseconds and is counted from the moment a new client is created or from the time the connection is lost. The last retry is going to happen exactly at the timeout time. Default is to try connecting until the default system socket timeout has been exceeded and to try reconnecting until 1h has elapsed.

max_attempts 0 Deprecated Please use retry_strategy instead. By default, a client will try reconnecting until connected. Setting max_attempts limits total amount of connection attempts. Setting this to 1 will prevent any reconnect attempt.

Upvotes: 3

Related Questions