Redis command to get all available keys on Redis Cluster?
Asked Answered
R

6

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?

Respiratory answered 17/3, 2016 at 6:48 Comment(6)
Shouldn't all nodes of a cluster share the same data and give the same result? Or are we talking multiple clusters here?Karlin
node-redis-cluster can do this. You can check it here: npmjs.com/package/node-redis-clusterKadner
@john Siu, all nodes giving separate dataRespiratory
can you show how you are setting the cluster value ?Aramaic
Or you can use redisClient.zrange("example", 0, -1, function(err, replies) {Aramaic
Which package are you using?Protectionist
N
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]])];
    })
}
Nellynelms answered 31/8, 2016 at 21:41 Comment(2)
@Albin Mathew Does it help?Nellynelms
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.Earphone
H
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();
    }
});
Heterocercal answered 24/2, 2017 at 8:53 Comment(0)
J
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]
    }
    */
    });
Jalisajalisco answered 29/3, 2017 at 15:43 Comment(0)
E
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);
            });
Earphone answered 10/10, 2021 at 19:3 Comment(0)
R
0

I converted this from my C# code. You need to query across all servers and build your list of keys.

const Redis = require('ioredis');

async function getKeyList(servers, pattern) {
    const keyList = [];

    for (const server of servers) {
        if (!server.isConnected) continue;

        const keys = await server.keys(pattern);

        if (keys.length === 0) continue;

        keyList.push(...keys);
    }

    return keyList;
}

// Example usage:
const servers = [
    new Redis({ host: 'server1', port: 6379 }),
    new Redis({ host: 'server2', port: 6379 }),
    // Add more server configurations as needed
];

const pattern = 'your_pattern_here';

getKeyList(servers, pattern)
    .then((keyList) => {
        console.log('Keys:', keyList);
    })
    .catch((error) => {
        console.error('Error:', error);
    });
Rebeckarebeka answered 15/11, 2023 at 14:33 Comment(0)
I
-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]);
  }
});  
Inherent answered 29/3, 2018 at 6:3 Comment(1)
this is a solution for a standalone redis server, not for a redis cluster as the question states.Aorist

© 2022 - 2024 — McMap. All rights reserved.