Your database CPU spikes to near-maximum every few minutes, right on schedule. Application response times go through the roof at the same interval. The database itself is fine — no slow queries, no lock contention, no schema problem. The pattern is too regular to be organic traffic. If your application uses Redis as a cache layer, there is a good chance thousands of keys are expiring at the exact same moment, and every request that would have been served from cache is now slamming your database simultaneously.
This is a cache stampede, sometimes called a thundering herd. It happens when a large batch of cache keys share the same time-to-live (TTL) value and were written around the same time, so they all expire together.
What the symptom looks like
The pattern is distinctive. You see periodic spikes in database queries — not random, not correlated with user traffic patterns, but repeating at a fixed interval that matches your cache TTL. If your application sets all product page caches to 300 seconds, you get a spike every five minutes. If session-related keys all use 3600 seconds and were populated during a traffic burst, you get a wall of database hits exactly one hour later.
From the application side, response times degrade sharply for a few seconds, then recover as the cache refills. From the database side, you see a sudden jump in connections and queries. If the database cannot absorb the burst, connections queue up, timeouts start, and what should have been a routine cache refresh becomes a partial or full outage.
Support engineers often chase this in the wrong direction first — checking for slow queries, looking at network issues, or suspecting a cron job. The regularity of the spike is the clue. If it matches a TTL value your application uses, look at Redis.
Sampling TTLs with redis-cli
Before fixing anything, confirm the problem exists. You need to see how TTL values are distributed across your keys. Redis does not have a built-in histogram for this, but you can sample it with a short script using redis-cli.
This one-liner scans a sample of keys and prints their remaining TTL in seconds:
redis-cli --scan --pattern '*' --count 1000 | head -500 | while read key; do
ttl=(redis-cli TTL "key")
echo "ttlkey"
done | sort -n | head -50
What you are looking for: clusters. If 400 out of 500 sampled keys all show a TTL within a narrow band — say, all between 280 and 300 — that confirms your keys were written around the same time with the same TTL and will expire together.
A few notes on interpreting the output. A TTL of -1 means the key has no expiry set (it lives forever). A TTL of -2 means the key does not exist (it may have expired between the SCAN and the TTL call). Neither of those is your problem. You are looking for large groups of keys with similar positive TTL values.
If you have many key prefixes (like product:*, session:*, user:*), run the scan per prefix to see which group is clustered:
redis-cli --scan --pattern 'product:*' --count 1000 | head -200 | while read key; do
echo "(redis-cli TTL "key") $key"
done | sort -n
This tells you exactly which cache namespace is the problem.
Eliminating red herrings
Before concluding it is a stampede, rule out a few other causes of periodic database spikes:
Cron jobs. Check if any scheduled task runs at the same interval. A cron that rebuilds a report or clears old records can produce similar-looking spikes. Compare the spike timing against your crontab entries.
Application-level cache warming. Some frameworks have a cache warm-up routine that runs on deploy or on a timer. If the spike only happens after deployments, that is a different problem.
Connection pool exhaustion. If your application opens new database connections on every cache miss instead of using a pool, even a moderate number of simultaneous misses can look catastrophic. Check your connection pool configuration separately.
Lazy expiration vs active expiration in Redis. Redis does not expire all keys the instant their TTL hits zero. It uses a combination of lazy expiration (checking TTL when a key is accessed) and active expiration (sampling random keys periodically and deleting expired ones). This means the actual moment keys disappear is slightly spread out — but not enough to prevent a stampede if thousands of keys expire within the same few-second window. The database spike happens when the application requests those keys and gets misses, not when Redis deletes them.
Confirming the stampede pattern
If your TTL sample shows clustering and the database spike interval matches the TTL value, you have confirmed the root cause. For extra confirmation, you can watch Redis key misses in real time during a spike:
redis-cli INFO stats | grep keyspace_misses
Run this a few times in quick succession during normal operation, then again when you expect the spike. A sharp jump in keyspace_misses during the spike window confirms that the application is requesting keys that no longer exist, triggering database fallback.
Fixing it: add jitter to TTL
The fix is straightforward. Instead of setting every key to the same TTL, add a random offset — commonly called jitter. If your base TTL is 300 seconds, set each key to a random value between, say, 270 and 330 seconds. This spreads expirations across a 60-second window instead of having them all hit at second 300.
In application code, this typically looks like adding a random number to the TTL when you write to cache. The exact implementation depends on your language and Redis client library, but the logic is always the same: base_ttl + random(0, jitter_range).
How much jitter to add depends on how many keys expire together and how much burst your database can handle. A rule of thumb: jitter of 10-20% of the base TTL is usually enough to flatten the spike into a manageable trickle. For a 300-second TTL, that means randomizing between 270 and 330 seconds. For a 3600-second TTL, randomizing between 3240 and 3960 seconds.
This is the simplest fix and handles the majority of stampede cases.
Alternative: early refresh before expiry
Jitter spreads expirations out, but every key still expires and causes a cache miss. An alternative approach is to refresh keys before they expire. The application checks the remaining TTL when reading a key, and if it is below some threshold, triggers a background refresh while still serving the cached value.
This is sometimes called probabilistic early recomputation. The idea: as a key gets closer to expiry, each request has an increasing probability of triggering a refresh. This way, popular keys get refreshed before they expire, and no request ever sees a miss for a hot key.
This approach is more complex to implement and is typically worth it only for keys that are expensive to regenerate (heavy database queries, external API calls) and accessed frequently. For most caching scenarios, jitter alone is sufficient.
When stampedes cascade beyond the database
A cache stampede does not always stop at the database. If your database becomes slow or unresponsive under the burst load, upstream services start timing out. Load balancers may start health-check failures. If your application retries failed requests, the retry storm amplifies the original burst. This is the cascading failure pattern — one layer's problem becomes every layer's problem.
This is the same general mechanism behind many large-scale outages: a thundering herd at one layer propagates through tightly coupled services. If your architecture has multiple cache layers or microservices that depend on the same database, a stampede in one service can starve others of database connections.
Practical takeaway
If you see regular, clock-like database spikes, sample your Redis TTLs. If they cluster, add jitter. It is a small code change — often a single line — that prevents a class of outage that looks mysterious until you know what to look for. Do the sampling before you add jitter, so you have evidence of the problem and can verify the fix worked by sampling again after deployment. The spikes should flatten into a steady, lower rate of cache misses spread across the full jitter window.