Master Redis Memory Management: Expiration & Eviction Strategies Explained
Redis employs distinct memory reclamation mechanisms—expiration policies (timed, lazy, periodic) and eviction strategies triggered when maxmemory is reached—each with trade-offs in CPU and memory usage, and offers configurable policies such as allkeys‑lru, volatile‑ttl, and noeviction to manage cache data effectively.
1. Redis Memory Reclamation Mechanism
Redis memory reclamation consists of two parts: the expiration policy, which deletes data that has exceeded its TTL, and the eviction policy, which is triggered when memory usage reaches the maxmemory limit.
Note that expiration and eviction are separate mechanisms and should not be confused in practice.
2. Redis Expiration Policies
Redis provides three expiration strategies: timed expiration, lazy expiration, and periodic expiration.
Timed expiration creates a timer for keys with an expiration time; the key is removed immediately when the timer expires. This clears expired data promptly, benefiting memory, but consumes significant CPU resources, affecting response time and throughput.
Lazy expiration checks a key’s TTL only when the key is accessed; if it has expired, it is removed. This saves CPU but can leave many expired keys in memory if they are never accessed again, leading to high memory usage.
Periodic expiration scans a portion of the expires dictionary at regular intervals, removing keys that have expired. It balances CPU and memory usage by adjusting the scan interval and the time budget per scan.
Redis typically uses both lazy and periodic expiration together.
3. Redis Eviction Policies
When memory reaches the maxmemory limit, Redis applies an eviction algorithm to free space. The default algorithm is LRU (least recently used), but Redis does not strictly delete the absolute least‑recently used keys; it samples three random keys and evicts the least recently used among them. The sample size can be configured (e.g., to 10) at the cost of additional CPU.
Configuration options:
maxmemory sets the maximum memory Redis can use. Exceeding this triggers eviction.
maxmemory-policy selects the eviction strategy. Main policies include:
allkeys-lru: evicts the least recently used key among all keys when memory is insufficient.
allkeys-random: evicts a random key among all keys.
noeviction: rejects new writes when memory is full.
volatile-random: evicts a random key among keys with an expiration set.
volatile-ttl: evicts keys with the nearest expiration time among keys with an expiration set.
volatile-lru: evicts the least recently used key among keys with an expiration set.
Note: Redis uses allkeys-lru as the default eviction policy.
The eviction process works as follows: the client issues a write operation; the Redis server checks the maxmemory limit; if the limit is exceeded, it applies the configured policy to remove some keys; the server then completes the write and returns the result to the client.
4. Summary
Redis’s eviction policies address memory shortage by removing data when needed, while expiration policies handle keys that have naturally expired. Eviction does not interfere with the handling of expired keys.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITFLY8 Architecture Home
ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
