NOTE
5.3 Redis Memory Eviction Strategy
Redis memory eviction strategies, configuration, eviction process, approximate LRU, and LFU.
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Is a Redis Memory Eviction Strategy?
After Redis has a maximum memory limit configured, if the space becomes full, it handles the situation according to the memory eviction strategy.
2. How to Configure the Redis Memory Eviction Strategy
2.1. Eviction Strategies
noevicion- Do not evict data; inserting new data directly returns an error.
allkeys-lru- Select the least recently used data from the dataset and evict it until enough space is freed.
- A small subset of the dataset is accessed much more frequently than the rest. Use this when unsure.
allkeys-random- Randomly select data from the dataset for eviction.
- Periodic continuous access to all keys.
volatile-lru- Select the least recently used data from the dataset with expiration times and evict it until enough space is freed.
- Run a single Redis instance and cache a set of keys that do not expire.
volatile-random- Randomly select data from the dataset with expiration times and evict it until enough space is freed.
- Run a single Redis instance and cache a set of keys that do not expire.
volatile-ttl- Select data that is about to expire from the dataset with expiration times and evict it until enough space is freed.
- Redis determines which entries are most suitable for expiration according to their TTL values.
2.2. Configuration
# Configure maximum memory. If it is 0, there is no limit
maxmemory 100mb
# Eviction strategy to use when memory is full
maxmemory-policy <eviction-strategy>
3. Redis Memory Eviction Process

- The client executes a command that adds more data.
- Redis checks memory usage. If it is above the
maxmemorylimit, keys are deleted according to the strategy. - Continue with step 1.
4. Redis Memory Eviction Implementation
4.1. LRU
Design an LRU Cache Structure Redis’s LRU is an approximate LRU. It does not introduce a linked list to record recently used elements. Instead, it uses random sampling on top of the existing data structures to evict elements. A last-access timestamp is added to each key. When memory is insufficient, an approximate LRU algorithm runs: it randomly samples 5 keys, with the default sample count being 5, and evicts the oldest key according to the timestamps.
4.2. LFU
Starting with Redis 4.0, two LFU modes were added to the maxmemory_policy eviction strategies:
volatile-lfu: use the LFU eviction algorithm for keys with expiration times.allkeys-lfu: use the LFU eviction algorithm for all keys. Redis counts key access frequency, and keys with very low access frequency are evicted.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub