Cache Eviction Policies - Complete Enterprise Guide
Learn Cache Eviction Policies used in enterprise systems including LRU, LFU, FIFO, LIFO, MRU, Random Replacement, TTL-based eviction, Write-Through, Write-Back, Spring Boot integration, Redis, Caffeine, and real-world architecture.
Introduction
Caching is one of the most important performance optimization techniques in software engineering.
Almost every enterprise application uses caching to reduce:
- Database Calls
- API Calls
- Network Latency
- CPU Usage
- Response Time
Examples include:
- Banking Systems
- Insurance Platforms
- E-Commerce Websites
- Healthcare Applications
- Social Media Platforms
- Streaming Services
However, cache memory is limited.
When the cache becomes full, the system must decide:
Which cached data should be removed to make room for new data?
This decision is called a Cache Eviction Policy.
Choosing the correct eviction strategy significantly impacts application performance.
Why Do We Need Cache Eviction?
Imagine an application using Redis.
Available Memory:
Redis Cache
↓
100 MB
Application stores:
- Customer Details
- Products
- Orders
- Exchange Rates
- Sessions
Eventually the cache becomes full.
A new object arrives.
Question:
Which object should be removed?
That's where cache eviction policies help.
High-Level Cache Architecture
flowchart LR
CLIENT[Client]
CLIENT --> API[Spring Boot Application]
API --> CACHE[Redis / Caffeine Cache]
CACHE --> DATABASE[(Database)]
DATABASE --> CACHE
CACHE --> API
API --> CLIENT
Cache Lifecycle
flowchart LR
REQ["Request"]
CACHE["Cache Lookup"]
HIT["Cache Hit"]
MISS["Cache Miss"]
DB["Database"]
RESP["Response"]
REQ --> CACHE
CACHE --> HIT --> RESP
CACHE --> MISS --> DB
DB --> CACHE
CACHE --> RESP
What is Cache Eviction?
Cache eviction is the process of removing existing cached data when cache capacity is reached.
Goals:
- Maximize cache hit ratio
- Minimize database calls
- Improve response time
- Efficient memory utilization
Types of Cache Eviction Policies
Enterprise systems commonly use:
- LRU (Least Recently Used)
- LFU (Least Frequently Used)
- FIFO (First In First Out)
- LIFO (Last In First Out)
- MRU (Most Recently Used)
- Random Replacement
- TTL-Based Expiration
Least Recently Used (LRU)
LRU removes the item that has not been accessed for the longest time.
Example:
Cache
A
B
C
D
Recent Access:
D
C
B
A
↓
Evict A
LRU Workflow
flowchart LR
FULL["Cache Full"]
FIND["Find Least Recently Used Item"]
EVICT["Evict Item"]
INSERT["Insert New Data"]
FULL --> FIND --> EVICT --> INSERT
Advantages
- Excellent for web applications
- High cache hit rate
- Widely supported
Use Cases
- Spring Boot
- Redis
- Caffeine Cache
- Hibernate Second-Level Cache
Least Frequently Used (LFU)
LFU removes the item with the lowest access frequency.
Example:
Product A
Accessed 100 times
Product B
Accessed 5 times
↓
Evict Product B
LFU Workflow
flowchart LR
ACCESS["Track Access Count"]
LOW["Find Lowest Frequency Item"]
EVICT["Evict Item"]
INSERT["Insert New Item"]
ACCESS --> LOW --> EVICT --> INSERT
Advantages
- Excellent for frequently accessed data
- Stable cache contents
Use Cases
- Product Catalog
- Banking Exchange Rates
- Frequently Used Configuration
First In First Out (FIFO)
FIFO removes the oldest inserted item.
Example:
Inserted
A
↓
B
↓
C
↓
D
↓
Evict A
Access frequency is ignored.
Advantages
- Very simple
- Easy implementation
Disadvantages
Frequently accessed items may still be removed.
Last In First Out (LIFO)
LIFO removes the most recently inserted item.
Example:
Cache
A
B
C
D
↓
Evict D
Rarely used for application caches but useful in specialized workloads.
Most Recently Used (MRU)
MRU removes the most recently accessed item.
Example:
Recently Accessed
↓
Customer 105
↓
Evict Customer 105
Useful when recently accessed data is unlikely to be reused immediately.
Random Replacement
Randomly removes one cached item.
Cache Full
↓
Random Selection
↓
Evict
Advantages:
- Very low overhead
- Simple implementation
Disadvantages:
- Unpredictable cache performance
Time-To-Live (TTL)
Each cache entry has an expiration time.
Example:
Exchange Rate
↓
Expires in 10 Minutes
After expiration:
- Entry removed automatically
- Fresh data loaded on next request
TTL Workflow
flowchart LR
ENTRY["Cache Entry"]
TTL["TTL Countdown"]
EXPIRED["Expired"]
REMOVE["Remove from Cache"]
RELOAD["Reload from Source"]
ENTRY --> TTL --> EXPIRED --> REMOVE --> RELOAD
Idle Time Expiration
Some caches expire entries if they are not accessed for a configured period.
Example:
Session
↓
Not Used
↓
30 Minutes
↓
Removed
Commonly used for user sessions.
Redis Eviction Policies
Redis supports multiple policies.
Examples:
| Policy | Description |
|---|---|
| noeviction | Reject new writes when memory is full |
| allkeys-lru | LRU across all keys |
| volatile-lru | LRU only on keys with TTL |
| allkeys-lfu | LFU across all keys |
| volatile-lfu | LFU only on TTL keys |
| allkeys-random | Random eviction |
| volatile-random | Random eviction on TTL keys |
| volatile-ttl | Evict keys with the nearest expiration |
Caffeine Cache Policies
Spring Boot commonly uses Caffeine.
Supports:
- Maximum Size
- Expire After Write
- Expire After Access
- Refresh After Write
- LRU-like eviction strategy
Ideal for in-memory application caching.
Write Strategies
Cache Aside
flowchart LR
Application
-->
Cache
Cache --> Database
Database --> Cache
Most popular approach.
Write Through
Application
↓
Cache
↓
Database
Both cache and database are updated together.
Write Back
Application
↓
Cache
↓
Database Later
Faster writes but requires careful handling to avoid data loss.
Spring Boot Integration
Spring Boot supports caching using:
- Spring Cache
- Redis
- Caffeine
- Hazelcast
- Ehcache
- Infinispan
Example:
@Cacheable("customers")
public Customer findById(Long id){
return repository.findById(id).orElseThrow();
}
Enterprise Architecture
flowchart TD
CLIENT[Client]
CLIENT --> LB[Load Balancer]
LB --> API[Spring Boot APIs]
API --> REDIS[(Redis Cache)]
API --> DATABASE[(PostgreSQL)]
DATABASE --> REDIS
API --> CLOUDWATCH[Monitoring]
Banking Example
Customer Balance
Customer
↓
Redis
↓
Database
Balance cached for 5 minutes.
E-Commerce Example
Popular Products
Products
↓
Redis
↓
Millions of Reads
Frequently viewed products remain cached.
Healthcare Example
Doctor Schedule
Schedule
↓
Cache
↓
Hospital Database
Updated every few minutes.
Cache Eviction Comparison
| Policy | Best For | Performance |
|---|---|---|
| LRU | General Applications | Excellent |
| LFU | Frequently Accessed Data | Excellent |
| FIFO | Simple Systems | Good |
| LIFO | Specialized Workloads | Moderate |
| MRU | Unique Access Patterns | Moderate |
| Random | Low Overhead | Variable |
| TTL | Frequently Changing Data | Excellent |
Choosing the Right Policy
| Scenario | Recommended Policy |
|---|---|
| Product Catalog | LFU |
| User Sessions | TTL |
| Banking Accounts | LRU + TTL |
| Exchange Rates | TTL |
| Search Results | LRU |
| Configuration | LFU |
| Notifications | TTL |
Common Mistakes
❌ Cache everything
❌ Very large TTL values
❌ No cache invalidation strategy
❌ Small cache size
❌ Ignoring memory usage
❌ Caching frequently changing data without expiration
❌ Missing monitoring
Best Practices
- Use LRU for most enterprise applications.
- Apply TTL to frequently changing data.
- Cache only expensive operations.
- Monitor cache hit ratio.
- Set appropriate maximum cache size.
- Use Redis for distributed caching.
- Use Caffeine for local application caching.
- Refresh critical data proactively when appropriate.
- Avoid caching sensitive information unless properly protected.
- Design clear cache invalidation strategies.
Enterprise Use Cases
Banking
- Customer Profiles
- Exchange Rates
- Branch Information
Insurance
- Policy Details
- Premium Plans
- Product Catalog
Healthcare
- Hospital Information
- Doctor Schedules
- Appointment Slots
Retail
- Products
- Categories
- Pricing
- Inventory Snapshots
Social Media
- User Profiles
- Trending Topics
- News Feed Metadata
Interview Questions
- What is cache eviction?
- Explain LRU.
- Explain LFU.
- What is TTL?
- What is the difference between LRU and LFU?
- What eviction policies does Redis support?
- When should you use Caffeine instead of Redis?
- What is Cache Aside?
- What is Write Through caching?
- How do you improve cache hit ratio?
Summary
Cache eviction policies determine how a cache manages limited memory while maximizing application performance.
Enterprise systems typically combine:
- LRU for general-purpose caching
- LFU for highly accessed data
- TTL for frequently changing information
- Cache Aside for application-level caching
- Redis for distributed caching
- Caffeine for local in-memory caching
Choosing the correct eviction strategy reduces database load, improves response time, increases cache hit ratio, and enables Spring Boot applications to scale efficiently in banking, insurance, healthcare, retail, and other high-performance enterprise environments.