Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis maxmemory-policy: performances of volatile-lru vs allkeys-lru

assuming all keys in a redis instance have an expire set, volatile-lru and allkeys-lru are similar. But is there a significative performance difference between the 2 when a key is removed?

Bonus question:

between 2 distinct instances configured with the allkeys-lru policy, having the same content and same configuration, except:

  • Instance A has all its keys with an expire set (different values of expire)
  • Instance B has none key with an expire set

Aside the overhead of memory in instance A due to the expires bits, is there a performance difference between the 2 when a key is removed by the allkeys-lru algorithm?

In both cases, I'm talking about instances of redis 2.4.x on linux 64 bits with maxmemory = 3Gb with 4-5000 keys when the maxmemory is reached (most of the keys are hashes).

Thanks

like image 598
colinux Avatar asked Oct 15 '12 09:10

colinux


People also ask

What is Allkeys LRU?

allkeys-lru: Keeps most recently used keys; removes least recently used (LRU) keys. allkeys-lfu: Keeps frequently used keys; removes least frequently used (LFU) keys. volatile-lru: Removes least recently used keys with the expire field set to true .

Is Redis cache volatile?

Eviction policyThe default policy for Azure Cache for Redis is volatile-lru , which means that only keys that have a TTL value set with a command like EXPIRE are eligible for eviction.

What is Maxmemory in Redis?

Maxmemory is a Redis configuration that allows you to set the memory limit at which your eviction policy takes effect. Memorystore for Redis designates this configuration as maxmemory-gb . When you create an instance, maxmemory-gb is set to the instance capacity.

How do I check my Redis Maxmemory policy?

First, the client runs the command to add new data. Before the command is executed, Redis check if the memory usage is higher than the set maxmemory limit. If the limit is reached, it uses the specified eviction policy to remove the keys. Finally, the command is executed, and new data is added.


1 Answers

redis.c, line 2311, unstable branch:

/* volatile-lru and allkeys-lru policy */
else if (server.maxmemory_policy == REDIS_MAXMEMORY_ALLKEYS_LRU ||
    server.maxmemory_policy == REDIS_MAXMEMORY_VOLATILE_LRU)
{
    for (k = 0; k < server.maxmemory_samples; k++) {
        sds thiskey;
        long thisval;
        robj *o;

        de = dictGetRandomKey(dict);
        thiskey = dictGetKey(de);
        /* When policy is volatile-lru we need an additonal lookup
         * to locate the real key, as dict is set to db->expires. */
        if (server.maxmemory_policy == REDIS_MAXMEMORY_VOLATILE_LRU)
            de = dictFind(db->dict, thiskey);
        o = dictGetVal(de);
        thisval = estimateObjectIdleTime(o);

        /* Higher idle time is better candidate for deletion */
        if (bestkey == NULL || thisval > bestval) {
            bestkey = thiskey;
            bestval = thisval;
        }
    }
}

It seems like all things being equal allkeys-lru would be strictly speaking faster, but not by a significant magnitude. Chances are we are talking about not much more than a fraction of a microsecond faster.

The second question got pretty much already answered, but just in case: it looks like it makes no difference to allkeys-lru how many keys are set to expire, or if any are. Both Instance A and B in your example would see the same performance when a key is purged by the lru algorithm.

like image 179
Mahn Avatar answered Oct 02 '22 12:10

Mahn