Eviction policies help Redis decide which data to remove when it runs out of memory. This keeps Redis fast and prevents crashes.
0
0
Eviction policies overview in Redis
Introduction
When your Redis server has limited memory and you want to keep it running smoothly.
When you want to control which data stays in Redis and which data gets removed automatically.
When you use Redis as a cache and want old or less important data to be removed first.
When you want to avoid errors caused by Redis running out of memory.
When you want to balance memory use and data freshness in your application.
Syntax
Redis
CONFIG SET maxmemory-policy <policy_name>
You set the eviction policy using the CONFIG SET command.
The <policy_name> is the name of the eviction policy you want to use.
Examples
This policy means Redis will not remove any data and will return errors when memory is full.
Redis
CONFIG SET maxmemory-policy noeviction
This policy removes the least recently used keys from all keys when memory is full.
Redis
CONFIG SET maxmemory-policy allkeys-lru
This policy removes the least recently used keys only among keys with an expiration time set.
Redis
CONFIG SET maxmemory-policy volatile-lru
This policy removes random keys from all keys when memory is full.
Redis
CONFIG SET maxmemory-policy allkeys-random
Sample Program
This sets Redis to use 100 megabytes of memory and to remove the least recently used keys from all keys when memory is full.
Redis
CONFIG SET maxmemory 100mb
CONFIG SET maxmemory-policy allkeys-lru
OutputSuccess
Important Notes
Eviction policies only work if you set a memory limit with maxmemory.
Choosing the right policy depends on your use case, like caching or persistent storage.
Common policies include noeviction, allkeys-lru, volatile-lru, allkeys-random, and volatile-random.
Summary
Eviction policies control how Redis frees memory when it is full.
You set eviction policies with CONFIG SET maxmemory-policy.
Pick a policy that fits your app's needs to keep Redis fast and stable.