Quick revision
The short answer
Expiry is a time limit we set on a key with EXPIRE, PEXPIRE or SET ... EX; Redis removes expired keys lazily when someone accesses them and actively with a background cycle that samples keys that have a TTL. Eviction is different: it happens only when memory reaches maxmemory, and Redis then removes keys chosen by maxmemory-policy whether or not they have expired. The policies are noeviction (the default, writes fail), allkeys-lru, allkeys-lfu, volatile-lru, volatile-lfu, volatile-ttl, allkeys-random and volatile-random, and a pure cache should use allkeys-lru or allkeys-lfu.
What to remember
In this answer, we saw that expiry removes keys whose TTL has passed, lazily on access and actively through a sampling background cycle, while eviction removes keys only when memory reaches maxmemory, chosen by maxmemory-policy regardless of their TTL. We went through the policies, from noeviction, the default that makes writes fail, to the allkeys and volatile families, saw that volatile policies behave like noeviction when no key has a TTL, and that LRU and LFU are approximated by sampling. For a pure cache, allkeys-lru or allkeys-lfu is the right choice.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Expiry is a time limit we set on a key with EXPIRE, PEXPIRE or SET ... EX; Redis removes expired keys lazily when someone accesses them and actively with a background cycle that samples keys that have a TTL. Eviction is different: it happens only when memory reaches maxmemory, and Redis then removes keys chosen by maxmemory-policy whether or not they have expired. The policies are noeviction (the default, writes fail), allkeys-lru, allkeys-lfu, volatile-lru, volatile-lfu, volatile-ttl, allkeys-random and volatile-random, and a pure cache should use allkeys-lru or allkeys-lfu.
Why this question is asked
Expiry and eviction both make keys disappear, so they are easy to confuse, and the confusion causes real outages: a cache that rejects writes when full, or keys that vanish earlier than their TTL said. Interviewers ask this to see whether we know the two mechanisms, how Redis implements them, and which policy suits which workload. In this answer, we will recap TTLs, look at how expiry actually removes keys, then at eviction and its policies, see how LRU and LFU are approximated, and finish with the right choice for a cache.
1A Quick Recap of TTLs
A TTL, time to live, is a countdown attached to a key. When it reaches zero, the key counts as expired and Redis treats it as missing. We can set it when writing the value, or add it to an existing key.
Notice the last line. Overwriting a key with a plain SET removes its TTL, so the value now lives forever unless we pass EX again or use KEEPTTL. That is a common way caches slowly fill up with keys that never expire.
2How Expiry Actually Removes Keys
Redis does not run a timer per key; with millions of keys that would be far too expensive. Instead it combines two methods. Lazy expiry happens on access: whenever a command touches a key, Redis first checks its expiry time, and if it has passed, deletes the key and behaves as if it never existed. So an expired key is never returned to a client.
But a key that nobody reads again would stay in memory forever with lazy expiry alone. So Redis also runs an active expiry cycle several times per second. It samples a small number of keys from the set of keys that have a TTL, deletes those that have expired, and if a large share of the sample was expired, it repeats straight away, because that suggests many more are waiting. The cycle limits its own CPU time so that it never blocks clients for long.
The result is that expired keys disappear promptly on access and gradually in the background. Memory used by expired but untouched keys is reclaimed over seconds, not instantly, which is why INFO stats shows expired_keys growing steadily rather than in exact time with each TTL.
- Client reads the keylazy path
- Has its expiry time passed?YesDelete it, reply as missingNoReply with the value
- Background cycle samples keys with a TTLactive path, several times a second
- Were many of the sample expired?YesDelete them and sample againNoDelete them and wait for the next cycle
4 steps in order. Play them, or step through them one at a time.
3Eviction Is a Different Mechanism
Eviction has nothing to do with time. It starts when Redis's memory use reaches the maxmemory limit we configured. Before executing a command that could add data, Redis checks memory, and if it is over the limit it removes keys until it is back under, choosing them according to maxmemory-policy. Those keys may have no TTL at all, or a TTL hours away; eviction removes them anyway because memory is full.
So the two answer different questions. Expiry answers: how long is this value valid? Eviction answers: memory is full, what do we give up? A healthy cache usually has both: TTLs that keep data fresh and an eviction policy that handles memory pressure gracefully.
4The maxmemory-policy Options
The policies combine two choices: which keys are candidates, and how to pick among them. The allkeys-* policies consider every key; the volatile-* policies consider only keys that have a TTL. LRU picks the least recently used, LFU the least frequently used, ttl the key closest to expiring, and random any key.
| Policy | Candidates | Removes |
|---|---|---|
| noeviction (default) | none | nothing; writes fail with OOM |
| allkeys-lru | every key | least recently used |
| allkeys-lfu | every key | least frequently used |
| allkeys-random | every key | a random key |
| volatile-lru | keys with a TTL | least recently used |
| volatile-lfu | keys with a TTL | least frequently used |
| volatile-ttl | keys with a TTL | the one closest to expiring |
| volatile-random | keys with a TTL | a random key |
noeviction, the default, removes nothing: writes that would need more memory fail with an OOM error, while reads keep working. That is right when Redis holds data we must not lose silently, such as a queue, and wrong for a cache.
But there is a catch with volatile-* policies. If none of the keys has a TTL, there are no candidates, and the instance behaves exactly like noeviction: writes start failing. That surprises teams who chose volatile-lru believing it was a safe cache setting.
5LRU and LFU Are Approximations
A true LRU would need a linked list of all keys in access order, updated on every read, which costs memory and time. Redis instead stores a small timestamp of the last access in each key's header. When it needs to evict, it samples maxmemory-samples keys, 5 by default, and evicts the best candidate among them, keeping a small pool of good candidates between rounds. Raising the sample count to 10 brings the result closer to true LRU at a little more CPU.
LFU works the same way but stores a small logarithmic counter of how often a key is used, which also decays over time so that keys popular last week do not stay forever. lfu-log-factor and lfu-decay-time tune how fast the counter grows and fades.
For a pure cache, allkeys-lru is the safe default: every key is a candidate and recently used data stays. allkeys-lfu is often better when a stable set of popular keys sits alongside a stream of one-off keys.
6Follow-up questions
When would LFU beat LRU?
When the workload has a steady set of popular keys plus bursts of keys used once. A nightly job that reads a million product pages once each makes every one of them recently used, so LRU evicts the truly popular keys to make room. LFU sees that the job's keys were used only once and keeps the popular ones, so the hit rate survives the scan.
Does an eviction count as an expiry?
No. INFO stats counts them separately as evicted_keys and expired_keys. A rising evicted_keys on a cache means it is too small for its working set or its TTLs are too long; a rising evicted_keys on an instance holding important data means data is being lost and the policy is wrong for it.
Can expired keys still use memory?
Yes, briefly. A key that expired but has not been touched or sampled yet still occupies memory until the active cycle finds it. With millions of keys expiring at once, this can take a while, and eviction may kick in first if memory is tight.
7Conclusion
In this answer, we saw that expiry removes keys whose TTL has passed, lazily on access and actively through a sampling background cycle, while eviction removes keys only when memory reaches maxmemory, chosen by maxmemory-policy regardless of their TTL. We went through the policies, from noeviction, the default that makes writes fail, to the allkeys and volatile families, saw that volatile policies behave like noeviction when no key has a TTL, and that LRU and LFU are approximated by sampling. For a pure cache, allkeys-lru or allkeys-lfu is the right choice.
Open with
Expiry and eviction are two separate mechanisms. Expiry is time-based: I set a TTL with EXPIRE, PEXPIRE or SET with EX, and once it passes the key is treated as gone.
Then, if they want more
Redis removes expired keys in two ways: lazily, when a command touches the key and finds it expired, and actively, with a background cycle that runs several times a second, samples keys that have a TTL, deletes the expired ones, and repeats if many were expired.
Eviction is memory-based: it only happens when used memory reaches maxmemory, and then Redis removes keys according to maxmemory-policy, whether or not they have a TTL or have expired.
The policies are noeviction, which is the default and makes writes fail with an OOM error; allkeys-lru, allkeys-lfu and allkeys-random, which consider every key; and volatile-lru, volatile-lfu, volatile-ttl and volatile-random, which consider only keys with a TTL, so if no key has a TTL they behave like noeviction.
For a pure cache I would use allkeys-lru, or allkeys-lfu when a stable set of hot keys competes with one-off scans. I would also mention that LRU and LFU are approximated by sampling a few keys, controlled by maxmemory-samples, rather than tracking exact order.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does key expiry work in Redis, and what is the difference between expiry and...": https://scrivio-interview-library.netlify.app/databases/redis/a2307e83c7/