Explain the difference between cache eviction and cache expiration.
Cache eviction and cache expiration are two different mechanisms used to remove data from a cache, but they serve different purposes. Cache eviction focuses on managing cache capacity, while cache expiration focuses on maintaining data freshness and consistency.
Key Points
Cache eviction removes entries when the cache reaches its size limit.
Cache expiration removes entries after a configured time period has elapsed.
Both mechanisms are often used together in production systems for optimal performance and accuracy.
Example
Consider an e-commerce application caching product information.
Product Cache Entry
Product ID: 101
Price: ₹500
Scenario 1 - Cache Expiration
The cache entry is configured with a TTL of 10 minutes.
After 10 minutes, the entry automatically expires even if there is enough cache space available.
Scenario 2 - Cache Eviction
The cache has reached its maximum size of 10,000 entries.
A new entry needs to be added.
The cache removes the least recently used item to create space.
Cache Expiration
Purpose
Maintain fresh and updated data.
Trigger
Time-based removal.
Common Strategies
Time To Live (TTL)
Time To Idle (TTI)
Examples
Session Tokens
Currency Exchange Rates
Product Prices
Example
User Profile Cache
TTL = 30 minutes
After 30 minutes
Cache entry expires automatically.
Next request fetches fresh data from the database.
Cache Eviction
Purpose
Prevent memory exhaustion.
Control cache size.
Trigger
Cache size limit reached.
Common Eviction Policies
LRU (Least Recently Used)
LFU (Least Frequently Used)
FIFO (First In First Out)
Examples
Redis
Caffeine
EhCache
Comparison
Cache Expiration
Triggered by time.
Improves data consistency.
Example: Remove entry after 15 minutes.
Cache Eviction
Triggered by memory or size limits.
Improves memory management.
Example: Remove least recently used entry.
Real-World Example
Online Shopping Application
Expiration
Product prices expire every 10 minutes to ensure accurate pricing.
Eviction
Cache size is limited to 50,000 products.
Older or rarely accessed products are removed when space is required.
Best Practice
Use expiration for frequently changing data.
Use eviction to control memory usage.
Combine both strategies for better performance and stability.
// Caffeine combines both: size-based eviction and time-based expiration
@Bean
CacheManager cacheManager() {
CaffeineCacheManager manager = new CaffeineCacheManager("products");
manager.setCaffeine(Caffeine.newBuilder()
.maximumSize(50_000) // EVICTION: least useful entries
// removed when the cache is full
.expireAfterWrite(Duration.ofMinutes(10)) // EXPIRATION: entries older than
// 10 minutes are invalid
.recordStats());
return manager;
}
// Manual eviction when the data changes
@CacheEvict(value = "products", key = "#product.id")
public void update(Product product) { repository.save(product); }
Interview Tip
A concise interview answer is: Cache expiration removes data after a predefined time period to ensure freshness, whereas cache eviction removes data when the cache reaches its capacity limit based on policies such as LRU or LFU. Expiration manages data validity, while eviction manages memory utilization.