Databases 6 min read

Understanding Redis Memory Eviction Strategies

Redis stores data in memory, so when usage reaches the maxmemory limit it triggers one of six eviction policies—noeviction, allkeys‑lru, volatile‑lru, allkeys‑random, volatile‑random, and volatile‑ttl—each suited to different scenarios, and can be set via redis.conf or CONFIG SET, as illustrated with a Java Jedis example.

java1234
java1234
java1234
Understanding Redis Memory Eviction Strategies

Redis eviction policies

Redis stores all data in memory. When the memory consumption reaches the configured maxmemory limit, Redis invokes an eviction policy to free space. Six built‑in policies are available.

noeviction

Description : After the limit is hit, new write commands are rejected while read commands continue to work.

Typical scenario : Applications that must preserve existing data and can tolerate write failures.

allkeys-lru

Description : Uses the Least Recently Used (LRU) algorithm on the entire keyspace, evicting the least recently accessed keys until enough memory is reclaimed.

Typical scenario : Workloads that constantly add new entries and need to keep the most frequently accessed items.

volatile-lru

Description : Applies LRU only to keys that have an explicit expiration time (TTL).

Typical scenario : Caches where only time‑limited entries should be retained.

allkeys-random

Description : Randomly selects a key from the whole keyspace for eviction.

Typical scenario : Environments with a uniform data distribution where hot keys are hard to predict.

volatile-random

Description : Randomly evicts a key among those that have a TTL.

Typical scenario : Similar to allkeys-random but limited to expiring keys.

volatile-ttl

Description : Prioritises eviction of keys with the shortest remaining TTL among expiring keys.

Typical scenario : Quickly discarding data that is about to expire to free space for newer entries.

Configuring an eviction policy

The policy can be set in redis.conf or changed at runtime with the CONFIG SET command.

maxmemory 100mb
maxmemory-policy allkeys-lru

The same settings can be applied dynamically:

127.0.0.1:6379> CONFIG SET maxmemory 100000000
127.0.0.1:6379> CONFIG SET maxmemory-policy allkeys-lru

Java example using Jedis

The program below creates a Jedis client, sets a 10 MB memory limit with the allkeys-lru policy, inserts keys until the limit is reached, and then demonstrates eviction.

import redis.clients.jedis.Jedis;

public class RedisMemoryEvictionExample {
    public static void main(String[] args) {
        // Connect to Redis
        Jedis jedis = new Jedis("localhost", 6379);

        // Set memory limit and eviction policy
        jedis.configSet("maxmemory", "10mb"); // 10 MB limit
        jedis.configSet("maxmemory-policy", "allkeys-lru"); // LRU across all keys

        // Insert many keys
        for (int i = 0; i < 1000; i++) {
            jedis.set("key" + i, "value" + i);
            System.out.println("Inserted: key" + i);
        }

        // Monitor memory usage and trigger eviction
        while (true) {
            long usedMemory = Long.parseLong(
                jedis.info("memory").split("
")[1].split(":")[1].trim()
            );
            System.out.println("Used Memory: " + usedMemory / 1024.0 / 1024.0 + " MB");

            if (usedMemory >= 10 * 1024 * 1024) { // exceeds 10 MB
                System.out.println("Memory reached limit, Redis will start evicting keys based on policy.");
                jedis.set("new_key", "new_value"); // triggers eviction
            }

            try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }
        }
    }
}

The loop prints the current memory usage every two seconds; once the usage surpasses the configured limit, inserting a new key forces Redis to evict existing keys according to the selected policy.

JavaRedisJedisLRUmemory evictionmaxmemory
java1234
Written by

java1234

Former senior programmer at a Fortune Global 500 company, dedicated to sharing Java expertise. Visit Feng's site: Java Knowledge Sharing, www.java1234.com

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.