Search code examples
stackexchange.redisazure-redis-cache

StackExchange.Redis server.Keys(pattern:"IsVerySlow*")


I'm new to redis so I'm doing something wrong, I'm sure:

I've stored roughly 16,000 key/values in Azure Redis.

I used the following to write the keys/values

      foreach (var worksheet in wksList)
      {
        var wksYYMM = string.Format("{0}{1:00}", worksheet.ReviewDt.Year, worksheet.ReviewDt.Month);
        var wksKey = string.Format("{0}:{1}:{2}", provCode, wksYYMM, worksheet.AcctNbr);
        string jsonStr = JsonConvert.SerializeObject( MakeWsListDto(worksheet, provCoderList, rvrList));
        cache.StringSet(wksKey, jsonStr);
      }

so my keys look like this: "AP:201401:AZ5798BK"

When I try a lookup like:

    var keys = server.Keys(pattern: "AP:201401:*"); // returns in milliseconds
    var keyAry = keys.ToArray(); // returns in over one minute
    (note: this returns 12 keys)

it takes 1 min 12 seconds to return the keys. Once I have the keys it takes milliseconds to retrieve the values for those. If I iterate over the value of keys and return the values I get a similar result. I did a ToArray() just to isolate the problem.

If I try the same query in redis-cli.exe it comes back in milliseconds.

Am I using this command incorrectly?


Solution

  • server.Keys automatically selects between KEYS and the preferred SCAN based on server version. I suspect what is happening is that you are thus using SCAN with a too-small page-size. There is an optional parameter for page size. Try specifying something considerably larger than the default - hundreds, thousands, etc. If not specified, the page-size uses the Redis SCAN default of 10, which could cause a lot of round-trips to be required.