Databases 3 min read

Redis Eviction Policies Explained

This article introduces the various Redis eviction strategies—including volatile-ttl, volatile-random, volatile-lru, volatile-lfu, allkeys-random, allkeys-lru, and allkeys-lfu—explains their behavior, shows where they are configured in redis.conf and the initServer source code, and notes the default noeviction policy when memory exceeds maxmemory.

Wukong Talks Architecture
Wukong Talks Architecture
Wukong Talks Architecture
Redis Eviction Policies Explained

Redis provides several eviction policies to decide which keys to remove when the memory limit (maxmemory) is reached. The policies are configured in the redis.conf file and can be examined in the source code.

Volatile policies (apply only to keys with an expiration time):

volatile-ttl : Removes keys that will expire soonest.

volatile-random : Randomly removes keys with an expiration time.

volatile-lru : Uses the Least Recently Used algorithm to evict the least recently accessed expiring keys.

volatile-lfu : Uses the Least Frequently Used algorithm, first discarding keys with few accesses and then, if needed, the oldest among those with equal access counts.

All‑keys policies (apply to all keys regardless of expiration):

allkeys-random : Randomly removes any key.

allkeys-lru : Evicts the least recently used key among all keys.

allkeys-lfu : Evicts the least frequently used key among all keys.

If none of these policies are selected, Redis defaults to noeviction , meaning that when memory exceeds maxmemory write operations will fail with an error.

The default policy is set in the server initialization code. In server.c , the initServer function contains the line:

void initServer(void) {
    ...
    server.maxmemory_policy = MAXMEMORY_NO_EVICTION;
    ...
}

This line establishes the noeviction behavior unless the configuration is changed.

CacheMemory ManagementRedisdatabasesEviction Policy
Wukong Talks Architecture
Written by

Wukong Talks Architecture

Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.

0 followers
Reader feedback

How this landed with the community

login 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.