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

1

8

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.

Louralourdes answered 4/5, 2013 at 3:27 Comment(1)
Redis allows you to set timeToLive at the key level. You could select the keys you want should expire in the application and set some appropriate expiration time.Indifference
R
11

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)

Rajasthan answered 2/7, 2013 at 19:57 Comment(1)
What if I have a noeviction policy but expire set on keys?Priesthood

© 2022 - 2024 — McMap. All rights reserved.