Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring redis to consistently evict older data first

Tags:

caching

redis

I'm storing a bunch of realtime data in redis. I'm setting a TTL of 14400 seconds (4 hours) on all of the keys. I've set maxmemory to 10G, which currently is not enough space to fit 4 hours of data in memory, and I'm not using virtual memory, so redis is evicting data before it expires.

I'm okay with redis evicting the data, but I would like it to evict the oldest data first. So even if I don't have a full 4 hours of data, at least I can have some range of data (3 hours, 2 hours, etc) with no gaps in it. I tried to accomplish this by setting maxmemory-policy=volatile-ttl, thinking that the oldest keys would be evicted first since they all have the same TTL, but it's not working that way. It appears that redis is evicting data somewhat arbitrarily, so I end up with gaps in my data. For example, today the data from 2012-01-25T13:00 was evicted before the data from 2012-01-25T12:00.

Is it possible to configure redis to consistently evict the older data first?

Here are the relevant lines from my redis.cnf file. Let me know if you want to see any more of the cofiguration:

maxmemory 10gb
maxmemory-policy volatile-ttl
vm-enabled no
like image 689
Ike Walker Avatar asked Jan 25 '12 21:01

Ike Walker


People also ask

How does Redis eviction work?

How the eviction process works. It is important to understand that the eviction process works like this: A client runs a new command, resulting in more data added. Redis checks the memory usage, and if it is greater than the maxmemory limit , it evicts keys according to the policy.

Which configuration setting is used to specify the memory eviction policy in Redis?

In order to specify the memory eviction policy in Redis the maxmemory configuration. directive is used so as in the data set a specified amount of memory can be used. During run time configuration set can be used to set the configuration directive.

What happens when Redis cache is full?

If this limit is reached, Redis will start to reply with an error to write commands (but will continue to accept read-only commands). You can also configure Redis to evict keys when the max memory limit is reached.

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.


Video Answer


1 Answers

AFAIK, it is not possible to configure Redis to consistently evict the older data first.

When the *-ttl or *-lru options are chosen in maxmemory-policy, Redis does not use an exact algorithm to pick the keys to be removed. An exact algorithm would require an extra list (for *-lru) or an extra heap (for *-ttl) in memory, and cross-reference it with the normal Redis dictionary data structure. It would be expensive in term of memory consumption.

With the current mechanism, evictions occur in the main event loop (i.e. potential evictions are checked at each loop iteration before each command is executed). Until memory is back under the maxmemory limit, Redis randomly picks a sample of n keys, and selects for expiration the most idle one (for *-lru) or the one which is the closest to its expiration limit (for *-ttl). By default only 3 samples are considered. The result is non deterministic.

One way to increase the accuracy of this algorithm and mitigate the problem is to increase the number of considered samples (maxmemory-samples parameter in the configuration file). Do not set it too high, since it will consume some CPU. It is a tradeoff between eviction accuracy and CPU consumption.

Now if you really require a consistent behavior, one solution is to implement your own eviction mechanism on top of Redis. For instance, you could add a list (for non updatable keys) or a sorted set (for updatable keys) in order to track the keys that should be evicted first. Then, you add a daemon whose purpose is to periodically check (using INFO) the memory consumption and query the items of the list/sorted set to remove the relevant keys.

Please note other caching systems have their own way to deal with this problem. For instance with memcached, there is one LRU structure per slab (which depends on the object size), so the eviction order is also not accurate (although more deterministic than with Redis in practice).

like image 90
Didier Spezia Avatar answered Sep 21 '22 16:09

Didier Spezia