A cache has a fixed memory budget. Eventually it fills up. When a new item needs to be stored and there is no room, the cache must evict (remove) an existing item to make space. The eviction policy determines which item gets removed. Choosing the wrong policy for your workload can silently destroy your cache's effectiveness.
1. TTL (Time-To-Live)
Every item gets an expiry timer when it enters the cache. After the configured duration (e.g., 5 minutes), the item is automatically removed, regardless of how popular it is. TTL is not strictly an eviction policy — it is an expiry mechanism — but it works alongside eviction policies to prevent stale data from living forever. In Redis, you set this with the EX option: SET user:123 "{...}" EX 300 (expires in 300 seconds).
2. LRU (Least Recently Used)
The industry standard for general-purpose workloads. The cache tracks when each item was last accessed. When the cache is full, it evicts the item that has gone the longest without being accessed. The assumption: if you haven't needed something recently, you probably won't need it soon.
LRU works well for workloads with temporal locality — where recently accessed data is likely to be accessed again soon (e.g., a user browsing their own profile repeatedly).
3. LFU (Least Frequently Used)
The cache counts the total number of times each item has been accessed. When full, it evicts the item with the lowest access count. This protects items that are accessed often, even if they haven't been accessed in the last few minutes.
LFU works well for workloads with frequency locality — where some items are permanently popular (e.g., a celebrity's profile page that gets millions of views per day). The risk: a new item that will become very popular starts with a count of 0 and may be evicted before it has a chance to prove itself.
4. ARC (Adaptive Replacement Cache)
ARC dynamically balances between LRU and LFU based on observed workload patterns. It maintains two lists internally: one for recently used items and one for frequently used items, and it adjusts the balance between them in real time. ARC is more complex but often outperforms either LRU or LFU alone without requiring manual tuning. It is used internally by ZFS and some database buffer pools.
Choosing the Right Policy
| Workload | Recommended Policy |
|---|---|
| General web application reads | LRU |
| Mix of trending and evergreen content | LFU or ARC |
| Session data, short-lived tokens | TTL |
| Write-heavy with coalescing | Write-Back + LRU |
In Redis, you configure the eviction policy via maxmemory-policy in redis.conf. Common values: allkeys-lru, allkeys-lfu, volatile-lru (LRU only on keys with TTL set).