Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Redis choose LRU eviction policy for only some of the keys?

Tags:

redis

Is there a way to make Redis choose a LRU (least recently used) eviction policy for only specific keys? I want a set of keys to be persistent and never be evicted if there's not enough memory. On the other hand, I want another set of keys to be freely evicted if there's low memory.

like image 974
Henley Avatar asked Mar 24 '23 02:03

Henley


1 Answers

Redis has an eviction policy which might be good for your case. You can set the maxmemory-policy to volatile-lru which causes Redis to:

remove the key with an expire set using an LRU algorithm

Which means that keys that are not set with TTL are not volatile, and therefor will not be evicted but keys that have TTL will be removed by Least-Recently-Used order.

Actually, volatile-lru is the default policy, so all you have to do is to make sure TTL is set for the keys you are willing to lose when memory is getting full.

Edit: Since version 3.0 the default eviction policy is "noeviction". (changelog)

like image 52
ofirski Avatar answered Apr 06 '23 10:04

ofirski