Databases 13 min read

Redis Memory Limits & Eviction: LRU, LFU, and Config Guide

Learn how to set Redis's maximum memory usage, understand its various eviction policies—including noeviction, allkeys‑lru, volatile‑lru, allkeys‑random, volatile‑random, volatile‑ttl, and the newer LFU strategies—plus see Java code examples and insights into approximate LRU implementation.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Redis Memory Limits & Eviction: LRU, LFU, and Config Guide

Redis Memory Size

Redis is an in‑memory key‑value database. Because system memory is limited, you can configure the maximum memory Redis is allowed to use.

Configure via redis.conf

Add the following line to the redis.conf file in the Redis installation directory:

maxmemory 100mb
The configuration file used at startup can be specified with a command‑line argument, so it may not always be the redis.conf located under the installation directory.

Configure at runtime

Redis also supports changing the memory limit while it is running:

127.0.0.1:6379> config set maxmemory 100mb
127.0.0.1:6379> config get maxmemory
If the maximum memory is not set or set to 0 , Redis has no limit on 64‑bit systems and a 3 GB limit on 32‑bit systems.

Redis Memory Eviction

When the configured memory limit is reached, Redis applies one of several eviction strategies:

noeviction (default): write requests are rejected with an error.

allkeys‑lru : evicts the least‑recently‑used key among all keys.

volatile‑lru : evicts the least‑recently‑used key among keys with an expiration.

allkeys‑random : evicts a random key among all keys.

volatile‑random : evicts a random key among keys with an expiration.

volatile‑ttl : evicts keys with an expiration that are closest to expiring.

When using volatile‑lru , volatile‑random , or volatile‑ttl , if no evictable key exists the behavior is the same as noeviction (an error is returned).

Get and set eviction policy

127.0.0.1:6379> config get maxmemory-policy

Set the policy in redis.conf: maxmemory-policy allkeys-lru Or change it at runtime:

127.0.0.1:6379> config set maxmemory-policy allkeys-lru

LRU Algorithm

LRU (Least Recently Used) is a cache replacement algorithm that evicts the data that has not been accessed for the longest time, assuming it is less likely to be needed soon.

When a cache is full, the least‑recently‑used items are removed to make space for new data.

Simple Java LRU implementation

public class LRUCache<K, V> {<br/>    private int capacity;<br/>    private int count;<br/>    private Map<K, Node> nodeMap;<br/>    private Node head;<br/>    private Node tail;<br/><br/>    public LRUCache(int capacity) {<br/>        if (capacity < 1) {<br/>            throw new IllegalArgumentException(String.valueOf(capacity));<br/>        }<br/>        this.capacity = capacity;<br/>        this.nodeMap = new HashMap<>();<br/>        Node headNode = new Node(null, null);<br/>        Node tailNode = new Node(null, null);<br/>        headNode.next = tailNode;<br/>        tailNode.pre = headNode;<br/>        this.head = headNode;<br/>        this.tail = tailNode;<br/>    }<br/><br/>    public void put(K key, V value) {<br/>        Node node = nodeMap.get(key);<br/>        if (node == null) {<br/>            if (count >= capacity) {<br/>                removeNode();<br/>            }<br/>            node = new Node(key, value);<br/>            addNode(node);<br/>        } else {<br/>            moveNodeToHead(node);<br/>        }<br/>    }<br/><br/>    public Node get(K key) {<br/>        Node node = nodeMap.get(key);<br/>        if (node != null) {<br/>            moveNodeToHead(node);<br/>        }<br/>        return node;<br/>    }<br/><br/>    private void removeNode() {<br/>        Node node = tail.pre;<br/>        removeFromList(node);<br/>        nodeMap.remove(node.key);<br/>        count--;<br/>    }<br/><br/>    private void removeFromList(Node node) {<br/>        Node pre = node.pre;<br/>        Node next = node.next;<br/>        pre.next = next;<br/>        next.pre = pre;<br/>        node.next = null;<br/>        node.pre = null;<br/>    }<br/><br/>    private void addNode(Node node) {<br/>        addToHead(node);<br/>        nodeMap.put(node.key, node);<br/>        count++;<br/>    }<br/><br/>    private void addToHead(Node node) {<br/>        Node next = head.next;<br/>        next.pre = node;<br/>        node.next = next;<br/>        node.pre = head;<br/>        head.next = node;<br/>    }<br/><br/>    public void moveNodeToHead(Node node) {<br/>        removeFromList(node);<br/>        addToHead(node);<br/>    }<br/><br/>    class Node<K, V> {<br/>        K key;<br/>        V value;<br/>        Node pre;<br/>        Node next;<br/><br/>        public Node(K key, V value) {<br/>            this.key = key;<br/>            this.value = value;<br/>        }<br/>    }<br/>}
This code implements a simple LRU cache with a doubly‑linked list and a hash map.

Approximate LRU in Redis

Redis uses an approximate LRU algorithm based on random sampling. By default it samples 5 keys and evicts the least‑recently‑used among them.

The number of samples can be changed with the maxmemory-samples configuration (e.g., maxmemory-samples 10 ).

Redis 3.0 introduced an optimization: a candidate pool of 16 keys is maintained, sorted by last access time. New sampled keys replace entries in the pool only if their last‑access time is older than the smallest entry, and eviction selects the key with the oldest access time from the pool.

LRU Algorithm Comparison

An experiment can compare strict LRU, approximate LRU with different sample sizes, and other policies by filling Redis to its memory limit and then adding additional data to trigger eviction.

LRU vs LFU comparison chart
LRU vs LFU comparison chart

LFU Algorithm

LFU (Least Frequently Used) was added in Redis 4.0. It evicts keys that have been accessed the fewest times, better reflecting a key’s hotness.

Two LFU policies are available:

volatile‑lfu – evicts among keys with an expiration.

allkeys‑lfu – evicts among all keys.

These policies require Redis 4.0 or newer; using them on older versions will result in an error.

Open Question

Why does Redis use an approximate LRU algorithm instead of a precise LRU implementation? Consider the trade‑offs and discuss.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaMemory ManagementredisLRULFUEviction Policies
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.