How to Configure Redis Memory Limits and Eviction Policies (LRU & LFU)
This article explains how to set Redis's maximum memory usage via configuration files or runtime commands, details the various eviction strategies including noeviction, LRU, random, and LFU, and provides Java code for a simple LRU cache implementation.
Redis Memory Usage
Redis is an in‑memory key‑value store; you can set a maximum memory limit using the configuration file or runtime commands.
1. Set via configuration file
Add maxmemory 100mb to redis.conf. Note that the config file used may be specified when starting Redis.
2. Set via command
Use CONFIG SET maxmemory 100mb and CONFIG GET maxmemory to view the setting. If maxmemory is 0 or not set, Redis on 64‑bit systems has no limit, while on 32‑bit it caps at ~3 GB.
Redis Eviction Policies
When memory is exhausted Redis can evict keys according to several policies:
noeviction – write commands return an error.
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 shortest TTL first.
For the LRU‑based policies, if no key is eligible, Redis behaves like noeviction .
How to get and set the eviction policy
Get current policy: CONFIG GET maxmemory-policy Set via redis.conf: maxmemory-policy allkeys-lru Set via command:
CONFIG SET maxmemory-policy allkeys-lruLRU Algorithm
LRU (Least Recently Used) removes the least recently accessed items when the cache is full.
Java implementation of a simple LRU cache
public class LRUCache<k, v> {
// capacity
private int capacity;
// current node count
private int count;
// cache nodes
private Map<k, Node<k, v>> nodeMap;
private Node<k, v> head;
private Node<k, v> tail;
// ... (methods put, get, addNode, removeNode, etc.)
}The code demonstrates a basic LRU cache with put, get, and internal node management.
Approximate LRU in Redis
Redis uses an approximate LRU algorithm based on random sampling (default 5 keys). The maxmemory-samples parameter controls the sample size; larger values make eviction closer to true LRU.
Redis 3.0 optimizations
Redis 3.0 introduces a candidate pool of 16 keys sorted by last access time, improving eviction accuracy.
LFU Algorithm
LFU (Least Frequently Used) was added in Redis 4.0. It evicts keys that are accessed least often. Two policies exist: volatile-lfu (only expiring keys) and allkeys-lfu (all keys). LFU is only available on Redis 4.0+.
Question
The article invites readers to discuss why Redis uses an approximate LRU instead of a precise LRU.
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.
