node-redis
node-redis copied to clipboard
setPingConnectionInterval for node-redis
Issue
Basically, if you run redis behind AWS-NLB or Azure-Loadbalancer there's an idle timeout configured by LoadBalancer like 350 seconds for AWS and 4 minutes for azure. Despite the keepalive you set on the machine hosting redis, the connection from the node app will be dropped if it's idle for more than the above time.
Is it possible to enable a parameter like setPingConnectionInterval
which basically sends a PING request to redis to keep the connection alive and prevent it from dropping?
This could be same as the setPingConnectionInterval used in Redisson client
or setTestOnBorrow in jedis
.
If there's existing functionality that takes care of it, please help out in pointing the same. Say- socket_initial_delay
which states this will also behave the interval keep-alive message sending to Redis.
From what I understand it will keep on sending the messages to keep the connection alive other than the initial delay. Correct me if i am wrong here.
Environment
-
Node.js Version:
9.1.0
-
Redis Version:
5.0.6
-
Platform:
Ubuntu-18.04
Hi team, is anyone checking this ?
Hi Team, Can someone guide the process of contributing this feature if it looks good. I mean any policies, processes to take care of when submitting pull requests for the code changes? Or if any docs to be created for review?
Hi @mercury200Hg, we are currently focusing on V4.
Also, I think that this problem should be solved using "TCP keepalive" instead of a PING command.
I'm adding this issue to the V4 to-do list.
@leibale TCP keepalive doesn't usually have effect as LoadBalancers don't honour it and closes the connection if idle for more than their fixed time period. This is the case for most of cloud providers.
We tried testing with keepalive but after the designated period given by AWS/Azure loadbalancer the client gives error of - timed out. Now this case has to be handled at client side and connection to be re-initiated and it works after that.
With something which checks PING at certain frequency the connection never remains idle and the errors in app are reduced.
Thanks for adding it in V4 to-do-list. Let me know if i can contribute there.
@mercury200Hg can you please share a link to the load balancer you're using/trying to use? I want to search for another solution, cause sending a PING command just to keep the connection alive is too much in my opinion, there has to be another "less expensive" solution.
Regarding contributing code - feel free to clone the project, checkout to a new branch from the v4 branch, make the changes, and open a PR.
BTW, in the meantime, you can use setInterval
or setTimeout
to send ping requests every X time:
setInterval(client => {
client.ping((err) => {
if (err) console.error('Redis keepalive error', err);
});
}, X);
@leibale
Link to load balancer for your info :
- https://docs.aws.amazon.com/elasticloadbalancing/latest/network/network-load-balancers.html
- https://docs.microsoft.com/en-us/azure/load-balancer/
Is this already implemented or there's still scope to add this ? @leibale
I still don't understand why there's a need to issue a PING
command in order to keep the TCP connection alive instead of just using the built-in TCP keepalive functionality (https://nodejs.org/api/net.html#socketconnectoptions-connectlistener)
That's because for cloud providers like Azure/AWS LoadBalancers don't honor the TCP connection alive set from Redis client.
Why your redis server is behind a load balancer?
That's how Azure managed Redis works internally. 😅
https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-best-practices-connection#idle-timeout