Master Redis Memory Limits and Eviction Policies: Config, LRU, LFU Explained
This article explains how to configure Redis's maximum memory usage, modify memory limits at runtime, understand the built‑in eviction strategies—including noeviction, allkeys‑lru, volatile‑lru, allkeys‑random, volatile‑random, and volatile‑ttl—how to query and set these policies, and the details of LRU and LFU algorithms with Java examples and performance comparisons.
Redis is an in‑memory key‑value database, and because system memory is limited you can configure the maximum memory Redis may use.
Redis Memory Configuration
You can set the memory limit in the redis.conf file: maxmemory 100mb The configuration file used at startup can be specified with a command‑line argument.
Redis also allows changing the limit at runtime with commands:
127.0.0.1:6379> config set maxmemory 100mb 127.0.0.1:6379> config get maxmemoryIf no limit is set or it is set to 0, Redis has no limit on 64‑bit systems and a 3 GB limit on 32‑bit systems.
Redis Eviction Policies
When the configured memory is exhausted Redis applies one of several eviction strategies:
noeviction (default): write requests are rejected.
allkeys‑lru: LRU eviction across all keys.
volatile‑lru: LRU eviction among keys with an expiration.
allkeys‑random: random eviction across all keys.
volatile‑random: random eviction among expiring keys.
volatile‑ttl: evicts keys with the shortest remaining TTL.
When using volatile‑lru, volatile‑random or volatile‑ttl, if no key can be evicted the behavior is the same as noeviction.
Getting and Setting the Eviction Policy
Query the current policy: 127.0.0.1:6379> config get maxmemory-policy Set the policy via the configuration file: maxmemory-policy allkeys-lru Or set it at runtime:
127.0.0.1:6379> config set maxmemory-policy allkeys-lruLRU Algorithm
LRU (Least Recently Used) evicts the least recently accessed items when memory is full. A simple Java implementation is provided:
public class LRUCache<K, V> {
private int capacity;
private int count;
private Map<K, Node<K, V>> nodeMap;
private Node<K, V> head;
private Node<K, V> tail;
// constructor, put, get, removeNode, removeFromList, addNode, addToHead, moveNodeToHead implementations ...
}
class Node<K, V> {
K key;
V value;
Node<K, V> pre;
Node<K, V> next;
public Node(K key, V value) { this.key = key; this.value = value; }
}LRU in Redis
Redis uses an approximate LRU algorithm that samples a configurable number of keys (default 5) and evicts the least recently used among the sample. The sample size can be changed with maxmemory-samples: maxmemory-samples 10 Redis 3.0 improves this with a candidate pool of 16 keys, maintaining a sorted list of recent accesses for more accurate eviction.
LFU Algorithm
Redis 4.0 introduced LFU (Least Frequently Used) eviction, which removes keys that are accessed the least often. Two policies are available:
volatile‑lfu: LFU among keys with an expiration.
allkeys‑lfu: LFU among all keys.
LFU policies can only be set on Redis 4.0 or newer; attempting to set them on older versions results in an error.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
