Step 1 · The pattern
Many keys, each with a TTL
The cache holds lots of keys. Each has a TTL, a countdown after which it is thrown away and rebuilt from the database on the next request.
- request
- cache hit
In short
TTL jitter: how it works and how it breaks
Caches tend to be filled in bursts: after a deploy, a restart, a nightly job or a traffic spike. If every key written in that burst gets the same time-to-live, they all expire at the same instant too. The database, which normally sees a trickle of cache misses, suddenly has to rebuild everything at once. Then the rebuilt keys are written together with the same TTL, so the wave returns on schedule. This is sometimes called a cache avalanche. The fix is small: add a random offset to each TTL, for example the base value plus or minus ten to twenty percent. Expiries drift apart, and the same number of rebuilds arrive as a steady trickle the database can absorb. Jitter spreads different keys apart. It does not stop a stampede on one very hot key; that needs leases or request coalescing.
In Spring Boot
A jittered TTL for Spring Cache on Redis
@Bean
RedisCacheConfiguration cacheConfiguration() {
return RedisCacheConfiguration.defaultCacheConfig()
.entryTtl((key, value) -> Duration.ofSeconds(
// 10 minutes, plus up to 2 minutes chosen per entry
600 + ThreadLocalRandom.current().nextLong(0, 121)));
}Spring Boot uses this bean as the default for every @Cacheable cache backed by Redis. The function runs for each entry as it is written, so entries cached in the same burst expire at different moments.
Gotcha: The property spring.cache.redis.time-to-live gives every entry the same fixed TTL, which is exactly the trap in this lesson; leave it unset and entries never expire at all. The per-entry function needs Spring Data Redis 3.2 or later.
Names checked against the official docs on 15 Nov 2026:
How this comes up in interviews
"After every deploy your database gets a burst of load at regular intervals, then calms down again. What would you look for?"
Then they ask
- How do you choose how much jitter to add, and what does it do to data freshness?
- You added jitter and one very popular key still floods the database when it expires. Why?
- What happens the first time, when the cache is completely empty after a restart?
A strong answer covers
- Keys written in the same burst with the same TTL expire together, so the rebuilds arrive together, and again one TTL later.
- Add a random offset to each TTL, for example the base value plus or minus ten to twenty percent.
- The number of rebuilds is unchanged. They arrive as a trickle instead of a wave.
- Jitter separates different keys. One hot key still needs a lease or request coalescing.
- An empty cache needs warming or a limit on concurrent rebuilds, because jitter only spreads later expiries.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Sources: AWS Builders' Library: Caching challenges and strategies ↗ · Scaling Memcache at Facebook (NSDI '13) ↗ · Redis docs: EXPIRE ↗ · Report a problem
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.