redis / ioredis

🚀 A robust, performance-focused, and full-featured Redis client for Node.js.
MIT License
14.41k stars 1.2k forks source link

Cannot get keyspace events under Cluster mode #1240

Open sj82516 opened 4 years ago

sj82516 commented 4 years ago

Issue description

I ran a Redis Cluster on local and I cannot get key expired event from ioredis Cluster.

Steps to reproduce the issue

  1. Start Redis Cluster
  2. Set the notify-keyspace-events: xE for every Redis nodes
  3. here is my code. I have tried to change nodes one by one. Only some of nodes could get partial expired events. I can get all the expired events after combined all the results.
    
    import Redis from 'ioredis';

async function main(){ const nodes = [ { host: '192.168.100.104', port: 7005, } ]; const sub = new Redis.Cluster(nodes); await sub.psubscribe('*');

sub.on('pmessage', function(pattern, message, channel) {
    console.log(channel, message);
});

const pub = new Redis.Cluster(nodes);
// @ts-ignore - TS2339: Property 'publish' does not exist on type 'Cluster'.
for(let i = 0; i < 100; i++){
    pub.set(i, 'updated', 'ex', 1);
}

}

main();



## what I expected
I expect the ioredis would subscribe to all nodes under the key events mode.
I have tried to modify ioredis source code to subscribe all nodes and it works as expected.    
So I would like to introduce new option to Cluster mode `subscribeToAll` for this situation which should be differentiated from normal Pub/Sub.   
I would like to open PR for this.

## Doc
When I read the Redis official doc Redis Keyspace Notifications
> Every node of a Redis cluster generates events about its own subset of the keyspace as described above. However, unlike regular Pub/Sub communication in a cluster, events' notifications are not broadcasted to all nodes. Put differently, keyspace events are node-specific. This means that to receive all keyspace events of a cluster, clients need to subscribe to each of the nodes.

Which is different from ioredis doc at Cluster > Pub/Sub paragraph
> Pub/Sub in cluster mode works exactly as the same as in standalone mode. Internally, when a node of the cluster receives a message, it will broadcast the message to the other nodes. ioredis makes sure that each message will only be received once by strictly subscribing one node at the same time.
JorgeReus commented 3 years ago

Hi All! Is there any progress on this?

mario-grgic commented 3 years ago

Just hit this as well. It would be useful to fix or document a workaround.

lil-shake-code commented 1 month ago

I just hit this too....

lil-shake-code commented 1 month ago

I subscribed to all nodes individually, so this is a workaround.