Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RedisCacheManager Not Updating keyspace_misses

I’m using the spring-boot spring-data-redis 1.8.9.RELEASE RedisCacheManager implementation of CacheManager for caching. One metric that I want visibility into is the cache hit/miss ratio. To get that, I’m extracting the keyspace_hits and keyspace_misses exposed via the redis server which can also be viewed via the redis_cli with INFO STATS. The problem is that RedisCacheManager never registers cache misses, i.e. keyspace_misses never increments even if there is a cache "miss".

Debugging the code, I see that spring-data-redis actually checks to see if the key EXISTS in redis before retrieving it. I see the sense with this approach however when EXISTS is executed against the redis server, it does not register a cache miss.

Is there any way to use RedisCacheManager and register cache misses? I know I can use other redis objects to accomplish this but I was wondering if it could be done with the standard CacheManager implementation?

Edit

The ideal solution won't add a great deal of overhead and I am unable to edit the configuration of the redis server.

Code that RedisCacheManager uses when retrieving an element from cache. Notice Boolean exists:

public RedisCacheElement get(final RedisCacheKey cacheKey) {
    Assert.notNull(cacheKey, "CacheKey must not be null!");
    Boolean exists = (Boolean)this.redisOperations.execute(new RedisCallback<Boolean>() {
        public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
            return connection.exists(cacheKey.getKeyBytes());
        }
    });
    return !exists ? null : new RedisCacheElement(cacheKey, this.fromStoreValue(this.lookup(cacheKey)));
}

The above code will execute these commands on redis viewable via MONITOR on a cache miss. Notice again that EXISTS is executed as per the code:

Redis commands executed for a cache miss

After the above commands are executed, keyspace_misses is not incremented even though there was a cache miss:

enter image description here

like image 693
Always Learning Avatar asked Apr 25 '18 22:04

Always Learning


People also ask

How do I debug keyspace misses in Redis?

See the Appendix of Debugging Redis Keyspace Misses for information about using keyspace notifications or MONITOR to debug these types of issues. Redis clients can issue the DEL or HDEL command to explicitly remove keys from Azure Cache for Redis. You can track the number of delete operations by using the INFO command.

Why are keys not available on azure cache for Redis?

Keys are not available on a replica because of data-replication delays. Azure Cache for Redis removes a key automatically if the key is assigned a time-out and that period has passed. For more information about Redis key expiration, see the EXPIRE command documentation.

Does Redis Cache Manager have a cache manager?

We didn’t have any Cache Manager or anything, but we were able to cache data. In this post, we will show how to use RedisCacheManager to cache the data. This manager can further be extended to customize the caching configuration even more. But we will not be looking into customization in this post particularly.

What is keyspace_hits in Redis?

The number of successful key lookups during the specified reporting interval. This number maps to keyspace_hits from the Redis INFO command. The latency of the cache calculated using the internode latency of the cache. This metric is measured in microseconds, and has three dimensions: Avg, Min, and Max.


1 Answers

The code mentioned in the question is part of RedisCache provided by Spring.

  1. Extend and Create a custom implementation of RedisCache class to override the behavior of "get" method to suit your need.
  2. Extend RedisCacheManager to override the method "createRedisCache" to use your custom RedisCache that you created in first step instead of default cache.
like image 67
sm19 Avatar answered Sep 22 '22 11:09

sm19