Back to results for “Caching”
Redis › Expiration & Eviction

What is the difference between the LRU and LFU eviction algorithms, and when would you choose one over the other?

Intermediate

Answer

LRU, meaning least recently used, evicts the key that has not been accessed for the longest time, while LFU, meaning least frequently used, evicts the key that has been accessed the fewest number of times overall, with LFU generally being a better choice when some keys are accessed very frequently over a long period even if there are occasional gaps between accesses.
CONFIG SET maxmemory-policy allkeys-lfu
Real-world example A recommendation engine that repeatedly accesses certain popular product keys throughout the day, but with occasional gaps, chooses LFU eviction to avoid accidentally evicting those consistently popular keys just because they were not accessed in the last few minutes.

Common follow-ups: How does Redis actually track access frequency for the LFU algorithm?;Which eviction policy is the more commonly recommended default for a general purpose cache?