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?
2
Answers
Yes,
node-redis
did have a default strategy as outlined in their README. However, all of them are now deprecated in favor ofretry_strategy
function.Specifically, look for
retry_max_delay
,connect_timeout
, andmax_attempts
.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.
[https://github.com/redis/node-redis/blob/HEAD/docs/client-configuration.md][1]
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.