node-redis
node-redis copied to clipboard
Redis cluster not emitting events
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
Currently the cluster emits errors only, wanna share the use case of those events in a cluster?
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.
👍 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.
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
Another use case is implementing an offline queue. I need this to work in order to migrate v3 code that relies on this functionality.
Adding this to the v5 backlog. The events I have in mind are:
-
connect
-
node-connect
-
node-ready
-
ready
-
reconnecting
-
error
is there any event that I'm missing?
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
I was able to implement a simple offline queue without these events, so they are not strictly necessary.
is this already fixed?
is this already fixed?
This is still an issue. It led us to a lot of confusion about why only the error event works in cluster mode.