node-redis icon indicating copy to clipboard operation
node-redis copied to clipboard

setPingConnectionInterval for node-redis

Open mercury200Hg opened this issue 3 years ago • 11 comments

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

mercury200Hg avatar Apr 15 '21 12:04 mercury200Hg

Hi team, is anyone checking this ?

mercury200Hg avatar May 20 '21 15:05 mercury200Hg

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?

mercury200Hg avatar Jun 25 '21 07:06 mercury200Hg

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 avatar Jun 25 '21 17:06 leibale

@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 avatar Jun 28 '21 05:06 mercury200Hg

@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 avatar Jun 28 '21 15:06 leibale

@leibale

Link to load balancer for your info :

  1. https://docs.aws.amazon.com/elasticloadbalancing/latest/network/network-load-balancers.html
  2. https://docs.microsoft.com/en-us/azure/load-balancer/

mercury200Hg avatar Jul 19 '21 17:07 mercury200Hg

Is this already implemented or there's still scope to add this ? @leibale

mercury200Hg avatar Aug 10 '22 05:08 mercury200Hg

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)

leibale avatar Aug 10 '22 12:08 leibale

That's because for cloud providers like Azure/AWS LoadBalancers don't honor the TCP connection alive set from Redis client.

mercury200Hg avatar Aug 10 '22 14:08 mercury200Hg

Why your redis server is behind a load balancer?

leibale avatar Aug 10 '22 16:08 leibale

That's how Azure managed Redis works internally. 😅

mercury200Hg avatar Aug 10 '22 18:08 mercury200Hg

https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-best-practices-connection#idle-timeout

leibale avatar Nov 01 '22 23:11 leibale