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

Example

Consider an e-commerce application caching product information.

Product Cache Entry

Product ID: 101

Price: ₹500

Scenario 1 - Cache Expiration

Scenario 2 - Cache Eviction

Cache Expiration

Purpose

Trigger

Common Strategies

Examples

Example

User Profile Cache

TTL = 30 minutes

After 30 minutes

Cache Eviction

Purpose

Trigger

Common Eviction Policies

Examples

Comparison

Cache Expiration

Cache Eviction

Real-World Example

Online Shopping Application

Expiration

Eviction

Best Practice

// 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.

Last updated: 1 November 2026

Previous question: How is Spring Security implemented in a Spring Boot application?

Next question: If you had to scale a Spring Boot application to handle high traffic, what strategies would you use?

More Spring Boot - Level II interview questions

All Spring Boot - Level II questions