What's the difference between cache invalidation and eviction?

Invalidation is primarily about correctness — the cached value should no longer be considered valid because the underlying data changed or its freshness window expired.

Eviction is primarily about cache resource management — removing an entry to free space or according to a cache policy such as LRU or LFU. An entry can be valid but evicted, or stale but still physically present until it’s removed.

InvalidationEviction
Main concernCorrectnessCapacity
Why?Data became staleCache needs space
Who usually triggers it?Application/business eventCache system
Does value have to be stale?Usually yesNo
ExampleProduct price changedRedis is full
Typical mechanismDelete/update/versionLRU/LFU/TTL policy

Explain the execution differences between @Cacheable, @CachePut, and @CacheEvict in Spring Boot.

@Cacheable skips execution on a cache hit. @CachePut always executes to update the cache. @CacheEvict deletes cache entries.

💡 Best answer

These three dictate how Spring’s AOP proxies interact with the caching layer. @Cacheable intercepts the call, checks the cache for the key, and on a hit returns the cached value — bypassing the method entirely.

🔬 Deeper explanation

@CachePut guarantees the method runs; it’s for refreshing a stale entry after a mutation. @CacheEvict purges entries (often allEntries=true) when the underlying record is deleted, so users never see stale ghost data.

➡️ Follow-up What happens if you put @Cacheable on a private method in a Spring service? (Nothing — proxy-based AOP can't intercept self-invoked private methods.)

🏗️ Real-world usage

@Cacheable on getUserById(), @CachePut on updateUser(), @CacheEvict on deleteUser().

What is a Cache Stampede (Thundering Herd), and how do you prevent it? `

Occurs when a hot key expires and thousands of threads hit the DB at once. Prevent with distributed locks (single-flight) or TTL jitter.

Contrast Cache-Aside, Write-Through, and Write-Behind.

Cache-Aside checks cache then DB. Write-Through writes both synchronously. Write-Behind writes cache and syncs to DB asynchronously.