Mastering Redis Memory Limits and Eviction Policies: Config, LRU, LFU Explained
Redis, an in‑memory key‑value store, lets you set a maximum memory limit via configuration files or runtime commands, choose among various eviction strategies such as noeviction, allkeys‑lru, volatile‑lru, and LFU, and understand how approximate LRU works, with Java code examples illustrating these concepts.
Redis is an in‑memory key‑value database. Because system memory is limited, you can configure the maximum memory Redis may use.
Configuring Redis Memory
1. Through the configuration file Add the following to redis.conf: maxmemory 100mb The configuration file used may not be the default redis.conf; you can specify a different file when starting Redis.
2. Through runtime commands You can change the limit while Redis is running:
127.0.0.1:6379> config set maxmemory 100mb
127.0.0.1:6379> config get maxmemoryIf maxmemory is not set or set to 0, Redis on 64‑bit systems has no limit, while on 32‑bit systems it caps at about 3 GB.
Redis Eviction Policies
When the configured memory is exhausted, Redis applies one of several eviction strategies:
noeviction (default): write commands are rejected.
allkeys‑lru: LRU eviction among all keys.
volatile‑lru: LRU eviction among keys with an expiration.
allkeys‑random: random eviction among all keys.
volatile‑random: random eviction among expiring keys.
volatile‑ttl: evict keys with the nearest expiration time.
For the volatile‑lru, volatile‑random, and volatile‑ttl policies, if no key is eligible for eviction the behavior is the same as noeviction.
Getting and setting the eviction policy
127.0.0.1:6379> config get maxmemory-policy
127.0.0.1:6379> config set maxmemory-policy allkeys-lruLRU Algorithm
What is LRU? Least Recently Used removes the least recently accessed items when the cache is full.
Java example of a simple LRU cache:
public class LRUCache<k, v> {
// capacity
private int capacity;
private int count;
private Map<k, Node<k, v>> nodeMap;
private Node<k, v> head;
private Node<k, v> tail;
// constructor and methods (addNode, removeNode, moveNodeToHead, etc.)
// ...
}Approximate LRU in Redis
Redis implements an approximate LRU by randomly sampling a few keys (default 5) and evicting the least recently used among them. The sample size can be changed with maxmemory-samples. Each key stores a 24‑bit timestamp for its last access.
Redis 3.0 improves this with a candidate pool of 16 keys, sorted by last access time, providing a closer approximation to true LRU.
LFU Algorithm
Since Redis 4.0, the Least Frequently Used (LFU) policy is available. It evicts keys that are accessed least often. Two variants exist:
volatile‑lfu: applies to keys with an expiration.
allkeys‑lfu: applies to all keys.
LFU policies can only be set on Redis 4.0 or newer.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
