Databases 18 min read

Redis Memory Management: Eviction and Expiration Strategies Explained

This article provides a comprehensive overview of Redis memory management, detailing how maxmemory limits trigger various eviction policies, explaining the internal freeMemoryIfNeeded algorithm, and describing expiration mechanisms—including active and lazy deletion—and offering guidance on selecting the appropriate eviction strategy for different workloads.

政采云技术
政采云技术
政采云技术
Redis Memory Management: Eviction and Expiration Strategies Explained

Eviction Strategy Principles

Redis uses the maxmemory setting to limit memory usage; when the limit is reached, it applies an eviction policy defined by maxmemory-policy to free space for new data.

maxmemory Configuration

# client command to view and set maxmemory
127.0.0.1:6379> config get maxmemory
"maxmemory"
"0"
127.0.0.1:6379> config set maxmemory 100mb
OK

If maxmemory=0 , no limit is enforced; on 32‑bit Windows the default usable space is about 3 GB and the policy defaults to noeviction , causing write errors when memory is exhausted.

Eviction Policy Types

Redis defines eight eviction policies (volatile‑lru, volatile‑ttl, volatile‑random, allkeys‑lru, allkeys‑random, noeviction, and LFU variants introduced in Redis 4.0).

# command‑line configuration of eviction policy
CONFIG GET maxmemory-policy
"noeviction"
CONFIG SET maxmemory-policy volatile-lru
OK

freeMemoryIfNeeded Logic (excerpt)

int freeMemoryIfNeeded(void) {
  size_t mem_used, mem_tofree, mem_freed;
  // Compute used memory, subtract slave output buffers and AOF buffers
  // If memory exceeds maxmemory, select keys according to the configured policy
  // and delete them, updating statistics and notifying replicas as needed.
}

Expiration Strategy Principles

Keys with an expiration are stored in the internal expires dictionary; Redis checks expiration on access and also via periodic active expiration and lazy deletion.

# expiration commands
expire key seconds
pexpire key milliseconds
expireat key timestamp
pexpireat key millisecond_timestamp

Active expiration runs at a configurable frequency (default 10 times per second) scanning a random subset of keys, while lazy expiration checks a key’s TTL during each read/write operation.

Choosing an Eviction Policy

If hot and cold data coexist, prefer allkeys‑lfu with lfu-decay-time to avoid cache pollution.

For uniformly accessed data, allkeys‑random provides simple random eviction.

To keep certain data longer, use volatile‑lfu (Redis 4.0+) or volatile‑lru (Redis 3.0‑).

When ordering by expiration time is required, choose volatile‑ttl .

Combining periodic active expiration with lazy deletion offers a balanced approach that efficiently reclaims memory while preserving data consistency.

cacheDatabaseRedisLRULFUexpirationMemory Eviction
政采云技术
Written by

政采云技术

ZCY Technology Team (Zero), based in Hangzhou, is a growth-oriented team passionate about technology and craftsmanship. With around 500 members, we are building comprehensive engineering, project management, and talent development systems. We are committed to innovation and creating a cloud service ecosystem for government and enterprise procurement. We look forward to your joining us.

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.