aspnetcore: Items with configured expiration stored in IDistributedCache (using RedisCache) end up non-expiring in Redis

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When I store items using e.g.

// ...
services.AddStackExchangeRedisCache(_ => { })
// ...
IDistributedCache cache = // from DI
cache.SetAsync(
    "key",
    "Test"u8.ToArray(),
    new() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10) })

the key ends up as non-expiring in Redis.

Expected Behavior

Keys should be set to expire in Redis if configured accordingly.

Steps To Reproduce

Unfortunately I can only reproduce this in a very specific environment:

  • client with Microsoft.Extensions.Caching.StackExchangeRedis @ 9.0.0 (SE.Redis @ 2.7.27 or 2.8.22) on Suse CloudFoundry
  • Bitnami Redis v7.0.4 (yes, outdated)

Connecting to the same cluster from my development environment (Ubuntu) or downgrading Microsoft.Extensions.Caching.StackExchangeRedis @ 8.0.11 does not trigger the bug (regardless of the SE.Redis version).

Exceptions (if any)

No response

.NET Version

SDK 9.0.100, targeting net9.0

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 31 (19 by maintainers)

Most upvoted comments

On an internal chat, @philon-msft echoed the sentiment that the addresses returned from CLUSTER NODES should be routable by the client. If they’re not, that would 100% explain these symptoms because:

  • initially, the set of nodes will consist of the host-based address in the connection string
  • CLUSTER NODES will add some IP-based endpoints, but they will all fail to connect
  • when routing commands, the library will fail to find a resolvable live endpoint, and instead use a “hail mary” strategy of sending it to the only live endpoint it has - where-ever we got to via the connection string
  • this will generate -MOVED that seems to include the host on redis 7
  • we redirect via the -MOVED, adding extra endpoints as needed
  • which are then included in the “hail mary” routing, but will not be used for the primary routing
  • we now have multiple “hail mary” destinations, which can lead to ordering problems (think “HMSET gets -MOVED, but the EXPIRE randomly got the correct server first time, so the ‘correct’ server gets EXPIRE then HMSET”)
  • chaos, bad things, human sacrifice, dogs and cats living together, MASS HYSTERIA!

So; long term, we can see whether SE.Redis should improve support for host-based CLUSTER routing, but right now, the short version would be: make the CLUSTER NODES advertised endpoints routable.

@CaringDev thoughts?

@CaringDev if I can refer you to CLUSTER NODES, emphasis mine:

  1. ip:port@cport: The node address that clients should contact to run queries, along with the used cluster bus port. :0@0 can be expected when the address is no longer known for this node ID, hence flagged with noaddr.
  2. hostname: A human readable string that can be configured via the cluster-annouce-hostname setting. The max length of the string is 256 characters, excluding the null terminator. The name can contain ASCII alphanumeric characters, ‘-’, and ‘.’ only.

It seems fairly explicit that the IP endpoint is the correct case, and that the text name (which is configurable) is intended for reference only. This is similarly the case in CLUSTER SHARDS, which has a different layout but again specifies that the IP and port is the expected use-case, and that the hostname is optional and may or may not be included.

The other thing we could do is that code is: in EXPIRE, check (bool)resp - it should be true:

                  int validated = 0, tooSoon = 0, serverSaidNo = 0;
...
                        case "EXPIRE":
                            if (!(bool)resp) serverSaidNo++;
                            if (keys.Remove((string)req[1]!))
                            {
                                validated++;
                            }
                            else
                            {
                                tooSoon++;
                            }
                            break;
...
            Console.WriteLine($"{validated} valid, {keys.Count} left unexpired, {tooSoon} expired too soon, {serverSaidNo} rejected by server");

and output serverSaidNo at the end; we hope it is 0, but it’ll be interesting to see if it is non-zero

(being able to replay and analyze traffic snapshots like this is super useful)

To be clear: what I did here was

  • manually search in the file fie each EXPIRE entry
  • select the guid for that EXPIRE, which in my editor automatically shows other occurrences
  • look (just eyeballs) for where else it occurs, and in each case: it is in a HMSET shortly before the EXPIRE