c# – How to get all of the keys off of a Redis partition?

I’m new to Redis and in my first year as a software engineer so thank you for your help in advance.

I’m trying to get all of the keys out of my DB in a specific partition as fast as possible. I know of two ways to do this.

Here is a picture of the code to help explain what I’m trying to do and understand:

enter image description here

Option 1 returns keys almost instantly but I can’t figure out how to combine the consumersPattern and nonConsumersPattern using something like "{2}.Keys.*", similar to what I’m doing for option 2. If I try "{2}.Keys.*", I get an error saying the value is null. As you can see, the consumersPattern + nonConsumersPattern count is 844 so there are obviously keys not being queried (when compared to the count from option 1). I want all the keys, not just the consumers and non consumers.

If I understand how this works, the {2} is the partition I’m querying. I’m trying to get all the keys on the partition which I believe is what option 2 is doing. Option 2, however, takes a really long time to run (between 30-50 seconds depending on which partition I’m querying).

How do I get all the keys on the partition and not sacrifice speed?

Here is the code used in the picture:

        private static List<RedisKey> GetKeysFromRedis(ConnectionMultiplexer connection, EndPoint endPoint,
            string keyPattern)
            IDatabase redisCache = connection.GetDatabase();

            var consumersPattern = "{2}.Keys.Consumers";
            var nonConsumersPattern = "{2}.Keys.NonConsumers";

            var redisKeysConsumers = JsonConvert.DeserializeObject<string()>(redisCache.StringGet(consumersPattern));
            var redisKeysNonConsumers = JsonConvert.DeserializeObject<string()>(redisCache.StringGet(nonConsumersPattern));

            var consumerKeys = redisKeysConsumers.Select(key => (RedisKey)key).ToList();
            var nonConsumerKeys = redisKeysNonConsumers.Select(key => (RedisKey)key).ToList();

            var totalKeys = consumerKeys.Concat(nonConsumerKeys).ToList().Count;

            var keys = connection.GetServer(endPoint).Keys(pattern: keyPattern).ToList();

            return keys;

Thanks again.