Cache Eviction Policies | System Design (original) (raw)
Last Updated : 4 May, 2026
Cache eviction is the process of removing data from a cache when it becomes full to make space for new or more relevant data. It ensures that limited cache memory is used efficiently while keeping frequently accessed data available.
- Removes less useful or outdated data when cache reaches capacity, maintaining efficient memory usage.
- Uses policies like LRU, LFU, and FIFO to decide which data to evict and improve performance.
**Example: In YouTube, if many videos are cached and memory is full, less useful or less recently accessed videos are removed so frequently watched videos can stay and load faster.
Cache Eviction Policies
Some of the most important and common cache eviction strategies are:
1. Least Recently Used(LRU)
LRU (Least Recently Used) is a cache eviction policy that removes the item that has not been accessed for the longest time when the cache is full. It assumes that recently used data is more likely to be used again, so it keeps track of access order and evicts the least recently used item.

LRU
**Example
Consider a cache with a maximum capacity of 3, initially containing items A, B, and C in that order.
- If a new item, D, is accessed, the cache becomes full, and the LRU policy would evict the least recently used item, which is A, because it was not accessed recently compared to B and C. The cache now holds items B, C, and D.
- If item B is accessed next, the order becomes C, D, B.
- If another item, E, is accessed, the cache is full again, and the LRU policy would evict C, resulting in the cache holding items D, B, and E. The order now is B, E, D.
LRU ensures that the most recently accessed items are retained in the cache, optimizing for scenarios where recent access patterns are indicative of future accesses.
**Advantages
LRU offers a simple and effective way to manage cache by keeping recently used data available.
- **Easy Implementation: LRU is a simple option for many caching applications due to its ease of understanding and implementation.
- **Efficient Use of Cache: When current accesses are a reliable indicator of future accesses, LRU works well. It ensures that recently accessed items remain in the cache, regardless of how frequently they were used earlier.
- **Adaptability: LRU is adaptable to various types of applications, including databases, web caching, and file systems.
**Disadvantages
LRU has some limitations when access patterns are unpredictable or resources are constrained.
- **Strict Ordering: LRU assumes that the order of access accurately reflects the future usefulness of an item. In certain cases, this assumption may not hold true, leading to suboptimal cache decisions.
- **Cold Start Issues: When a cache is initially populated, LRU might not perform optimally as it requires sufficient historical data to make informed eviction decisions.
- **Memory Overhead: Implementing LRU often requires additional memory to store timestamps or maintain access order, which can impact the overall memory consumption of the system.
**Use Cases
LRU is widely used in systems where recent data access patterns help improve performance and speed.
- **Web Caching: LRU is commonly employed to store frequently accessed web pages, images, or resources. This helps in reducing latency by keeping the most recently used content readily available, improving overall website performance.
- **Database Management: LRU is often used in database systems to cache query results or frequently accessed data pages. This accelerates query response times by keeping recently used data in memory, reducing the need to fetch data from slower disk storage.
- **File Systems: File systems can benefit from LRU when caching file metadata or directory information. Frequently accessed files and directories are kept in the cache, improving file access speed and reducing the load on the underlying storage.
2. Least Frequently Used(LFU)
The least frequently accessed entries are eliminated first under the LFU cache eviction policy. It is based on the idea that things that are used the least are less likely to be needed later. When the cache is full, LFU removes the item with the lowest access frequency after keeping track of the amount of times each item is accessed.

LFU
**Example
Consider a cache with items X, Y, and Z. If item Z has been accessed fewer times than items X and Y, the LFU policy will retain the items X and Y and potentially evict item Z when the cache reaches its capacity.
**Advantages
LFU works well for systems where frequently accessed data over time is more important than recent usage.
- **Adaptability to Varied Access Patterns: LFU is effective in scenarios where some items may be accessed infrequently but are still essential. It adapts well to varying access patterns.
- **Optimized for Long-Term Trends: LFU can be beneficial when the relevance of an item is better captured by its overall frequency of access over time rather than recent accesses.
- **Low Memory Overhead: Since LFU doesn't need to keep timestamps, it might have less memory overhead than some LRU implementations.
**Disadvantages
LFU may struggle with dynamic workloads and requires careful implementation of frequency tracking.
- **Sensitivity to Initial Access: LFU may not perform optimally during the initial stages when access frequencies are still being established. It relies on historical access patterns, and a new or less frequently accessed item might not be retained in the cache until its long-term frequency is established.
- **Difficulty in Handling Changing Access Patterns: LFU can struggle in scenarios where access patterns change frequently. Items that were once heavily accessed but are no longer relevant might continue to be retained in the cache.
- **Complexity of Frequency Counters: Implementing accurate frequency counting for items can add complexity to LFU implementations.
**Use Cases
LFU is suitable for systems where long-term access patterns are more important than recent activity.
- **Database Query Caching: In database management systems, LFU can be applied to cache query results or frequently accessed data.
- **Network Routing Tables: LFU is useful in caching routing information for networking applications. Items representing less frequently used routes are kept in the cache, allowing for efficient routing decisions based on historical usage.
- **Content Recommendations: In content recommendation systems, LFU can be employed to cache information about user preferences or content suggestions. It ensures that even less frequently accessed recommendations are considered over time.
3. First-In-First-Out(FIFO)
First-In-First-Out (FIFO) is a cache eviction policy that removes the oldest item from the cache when it becomes full. In this strategy, data is stored in the cache in the order it arrives, and the item that has been present in the cache for the longest time is the first to be evicted when the cache reaches its capacity.

FIFO
**Example
Imagine a cache with a capacity of three items:
- A is added to the cache.
- B is added to the cache.
- C is added to the cache.
At this point, the cache is full (capacity = 3)
If a new item, D, needs to be added, the FIFO policy would dictate that the oldest item, A, should be evicted. The cache would then look like:
- D is added to the cache (A is evicted).
- The order of items in the cache now is B, C, and D, reflecting the chronological order of their arrival.
**Advantages
FIFO is simple and predictable, making it useful where order-based eviction is sufficient.
- **Simple Implementation: FIFO is straightforward to implement, making it an easy choice for scenarios where simplicity is a priority.
- **Predictable Behavior: The eviction process in FIFO is predictable and follows a strict order based on the time of entry into the cache.
- **Memory Efficiency: Since FIFO eliminates the need for extra tracking of timestamps and access frequency, it has a comparatively minimal memory overhead when compared to some other eviction strategies.
**Disadvantages
FIFO does not consider usage patterns, which can lead to inefficient cache decisions.
- **Lack of Adaptability: FIFO may not adapt well to varying access patterns. It strictly adheres to the order of entry, which might not reflect the actual importance or relevance of items.
- **Inefficiency in Handling Variable Importance: FIFO might lead to inefficiencies when newer items are more relevant or frequently accessed than older ones.
- **Cold Start Issues: When a cache is initially populated or after a cache flush, FIFO may not perform optimally, as it tends to keep items in the cache based solely on their entry time, without considering their actual usage.
**Use Cases
FIFO is suitable where maintaining order is more important than optimizing access patterns.
- **Task Scheduling in Operating Systems: In task scheduling, FIFO can be employed to determine the order in which processes or tasks are executed.
- **Message Queues: FIFO guarantees that messages are handled in the order that they are received in message queuing systems. In applications that depend on message-based communication, this is essential for preserving the order of processes.
- **Cache for Streaming Applications: For some streaming applications where preserving the data's order is crucial, FIFO may be appropriate. For instance, FIFO guarantees that frames are displayed in the proper order in a video streaming cache.
4. Random Replacement
Random Replacement is a cache eviction policy where, when the cache is full and a new item needs to be stored, a randomly chosen existing item is evicted to make room. Unlike some deterministic policies like LRU (Least Recently Used) or FIFO (First-In-First-Out), which have specific criteria for selecting items to be evicted, Random Replacement simply selects an item at random.

Random Placement
**Example
Consider a cache with three slots and the following data:
- Item A
- Item B
- Item C
Now, if the cache is full and a new item, Item D, needs to be stored, Random Replacement might choose to evict Item B, resulting in:
- Item A
- Item D
- Item C
The selection of Item B for eviction is entirely random in this policy, making it a straightforward but less predictable strategy compared to others.
Advantages
Random replacement is simple and efficient, making it suitable for systems where low overhead is preferred.
- **Simplicity: Random replacement is a straightforward and easy-to-implement strategy. It does not require complex tracking or analysis of access patterns.
- **Avoids Biases: Since random replacement doesn't rely on historical usage patterns, it avoids potential biases that may arise in more deterministic policies.
- **Low Overhead: The algorithm involves minimal computational overhead, making it efficient in terms of processing requirements.
Disadvantages
Random replacement ignores usage patterns, which can reduce cache efficiency.
- **Suboptimal Performance: Random replacement may lead to suboptimal cache performance compared to more sophisticated policies. It doesn't consider the actual usage patterns or the likelihood of future accesses.
- **No Adaptability: It lacks adaptability to changing access patterns. Other eviction policies, like LRU or LFU, consider the historical behavior of items and adapt to evolving patterns, potentially providing better cache performance over time.
- **Possibility of Poor Hit Rates: The random nature of eviction may result in poor hit rates, where frequently accessed items are unintentionally evicted, leading to more cache misses.
Use Cases
Random replacement is useful in simple or resource-limited environments where efficiency matters more than optimization.
- **Non-Critical Caching Environments: In scenarios where the impact of cache misses is minimal or where caching is employed for non-critical purposes, such as temporary storage of non-essential data, random replacement can be sufficient.
- **Simulation and Testing: In testing situations and simulation environments where simplicity and convenience of use are more important than complex eviction policies, random replacement is helpful.
- **Resource-Constrained Systems: In resource-constrained environments, where computational resources are limited, the low overhead of random replacement may be advantageous.