How LRU Cache Replacement Works
Imagine your CPU needs data that's not in its small, fast cache. Something has to go to make room for the new data. But what? The Least Recently Used (LRU) policy tackles this by tracking which items haven't been touched for the longest time and replacing them first.
Core Concept
LRU operates on a simple principle: If you haven't used something in a long time, you probably won't need it soon.
This exploits temporal locality – the observation that programs tend to access recently used data again in the near future.
How LRU Tracking Works
For each cache set, LRU maintains a relative "age" for each line, recording when it was last accessed:
-
When any cache line is accessed (read or written):
- Mark it as "most recently used"
- Make all other lines "older"
-
When replacement is needed:
- Identify and evict the "oldest" line
- Load the new data into this position
- Mark the new line as "most recently used"
Example: 4-Way Set Associative Cache
Imagine a cache set with 4 lines. Let's track LRU with numbers (0 = most recent, 3 = least recent):
Initial state:
Line A: age 0 (most recent) - contains address 0x1000
Line B: age 1 - contains address 0x2000
Line C: age 2 - contains address 0x3000
Line D: age 3 (least recent) - contains address 0x4000
CPU accesses address 0x3000 (hit on Line C):
Line C: age 0 (promoted to most recent)
Line A: age 1 (pushed down)
Line B: age 2 (pushed down)
Line D: age 3 (still least recent)
CPU accesses address 0x5000 (miss, need replacement):
[Line D is evicted as it's the least recently used]
New line: age 0 (most recent) - contains address 0x5000
Line C: age 1 (pushed down)
Line A: age 2 (pushed down)
Line B: age 3 (now least recent)
Real-World Implementation Methods
Implementing perfect LRU becomes challenging as associativity increases. Common approaches include:
-
Counter Method: Maintain a global counter, store the access time with each line
-
Stack/List Method: Maintain lines in a priority list, move accessed line to top
-
Age Matrix: Use a triangular matrix of bits to track relative age between any two lines
-
Pseudo-LRU (Tree-based): Use a binary tree of bits to approximate LRU decisions with less overhead
Practical Example: 2-bit LRU for 4-way Set
For a 4-way set, you need log₂(4!) = 4 bits for perfect LRU tracking. But many caches use approximations.
A simple 3-bit pseudo-LRU implementation uses a binary tree:
Root bit
/ \
Left bit Right bit
/ \ / \
0 1 2 3 (cache lines)
When accessing a line, update the bits to point away from the accessed line.
Advantages of LRU
- Exploits temporal locality effectively
- Adaptive to changing access patterns
- Better hit rates than random or FIFO for most workloads
Limitations
- Hardware cost increases with associativity
- Not always optimal for certain access patterns (e.g., scanning large arrays)
- Perfect LRU is rarely implemented in hardware for high-associativity caches
LRU in Modern Systems
Modern processors often use:
- PLRU (Pseudo-LRU): Tree-based approximation
- QLRU (Quad-LRU): Partition ways into groups with local LRU
- RRIP (Re-Reference Interval Prediction): Predict future reuse distance
- DIP (Dynamic Insertion Policy): Adaptively switch between LRU and other policies
The concept of LRU extends beyond CPU caches to page replacement in virtual memory, database buffer management, and web browser caches – anywhere a limited fast resource needs intelligent management.