Course contentsShow
← Software Engineering
Lesson 963 of 1,93330. Caching StrategiesFree lesson

Cache Size Limits and Eviction

Managing cache memory constraints through eviction policies like LRU, LFU, and FIFO when capacity is reached.

Cache Size Limits and Eviction

What you'll learn: How caches decide what to discard when they run out of space.

Why Caches Can't Hold Everything

No cache has infinite memory. Whether it's your browser's cache, a Redis instance, or an application-level cache, eventually you'll hit capacity limits. When the cache is full and a new item needs to be stored, something old must go. This process is called eviction.

Think of a cache like a coat rack by your door: it holds the coats you wear most often. When it's full and someone brings a new coat, you need a strategy—do you remove the coat worn longest ago? The one worn least frequently? The first coat hung up?

Common Eviction Policies

LRU (Least Recently Used)

Evicts the item that hasn't been accessed in the longest time. LRU assumes recently-used data is more likely to be needed again soon. This is the most popular policy because it balances simplicity and effectiveness.

Analogy: Discarding the book at the bottom of your reading pile—you haven't touched it in weeks.

LFU (Least Frequently Used)

Evicts the item accessed least often over its lifetime. LFU protects frequently-accessed data but can struggle when access patterns change.

Analogy: Throwing out the tool you've only used once, even if you used it yesterday.

FIFO (First In, First Out)

Evicts the oldest cached item regardless of usage. Simple but ignores access patterns entirely.

Analogy: Removing the first item you put on that coat rack, even if you wore it this morning.

Choosing Your Strategy

LRU works well for most applications with temporal locality (recently-used data gets reused). LFU suits scenarios with stable, uneven access patterns. FIFO is rare in practice but simple to implement.

Key Takeaway: Cache eviction policies like LRU, LFU, and FIFO automatically manage memory constraints by removing less valuable items when capacity is reached, with LRU being the most commonly effective choice.