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.

**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.

system_design_8

LRU

**Example

Consider a cache with a maximum capacity of 3, initially containing items A, B, and C in that order.

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.

**Disadvantages

LRU has some limitations when access patterns are unpredictable or resources are constrained.

**Use Cases

LRU is widely used in systems where recent data access patterns help improve performance and speed.

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.

system_design_9

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.

**Disadvantages

LFU may struggle with dynamic workloads and requires careful implementation of frequency tracking.

**Use Cases

LFU is suitable for systems where long-term access patterns are more important than recent activity.

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.

system_design_10

FIFO

**Example

Imagine a cache with a capacity of three items:

  1. A is added to the cache.
  2. B is added to the cache.
  3. 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:

**Advantages

FIFO is simple and predictable, making it useful where order-based eviction is sufficient.

**Disadvantages

FIFO does not consider usage patterns, which can lead to inefficient cache decisions.

**Use Cases

FIFO is suitable where maintaining order is more important than optimizing access patterns.

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.

system_design_11

Random Placement

**Example

Consider a cache with three slots and the following data:

  1. Item A
  2. Item B
  3. 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:

  1. Item A
  2. Item D
  3. 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.

Disadvantages

Random replacement ignores usage patterns, which can reduce cache efficiency.

Use Cases

Random replacement is useful in simple or resource-limited environments where efficiency matters more than optimization.

  1. **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.
  2. **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.
  3. **Resource-Constrained Systems: In resource-constrained environments, where computational resources are limited, the low overhead of random replacement may be advantageous.