18

I am using this

redisManager.redisClient.keys('*example*', function (err, keys) {
})

But it only gives keys from only one of the redis cluster. How can I get keys from all cluster?

zangw
  • 43,869
  • 19
  • 177
  • 214
Albin Mathew
  • 604
  • 9
  • 19

5 Answers5

1

You can't get keys for all nodes using a single command. You have to get keys for all nodes and merge them. Reference - https://github.com/antirez/redis/issues/1962

You can do something like.

var redis = require('redis');

redisConfig = new Array(
    {"port": 1234, "host": "192.168.1.2"},
    {"port": 5678, "host": "192.168.1.3"}
);

keys    = new Array();
allKeys = new Array();

for(i = 0; i < redisConfig.length; i++){
    redisClient = redis.createClient(redisConfig[i].port, redisConfig[i].host);    
      keys[i] = redisClient.keys('*example*', function (err, keys) {
      allkeys = [...new Set([...allKeys ,...keys[i]])];
    })
}
Shivam Mathur
  • 2,543
  • 19
  • 26
  • @Albin Mathew Does it help? – Shivam Mathur Sep 01 '16 at 17:31
  • keys command is slower in redis and not recommend. Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets. – Asif Saeed Oct 10 '21 at 18:59
1

Have you tried using the node-redisscan package? It allows you to use SCAN commands on Redis clusters.

You'd have to collect each matching key with the callback. Here's how you'd do it (directly lifted from the README file) -

var redisScan = require('redisscan');
var redis = require('redis').createClient();

redisScan({
    redis: redis,
    each_callback: function (type, key, subKey, value, done) {
        console.log(type, key, subKey, value);
        done();
    },
    done_callback: function (err) {
        if (err) throw err;
        redis.quit();
    }
});
GPX
  • 3,506
  • 10
  • 52
  • 69
1

You can use this code inorder to find keys from all cluster. Please refer to link for more info.

    var RedisCluster = require('node-redis-cluster').RedisCluster;

    var rcluster = RedisCluster.create([
        { port: 6379, host: '10.0.0.1' },
        { port: 6379, host: '10.0.0.2' },
        { port: 6379, host: '10.0.0.3' },
    ]);
    rcluster.execAll('keys', ['*'], function(err, results) {
    /* results will be
    {
        '10.0.0.1:6379': [ // keys],
        '10.0.0.2:6379': [ // keys],
        '10.0.0.3:6379': [ // keys]
    }
    */
    });
Tolsee
  • 1,695
  • 14
  • 23
0

Keys command not recommended to be used in productions mode.

From redis docs Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.

Use scanStream Instead

            redisConnection = redisConnectionObject;

            const slaves = redisConnection.nodes("slave");

            config.info('number of slaves', slaves.length)


            let keys = [];
            let slavePromises = [];

            slaves.forEach((slave) => {
                let slaveCreate = new Promise((resolve) => {

                    const stream = slave.scanStream({
                        match: prefix
                    });

                    stream.on("data", (resultKeys) => {
                        keys = [...keys, ...resultKeys];
                    });
                    stream.on("end", () => {
                        resolve();
                    });
                });

                slavePromises.push(slaveCreate);
            });

            Promise.all(slavePromises).then(([slaveOneData, slaveTwoData]) => {

                keys = _.uniq(keys);

                resolveOne(keys);
            });
Asif Saeed
  • 1,985
  • 14
  • 24
-1

Run command $ npm install redis to install redis.

var redis = require('redis'),
    client = redis.createClient();

client.keys('*', function (err, keys) {
  if (err) return console.log(err);

  for(var i = 0, len = keys.length; i < len; i++) {
    console.log(keys[i]);
  }
});  
Muhammad Faizan
  • 1,709
  • 1
  • 15
  • 37
KrGyan
  • 22
  • 2
  • this is a solution for a standalone redis server, not for a redis cluster as the question states. – Asalle Apr 05 '19 at 12:40