Cache Stampede, Invalidation, and Consistency
Prevent thundering herds and stale reads with key design, conditional caching, and synchronized loads.
The Problem: Cache Stampede
A cache stampede (a.k.a. thundering herd or dog-piling) happens when a hot cache entry expires and many concurrent requests all miss at once. Each request then hits the slow backing store (database, remote API) to recompute the same value.
- One popular key expires
- 1,000 requests arrive in the same instant
- All 1,000 see a miss and stampede the database
- The DB spikes, latency explodes, sometimes it falls over
In this lesson you'll learn to prevent thundering herds and stale reads using key design, conditional caching, and synchronized loads in Spring Boot 4.
Synchronized Loading with sync = true
Spring's @Cacheable supports sync = true. When several threads miss the same key at the same time, only one thread computes the value while the others block and wait for the result. This collapses the herd to a single load per key.
- Requires a cache manager that supports synchronized loading (Caffeine does)
- You cannot combine
sync = truewith multiple cache names,unless, or a customconditionthat depends on the return value
@Service
public class ProductService {
@Cacheable(cacheNames = "products", key = "#id", sync = true)
public Product findById(Long id) {
// Only ONE thread runs this per key, even under a stampede
return loadFromDatabase(id);
}
}All lessons in this course
- The Spring Cache Abstraction Fundamentals
- In-Memory Caching with Caffeine Tuning
- Distributed Caching with Redis and TTLs
- Cache Stampede, Invalidation, and Consistency