Memory Limits and Eviction
Setting a memory limit
Section titled “Setting a memory limit”maxmemory caps Redis memory usage. When the limit is reached, Redis applies the eviction policy to decide which keys to remove. You can set it in redis.conf or at runtime with CONFIG SET.
maxmemory 512mbmaxmemory-policy allkeys-lruEviction policies
Section titled “Eviction policies”| Policy | Behavior |
|---|---|
noeviction | Return error on writes when full (default; safe for databases) |
allkeys-lru | Evict least recently used key from all keys |
volatile-lru | Evict LRU from keys with a TTL set |
allkeys-lfu | Evict least frequently used key from all keys |
volatile-lfu | Evict LFU from keys with a TTL |
volatile-ttl | Evict the key with the shortest remaining TTL |
allkeys-random | Evict a random key from all keys |
volatile-random | Evict a random key from keys with a TTL |
Checking and changing policy at runtime
Section titled “Checking and changing policy at runtime”127.0.0.1:6379> CONFIG GET maxmemory-policy1) "maxmemory-policy"2) "noeviction"127.0.0.1:6379> CONFIG SET maxmemory-policy allkeys-lruOK127.0.0.1:6379> CONFIG GET maxmemory1) "maxmemory"2) "0"127.0.0.1:6379> CONFIG SET maxmemory 256mbOKCONFIG GET maxmemory-policy
CONFIG SET maxmemory-policy allkeys-lru
CONFIG GET maxmemory
CONFIG SET maxmemory 256mb