NOTE

5.3 Redis Memory Eviction Strategy

When Redis reaches its configured memory limit, the selected policy determines what happens next. These notes examine the behavior of different eviction policies.

Redis / CacheCreated Updated 2 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. What Is a Redis Memory Eviction Strategy?

Once a maximum memory limit is configured, Redis uses its memory eviction strategy to handle cases where memory is full.

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.
    • Use when all keys are accessed repeatedly in a cycle.
  • 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

  1. The client executes a command that adds more data.
  2. Redis checks memory usage. If it is above the maxmemory limit, keys are deleted according to the strategy.
  3. 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.

5. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub