ioredis icon indicating copy to clipboard operation
ioredis copied to clipboard

Cannot get keyspace events under Cluster mode

Open sj82516 opened this issue 4 years ago • 4 comments

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.

sj82516 avatar Nov 18 '20 22:11 sj82516