Databases 9 min read

How Redis Handles Full Cache: Memory Eviction Policies Explained

When Redis memory reaches its configured maxmemory limit, it triggers a set of eviction policies—such as noeviction, allkeys‑lru, volatile‑lfu, and others—to decide which keys to discard, and this article explains how to view, configure, and understand each strategy.

dbaplus Community
dbaplus Community
dbaplus Community
How Redis Handles Full Cache: Memory Eviction Policies Explained

Introduction

Redis stores cached data in memory, and as the amount of cached data grows, the limited memory can become full. When this happens, Redis uses a memory eviction mechanism, which is distinct from its key expiration policy, to decide which keys to remove.

Redis Maximum Memory

The eviction mechanism is only triggered when Redis's used memory reaches the value set by the maxmemory configuration option. This value can be inspected with the command:

127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "0"

A value of 0 means no limit (the default on 64‑bit systems). On 32‑bit systems the default limit is 3 GB.

Viewing the Current Eviction Policy

You can check which eviction policy Redis is currently using with:

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

The default noeviction policy means that when memory exceeds the limit, Redis will not evict any keys; instead, write operations will fail.

Eviction Policy Types

noeviction : No keys are evicted; writes error when memory is full.

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

allkeys-random : Evicts a random key among all keys.

volatile-lru : Evicts the least‑recently‑used key among keys with an expiration set.

volatile-random : Evicts a random key among keys with an expiration set.

volatile-ttl : Evicts the key with the nearest expiration time among keys with an expiration set.

Redis 4.0 added two more policies:

volatile-lfu : Evicts the least‑frequently‑used key among keys with an expiration set.

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

The allkeys-xxx policies consider every key, while the volatile-xxx policies only consider keys that have an expiration.

Changing the Eviction Policy

There are two ways to set a new eviction policy:

Run config set maxmemory-policy <policy>. This takes effect immediately but is lost after a Redis restart.

Edit the redis.conf file to set maxmemory-policy <policy>. The change persists across restarts but requires a restart for it to take effect.

Eviction Algorithms

Redis implements two main algorithms behind the policies: LRU (Least Recently Used) and LFU (Least Frequently Used).

LRU Algorithm

LRU selects the key that has not been accessed for the longest time. Redis uses an approximate LRU implementation: each object stores a timestamp of its last access, and when eviction is needed, Redis samples a small number of keys (default 5, configurable) and evicts the one with the oldest timestamp.

Implementation details:

typedef struct redisObject {</code><code>    unsigned type:4;</code><code>    unsigned encoding:4;</code><code>    unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or</code><code>                            * LFU data (least significant 8 bits frequency</code><code>                            * and most significant 16 bits access time). */</code><code>    int refcount;</code><code>    void *ptr;</code><code>} robj;

Because LRU is approximate, a key that was accessed recently may avoid eviction even if it is rarely used overall.

LFU Algorithm

LFU (Least Frequently Used) evicts keys that have been accessed the fewest times. Each object stores two fields: an 8‑bit counter ( logc) for frequency and a 16‑bit timestamp ( ldt) for the last decrement time. A lower logc value means the key is a better eviction candidate. logc: Stores access frequency (0‑255). Smaller values indicate lower usage. ldt: Stores the last time the counter was decremented.

LFU solves the problem where a key accessed once long ago would otherwise stay in cache under pure LRU.

Summary

Redis’s memory eviction policies are separate from its key expiration mechanisms. When used memory exceeds maxmemory, Redis consults the maxmemory-policy setting to choose one of eight possible strategies (including noeviction, allkeys‑lru, volatile‑lfu, etc.). Understanding and configuring the appropriate policy—and the underlying LRU or LFU algorithm—helps maintain Redis performance under memory pressure.

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.

performancedatabaseRedisLRULFUmemory eviction
dbaplus Community
Written by

dbaplus Community

Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.

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.