How Redis Evicts Keys
Follow Redis from its maxmemory setting to the sample of keys it inspects when the cache fills up.
Redis has a lot going for it as an in-memory key-value store. It’s fast, it understands several data types, you can talk to it over the network, and you can turn on persistence if you need it. That’s made it a popular choice for all sorts of projects.
A cache buys fast reads and writes with a fairly tight space budget. Redis keeps its data in RAM, so it can run circles around a disk-based database, right up until it runs out of memory. A busy cache eventually has to make room. In general that might mean discarding old data or moving it to secondary storage. Redis evicts keys.
Redis is probably best known for LRU (least recently used) eviction. Redis 4.0 also added LFU, short for least frequently used, which I’ll get to at the end. First I want to look at two details that matter when you use Redis as a cache. How much memory can it use, and what does it throw away when it gets there?
Setting a memory limit
The maxmemory setting gives Redis a memory budget to work with.
You can set it on a running server with CONFIG SET maxmemory 100mb. To configure the same 100 MB limit at startup, put maxmemory 100mb in redis.conf.
A maxmemory of 0 means unlimited. That’s the default on 64-bit systems. On 32-bit systems, Redis defaults to a limit of 3 GB.
Once Redis reaches the limit, its eviction policy kicks in. It might return an error for an operation that needs more memory, or it might evict older data to get back under budget.
Picking an eviction policy
When memory runs short, maxmemory-policy determines what Redis does next.
Here are the choices for this part of the discussion.
- noeviction returns an error for commands that would consume more memory. Most of those are writes.
- allkeys-lru looks for a least recently used key to evict and makes room for new data.
- volatile-lru does the same search among keys that have an expiry set.
- allkeys-random evicts a random key from the entire keyspace. A curious choice at first glance.
- volatile-random picks a random key from those with an expiry.
- volatile-ttl evicts the key with the shortest remaining time to live, or TTL.
If none of the keys have an expiry, the volatile policies have nothing to evict and return an error just as noeviction would.
You can change the policy while Redis is running and use INFO to keep an eye on cache hits.
Here are the rules of thumb I use when choosing a policy.
- If some keys are much hotter than others, or you’re unsure where to start, try allkeys-lru. It usually works well.
- If the application scans the same keys over and over in a loop, or every key is about equally likely to be read, allkeys-random makes sense.
- If you can assign TTLs that reflect which data you’re willing to lose first, consider volatile-ttl.
Expiry metadata costs memory too. If memory is tight and the data doesn’t actually need to expire, I’d use allkeys-lru and leave the expiries out.
What happens during eviction?
It helps to look at the sequence of events.
- A client issues a command that increases memory usage.
- Redis checks whether it’s over the limit and applies the configured policy if it is.
- The client issues another command and the cycle repeats.
The timing matters here. Redis evicts keys after memory usage goes over the limit. A single operation that adds a lot of data, say one enormous value, can take Redis well over its budget before eviction catches up.
Approximating LRU
Redis takes a shortcut with LRU. Finding the single least recently used key in the entire database would be expensive. Redis samples a handful of candidates and evicts the one whose last access was furthest in the past. That won’t always be the key exact LRU would pick.
Redis 3.0 improved the approximation by keeping a pool of eviction candidates across samples. Its choices come closer to what exact LRU would do.
One nice thing about this design is that you can tune the number of keys sampled. For instance, maxmemory-samples 5 tells Redis to sample five keys at a time.
An exact implementation of LRU would need more memory. How much does the shortcut actually cost us? Here’s a comparison.

The test fills a Redis server with keys, then accesses every key in insertion order. Under exact LRU, the first key inserted would now be first in line for eviction. Next, the test adds half as many new keys as it started with. Exact LRU would evict the oldest half of the original set.
Each dot is a key. Insertion order runs from the upper left to the lower right.
- Light gray dots are keys that got evicted.
- Dark gray dots are keys from the original batch that survived.
- Green dots are the newly inserted keys.
At a sample size of five, Redis 3.0 already gets closer to exact LRU than the earlier implementation, although you can still see the difference. Take ten samples and the two pictures look remarkably similar.
LRU is only a model of what you might read next. If accesses follow something like a power law, so that most requests land on a fairly small set of keys, it’s a pretty good model.
LFU mode
Redis 4.0 added LFU eviction. It estimates how often each key is accessed, which gives frequently used data a better chance of surviving.
Here’s a case where I’d trust LFU over LRU. Say I read a key I almost never touch. There’s another key I use all the time, but my last read of it was a little earlier. LRU could throw out the busy key first. LFU remembers enough about access frequency to avoid that trap.
Two policies enable LFU mode.
- volatile-lfu applies LFU eviction to keys with an expiry.
- allkeys-lfu applies it to all keys.
Redis keeps a probabilistic counter, a Morris counter, that estimates access frequency in just a few bits. The count decays over time, so a key’s popularity reflects a recent window of activity.
LFU gives you two knobs to turn directly. One sets the decay interval. The other controls how quickly the counter grows.
The defaults are lfu-decay-time 1 and lfu-log-factor 10. The decay interval is in minutes. The counter tops out at 255. The log factor determines how quickly it gets there. Here’s roughly what ends up in the counter for a given number of hits.
+--------+------------+------------+------------+------------+------------+| factor | 100 hits | 1000 hits | 100K hits | 1M hits | 10M hits |+--------+------------+------------+------------+------------+------------+| 0 | 104 | 255 | 255 | 255 | 255 |+--------+------------+------------+------------+------------+------------+| 1 | 18 | 49 | 255 | 255 | 255 |+--------+------------+------------+------------+------------+------------+| 10 | 10 | 18 | 142 | 255 | 255 |+--------+------------+------------+------------+------------+------------+| 100 | 8 | 11 | 49 | 143 | 255 |+--------+------------+------------+------------+------------+------------+I’ll spare you the formula for factor. You can get a pretty good feel for it from those numbers.