Databases 10 min read

Understanding Redis Memory Eviction Policies and Configuration

This article explains how Redis uses the maxmemory setting to trigger memory eviction, details the various eviction policies (including LRU and LFU), shows how to query and modify these settings with config commands, and briefly mentions promotional offers for a ChatGPT community.

Top Architect
Top Architect
Top Architect
Understanding Redis Memory Eviction Policies and Configuration

Redis stores cached data in memory, and when the allocated memory is exhausted, an eviction mechanism decides which keys to remove.

The maxmemory configuration item defines the threshold at which eviction starts; you can view the current setting with config get maxmemory . A value of 0 (the default on 64‑bit systems) means no limit.

When memory exceeds maxmemory , Redis applies a memory‑eviction policy defined by maxmemory-policy . The default is noeviction , which rejects new writes once the limit is reached. Other policies include allkeys-lru , allkeys-random , volatile-lru , volatile-random , volatile-ttl , and, since Redis 4.0, volatile-lfu and allkeys-lfu .

You can query the current policy with:

127.0.0.1:6379> config get maxmemory-policy
1) "maxmemory-policy"
2) "noeviction"

Redis implements the LRU (Least Recently Used) algorithm using an approximate method: each key stores a last‑access timestamp, and eviction samples a small number of keys (default 5) to pick the least recently used one.

The LFU (Least Frequently Used) algorithm tracks usage frequency. In the object header, the lru field stores LFU data (logc for frequency and ldt for last decrement time). Example struct definition:

typedef struct redisObject {
unsigned type:4;
unsigned encoding:4;
unsigned lru:LRU_BITS; /* LRU time or LFU data */
int refcount;
void *ptr;
} robj;

To change the eviction policy you have two options: (1) run config set maxmemory-policy <policy> for an immediate but non‑persistent change, or (2) edit the Redis configuration file and restart the server for a permanent change.

In summary, Redis separates expiration (removing expired keys) from memory eviction (removing keys when memory is full). The maxmemory and maxmemory-policy settings control this behavior, with eight possible policies available in recent versions, the default being noeviction .

The remainder of the page contains promotional material for a ChatGPT community, offering free accounts, courses, and other incentives, which is unrelated to the technical discussion.

RedisLRUdatabasesLFUMemory Evictionmaxmemory
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.