Cache Eviction Policies: LRU, LFU, FIFO, and Random
Compare LRU, LFU, FIFO, and random cache eviction with a shared request trace, then review Redis's version-specific policy behavior.
Eviction chooses which entry leaves when a cache reaches its memory limit. This guide compares common policies with one shared request trace, then explains how Redis approximates LRU and LFU.
Cache Eviction Policies: LRU, LFU, FIFO, and Random
A cache eviction policy chooses a resident entry to remove when the cache needs room. The choice matters when the active working set is larger than the memory budget. A policy can keep likely-to-be-used entries around, but it cannot make an undersized cache fit the workload.
This page focuses on replacement decisions. For read and write behavior, see Cache Patterns; for cache layers and freshness rules, see Caching Strategies.
Eviction and expiration are different
Eviction happens because the cache needs capacity. Expiration happens because an entry’s time-to-live (TTL) has elapsed. A system may support both: expired entries become invalid, and an eviction policy chooses among valid entries when memory fills.
A TTL does not by itself select a victim based on recent or frequent access. Redis’s volatile-ttl policy is an exception in the sense that it uses remaining TTL as a replacement signal for keys that have an expiry; the key still needs an expiration set.
Common policies
| Policy | What it tracks | Works well when | Watch for |
|---|---|---|---|
| LRU (least recently used) | Recency of access | Recently used entries tend to be used again soon | A scan of one-time keys can displace a useful working set. |
| LFU (least frequently used) | Access frequency | The same entries remain popular over time | A new hot entry may need time to build frequency; old popularity can linger without aging. |
| FIFO (first in, first out) | Insertion order | A simple, predictable baseline is enough | It ignores reads, so a frequently used entry can be removed just because it arrived first. |
| Random | Nothing beyond the candidate set | Access is close to uniform or a simple baseline is useful | It can remove a hot entry by chance. |
| TTL expiry | Expiration time | Data has a natural validity period | It controls freshness, not which valid entry should leave when capacity fills. |
LRU and LFU need metadata to rank entries. A common in-memory LRU implementation combines a key lookup table with a linked list so a hit can move an entry to the most-recent end and eviction can remove the least-recent end. Production caches may use approximations to reduce bookkeeping.
Compare policies on the same request stream
Assume a two-entry cache starts empty and receives A, B, B, A, C. New entries start with frequency one. The LFU column breaks equal-frequency ties by insertion order; the hybrid breaks frequency ties by least-recently-used order.
| Request | LRU (least → most recent) | LFU (insertion-order tie) | Frequency + recency (LRU tie) |
|---|---|---|---|
A |
[A] |
[A] |
[A] |
B |
[A, B] |
[A, B] |
[A, B] |
B |
[A, B] |
[A, B] (B count 2) |
[A, B] (B count 2) |
A |
[B, A] |
[B, A] (both counts 2) |
[B, A] (both counts 2) |
C |
[A, C] (evicts B) |
[B, C] (tie evicts older A) |
[A, C] (tie evicts less-recent B) |
This trace shows why a tie-break rule matters. It is a small example, not a benchmark. Actual hit rates depend on the request distribution, cache capacity, admission rules, and implementation.
Choose from the workload
Start with a request trace or production hit/miss data. Ask what a useful entry has in common: was it accessed recently, accessed often over a longer window, or given a short validity period by the application?
- Try LRU when recent use is a reasonable predictor of the next request.
- Try LFU when popularity is stable enough that repeated past use predicts future use. Check how the implementation ages frequency data.
- Try FIFO or random as simple baselines, or when the access distribution makes recency tracking less useful.
- Use TTL to express when data becomes stale. Select the capacity policy separately.
Then replay a representative trace or compare the policies under the same workload and cache size. Track hit rate, misses, evictions, memory use, and request latency. A higher hit rate is useful only if it also improves the outcome the cache was added to improve.
Redis behavior
Last verified: 2026-11-08. The Redis key eviction documentation describes the policies and settings below. Redis versions and hosted offerings can differ; check the documentation for the instance you operate before relying on a policy name or default.
Redis uses maxmemory-policy to choose a policy when the configured memory limit is reached. The allkeys-* policies can consider any key; volatile-* policies consider only keys with an expiry. If no keys have an expiry, a volatile-* policy behaves like noeviction: new commands that add data can fail instead of freeing memory.
maxmemory 100mb
maxmemory-policy allkeys-lru
Common choices include allkeys-lru, allkeys-lfu, allkeys-random, and their volatile-* counterparts. Redis also documents volatile-ttl; Redis Open Source 8.6 and later documents LRM (least recently modified) policies. Availability varies by Redis version and hosted service.
Redis’s LRU and LFU policies are approximations. The LRU policy samples candidate keys rather than maintaining a complete exact access order. LFU keeps an approximate frequency count that can decay over time. These are implementation details; do not assume every cache product uses the same algorithm just because it uses the same policy name.
Use INFO stats to inspect keyspace_hits, keyspace_misses, and evicted_keys; use INFO memory to inspect memory use. Compare those readings with the workload and the configured policy before changing settings. The Redis documentation explains the metrics and available options in its eviction reference.
Common failure cases
| Symptom | Likely explanation | What to check |
|---|---|---|
| Useful entries disappear during a scan | One-time keys are competing with frequently reused keys. | Compare a scan-heavy trace with the current policy; consider a scan-resistant admission or replacement strategy. |
A volatile-* Redis policy stops making room |
Remaining keys may not have expirations. | Check key expiry and whether allkeys-* or noeviction better matches the data contract. |
| LFU retains an old hot set | Popularity changed faster than the implementation’s aging behavior. | Review its frequency decay settings and replay a recent request trace. |
| Evictions rise while hit rate falls | The working set may exceed the budget, or the replacement signal may not fit the access pattern. | Check memory sizing, trace shape, and request latency before changing policy. |
Do not choose an eviction policy to solve a refresh stampede or keep two cache layers consistent. Those are separate problems; see Cache Stampede Prevention and the cache-layer guidance in Caching Strategies.
Interview Questions
LRU ranks entries by how recently they were accessed. LFU ranks them by how often they were accessed within the implementation's frequency model. LRU adapts quickly when the hot set changes; LFU can favor steady popularity but may need frequency aging.
A shared trace holds the workload constant. It makes hits, misses, evictions, and tie-break effects visible, but it does not predict hit rate for a different workload.
A TTL determines when an entry expires. A capacity policy chooses a valid entry to remove when space is needed. Some systems can use remaining TTL as a replacement signal, but the two decisions are distinct.
No. Redis Open Source documents an approximation that samples keys. The policy names and details can vary by Redis release or hosted offering, so consult the matching documentation.
Further Reading
- Redis key eviction documentation — policy behavior, configuration, and runtime metrics.
- Cache Patterns — application read and write patterns.
- Cache Stampede Prevention — coordinate concurrent cache refreshes.
- Caching Strategies — select layers and freshness rules.
- CDN Deep Dive — edge cache request paths and purge behavior.
Category
Related Posts
Bloom Filters: Space-Efficient Probabilistic Data Structure
How Bloom filters provide memory-efficient membership testing with configurable false positive rates for caches, databases, and distributed systems.
Cache Patterns: Choosing Read and Write Strategies
Compare cache-aside, read-through, write-through, write-around, and write-behind patterns, their freshness trade-offs, and update races to plan for.
Distributed Caching: Scaling Cache Across Multiple Nodes
Learn how distributed cache clusters route keys, replicate data, recover from node failures, and manage capacity and eviction trade-offs.