Master Redis Memory Limits and Eviction: Configuring Maxmemory, LRU & LFU Explained
This article explains how to set Redis's maximum memory usage via configuration files or runtime commands, details all built‑in eviction policies, demonstrates retrieving and changing these policies, and dives into the LRU and LFU algorithms—including Java sample code and Redis's approximate LRU implementation.
Configuring Redis Max Memory
Redis stores data in memory, so you can limit its usage by setting maxmemory either in the redis.conf file or with the CONFIG SET command at runtime.
maxmemory 100mb 127.0.0.1:6379> CONFIG SET maxmemory 100mb
127.0.0.1:6379> CONFIG GET maxmemoryIf maxmemory is 0 or unset, Redis on 64‑bit systems has no limit; on 32‑bit systems it caps at ~3 GB.
Redis Eviction Policies
noeviction (default): write commands are rejected when memory is exhausted.
allkeys-lru : evicts the least‑recently‑used key among all keys.
volatile-lru : evicts LRU among keys with an expiration.
allkeys-random : evicts a random key among all keys.
volatile-random : evicts a random key among expiring keys.
volatile-ttl : evicts keys with the nearest expiration time first.
When using volatile-lru , volatile-random or volatile-ttl , if no eligible key exists Redis behaves like noeviction .
Getting and Setting the Eviction Policy
127.0.0.1:6379> CONFIG GET maxmemory-policySet via configuration file: maxmemory-policy allkeys-lru Or change at runtime:
127.0.0.1:6379> CONFIG SET maxmemory-policy allkeys-lruLRU Algorithm
LRU (Least Recently Used) evicts the data that has not been accessed for the longest time, assuming it is unlikely to be needed soon.
Java Simple LRU Implementation
public class LRUCache<K,V> {
private int capacity;
private int count;
private Map<K,Node> nodeMap;
private Node head;
private Node tail;
public LRUCache(int capacity) {
if (capacity < 1) throw new IllegalArgumentException(String.valueOf(capacity));
this.capacity = capacity;
this.nodeMap = new HashMap<>();
Node headNode = new Node(null, null);
Node tailNode = new Node(null, null);
headNode.next = tailNode;
tailNode.pre = headNode;
this.head = headNode;
this.tail = tailNode;
}
public void put(K key, V value) {
Node node = nodeMap.get(key);
if (node == null) {
if (count >= capacity) removeNode();
node = new Node(key, value);
addNode(node);
} else {
moveNodeToHead(node);
}
}
public Node get(K key) {
Node node = nodeMap.get(key);
if (node != null) moveNodeToHead(node);
return node;
}
private void removeNode() {
Node node = tail.pre;
removeFromList(node);
nodeMap.remove(node.key);
count--;
}
private void removeFromList(Node node) {
Node pre = node.pre;
Node next = node.next;
pre.next = next;
next.pre = pre;
node.next = null;
node.pre = null;
}
private void addNode(Node node) {
addToHead(node);
nodeMap.put(node.key, node);
count++;
}
private void addToHead(Node node) {
Node next = head.next;
next.pre = node;
node.next = next;
node.pre = head;
head.next = node;
}
public void moveNodeToHead(Node node) {
removeFromList(node);
addToHead(node);
}
class Node {
K key;
V value;
Node pre;
Node next;
Node(K key, V value) { this.key = key; this.value = value; }
}
}Approximate LRU in Redis
Redis implements an approximate LRU using random sampling. By default it samples five keys; the one with the oldest access time is evicted. The sample size can be changed with maxmemory-samples, e.g., maxmemory-samples 10 makes the behavior closer to true LRU.
Each key stores a 24‑bit field recording its last access time. Redis 3.0 adds a candidate pool of 16 entries, sorted by access time, improving eviction accuracy.
LFU Algorithm (Redis 4.0+)
LFU (Least Frequently Used) evicts keys that have been accessed the fewest times. It better reflects key “heat”. Two policies are available:
volatile-lfu : LFU among keys with an expiration.
allkeys-lfu : LFU among all keys.
LFU policies are only supported in Redis 4.0 and later; using them on older versions results in an error.
Open Question
Why does Redis choose an approximate LRU algorithm instead of a strict LRU implementation? The article leaves this as a discussion point for readers.
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
