Course contentsShow
← Databases & SQL
Lesson 1298 of 1,95312. Key-Value and Caching EnginesFree lesson

Cache Eviction Policies

Evaluate LRU vs LFU strategies to optimize memory usage under high pressure.

In this lesson, you will learn how to choose between Least Recently Used (LRU) and Least Frequently Used (LFU) strategies to manage memory when your cache reaches its capacity limit.

When the Memory Tank Runs Dry

When working with in-memory systems like Redis, you often set a maximum memory limit to prevent your application from crashing the host machine. Once you hit that threshold, the database must delete old data to make room for new incoming requests. This process is called eviction.

Choosing the right policy depends entirely on your workload’s "access pattern."

LRU: The "Recency" Approach

Least Recently Used (LRU) assumes that data accessed recently is likely to be accessed again soon. It tracks the "age" of your keys. When the cache is full, it discards the item that has gone the longest without being requested.

  • Best for: General-purpose caching, like session storage or web page fragments, where recent user activity is the best predictor of future needs.

LFU: The "Popularity" Approach

Least Frequently Used (LFU) ignores how long ago an item was accessed and instead focuses on the total number of requests. It keeps a counter for each key. When space is needed, it evicts the items with the lowest hit counts.

  • Best for: Scenarios where you have "heavy hitters" that might have been accessed a long time ago but are still critical to have in memory because they are requested constantly.

Comparing the Logic

Imagine a leaderboard system. If you use LRU, a player who was active ten minutes ago remains in cache, while a player who was active twenty minutes ago is evicted—even if the older player is a "superstar" requested by thousands of users every second. LFU would protect that superstar because their frequency counter is massive.

-- Conceptual logic for policy selection
-- LRU: Focus on 'last_accessed_timestamp' (Evict oldest)
-- LFU: Focus on 'access_frequency_counter' (Evict lowest count)

If your traffic is bursty, LRU is usually the safest bet. If you have a long-tail distribution where specific, rare items are consistently popular over long periods, LFU will provide a higher hit rate.

Key Takeaway: Use LRU when temporal recency predicts value, and use LFU when absolute request volume determines importance.