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

Redis cluster not emitting events

Open danish241194 opened this issue 2 years ago • 11 comments

 const Redis  = require("redis");

  const cluster = Redis.createCluster({
      rootNodes: [
        {
          url: 'redis://localhost:7000',
        }]
    });

    cluster.on('ready', () => console.log('Redis Cluster ready'));

   cluster.on('connect', () => console.log('Redis Cluster connect'));

    cluster.on("reconnecting", () => console.log('Redis Cluster reconnecting '));
    cluster.on('error', (err : any) => console.log('Redis Cluster Error', err));
    cluster.connect().then(()=> {
         console.log("connected");
     });

With the above code, I get connected log but i can see 'ready' , 'connect' ,'reconnecting' events are not getting trigerred .

Is there any other way to get ready , connect and reconnecting event when dealing with cluster ? Environment:

  • "node": ">=12.22.7"
  • Redis Server Version: redis_version:6.2.5
  • node "redis": "^4.0.0",
  • Platform: mac os 10.15.7 , also tried on Amazon Linux 2

danish241194 avatar Jan 20 '22 07:01 danish241194

Currently the cluster emits errors only, wanna share the use case of those events in a cluster?

leibale avatar Jan 24 '22 17:01 leibale

I want to raise some kind of metric on reconnecting and and ready event. Also i more thing when i terminated my server i can see the cluster.on('error', (err : any) => console.log('Redis Cluster Error', err)); is not catching the connection error issue , it causes app to crash.

danish241194 avatar Jan 24 '22 17:01 danish241194

👍 I have a potentially very specific use-case for wanting cluster "ready" events:

Cluster events like "ready" would be helpful when managing connections within a long-running application. If we can listen to "ready" and "error" events, we can effectively flip between using the cluster client or not. For example:

const cluster = createCluster();
const isConnected = false;

cluster.on('ready', () => {
  isConnected = true;
});

async function get(id) => {
  if (isConnected) {
    const val = await cluster.get(id);
    return val;
  } else {
     // fallback to alternative data source
     // and try to reconnect redis in the background
     cluster.connect();
     const val = await getFromSomethingElse(id);
     return val;
  }
}

This allows an application to avoid constantly attempting to connect to redis before falling back to the alternative datasource with a try/catch block, which can lead to latency and unnecessary network I/O when the cluster is out of commission.

I assume there's a variety of reasons why cluster events aren't emitted, though! For instance a "ready" event in a regular client may not mean the same thing as "ready" in cluster which is running multiple clients to manage the cluster. So it may even be worth simply noting in documentation the client doesn't support any listeners beyond "error" in for folks wondering which events they can subscribe to.

Alternatively, it appears cluster clients don't have client.isOpen or client.isReady states, which would definitely suffice instead of emitting "ready" events.

mapsam avatar Sep 08 '22 23:09 mapsam

I also have a use case for this - using node-redis in a Lambda, I need a solid way to check that the connection is ready, as on new deployments I get 502s until the cluster has connected

philpicton avatar Oct 18 '22 15:10 philpicton

Another use case is implementing an offline queue. I need this to work in order to migrate v3 code that relies on this functionality.

dtikhonov-iex avatar May 02 '23 18:05 dtikhonov-iex

Adding this to the v5 backlog. The events I have in mind are:

  1. connect
  2. node-connect
  3. node-ready
  4. ready
  5. reconnecting
  6. error

is there any event that I'm missing?

leibale avatar May 02 '23 19:05 leibale

Another use case is implementing an offline queue

Would greatly appreciate your insight on this @leibale. How does the RedisClusterType client behave when there's no connection to the cluster? fwict there is no disableOfflineQueue equivalent. Could not find any docs describing this scenario

alexanderclarktx avatar Jun 03 '23 18:06 alexanderclarktx

I was able to implement a simple offline queue without these events, so they are not strictly necessary.

dtikhonov-iex avatar Jun 05 '23 13:06 dtikhonov-iex

is this already fixed?

bossajie avatar Oct 12 '23 23:10 bossajie

is this already fixed?

carlosajr avatar Oct 19 '23 06:10 carlosajr

This is still an issue. It led us to a lot of confusion about why only the error event works in cluster mode.

martinssonj avatar May 08 '24 11:05 martinssonj