First, you need to configure the Redis conf file, which involves the LRU related configuration A total of three are:
MaxMemory, sets the maximum memory size that Redis uses to hold data, and once it exceeds this memory size, it immediately cleans up some of the data using the LRU algorithm
Maxmemory-policy, you can set the memory to the maximum idle, what policy to take to handle
(1) Noeviction: If the memory is used to reach maxmemory,client and continue to write data, then directly error to the client
(2) Allkeys-lru: Is what we often call the LRU algorithm, remove the least recently used keys corresponding data, PS the longest use of the strategy
(3) Volatile-lru: Also take the LRU algorithm, but only for those setting the specified time-to-live (TTL) key will be cleaned out
(4) Allkeys-random: Randomly select some key to delete
(5) Volatile-random: Randomly select some keys with TTL set to delete
(6) Volatile-ttl: Remove some keys and select the keys that have a shorter TTL time
LRU uses the approximate algorithm, starting from 3.0 LRU algorithm introduced the pool mechanism, the performance can be comparable with the real LRU algorithm, after sampling the LRU cleanup and the traditional full-volume LRU slightly different.
Configuration using Maxmemory-samples, such as 5, can set the size of the sample, if set to 10, then the effect is better, but also consumes more CPU resources
This is the explanation for the Redis LRU cache cleanup related issues.
Redis LRU Cache cleanup algorithm for detailed understanding and related configuration