How to Prevent Cache Penetration, Breakdown, and Avalanche in Redis

The article explains the concepts of cache penetration, cache breakdown, and cache avalanche, illustrates why they cause massive database load, and provides practical mitigation techniques such as parameter validation, storing null placeholders, Bloom filters, rate limiting, hot‑key pre‑warming, distributed locking, and cache pre‑warming strategies, with Java code examples.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
How to Prevent Cache Penetration, Breakdown, and Avalanche in Redis

Cache Penetration

Cache penetration occurs when a large number of requests contain keys that are invalid – they do not exist in the cache and also do not exist in the database. These requests bypass the cache layer and hit the database directly, potentially overwhelming it.

Cache penetration illustration
Cache penetration illustration

Example: an attacker generates a massive number of illegal keys, causing every request to miss both cache and database and putting huge pressure on the database.

Mitigation Strategies for Cache Penetration

Validate request parameters early; reject obviously illegal values (e.g., negative IDs, malformed email formats) with an error response.

Cache null placeholder : when a key is missing in both cache and database, store a dummy value in Redis with a short TTL. Example command: SET key value EX 60 Short TTL (e.g., 60 seconds) limits attack amplification.

Standard key naming convention: table:column:primaryKey:primaryKeyValue Bloom filter : pre‑populate a Bloom filter with all valid keys. On each request, check the filter; if it reports the key does not exist, return an error without querying the database. For 1,000,000 elements the filter consumes about 122 KB.

Bloom filter principle diagram
Bloom filter principle diagram

Interface rate limiting per user or IP, optionally adding offending IPs to a blacklist.

Java example for caching null values:

public Object getObjectInclNullById(Integer id) {
    // Try cache first
    Object cacheValue = cache.get(id);
    if (cacheValue == null) {
        // Fallback to DB
        Object dbValue = storage.get(id);
        // Cache the result (including null)
        cache.set(id, dbValue);
        if (dbValue == null) {
            // Set a short expiration to avoid amplification
            cache.expire(id, 60 * 5); // 5 minutes
        }
        return dbValue;
    }
    return cacheValue;
}

Cache Breakdown (Cache Stampede)

Cache breakdown happens when a hot key exists in the database but is missing in the cache (usually because the cached entry expired). A sudden surge of requests for that hot data then all hit the database, potentially causing a crash.

Cache breakdown illustration
Cache breakdown illustration

Mitigation Strategies for Cache Breakdown

Set hot data to never expire or give it a long TTL.

Pre‑warm hot data before expiration (e.g., keep flash‑sale items cached until the sale ends).

Use a distributed mutex lock: before querying the database and writing to cache, acquire a lock so only one request performs the DB read while others wait for the cached result.

Cache Avalanche

A cache avalanche occurs when a large portion or all of the cache expires at the same time, or when the Redis service becomes unavailable. The sudden flood of requests then bypasses the cache and overwhelms the database.

Cache avalanche illustration
Cache avalanche illustration

Mitigation Strategies for Cache Avalanche

Deploy Redis in a cluster to avoid single‑node failure.

Apply rate limiting to smooth traffic spikes.

Use multi‑level caching (local + Redis) so that a Redis outage still allows partial data retrieval.

Randomize TTL for keys to prevent simultaneous expiration.

Cache pre‑warming: schedule tasks (e.g., with XXL‑Job) or use a message queue (e.g., Kafka) to repopulate hot data after startup or before expiration.

Differences Between the Three Issues

Cache penetration : the requested key is absent from both cache and database.

Cache breakdown (stampede) : the key corresponds to hot data that exists in the database but is missing in the cache, usually because the cached entry expired.

Cache avalanche : massive simultaneous cache expiration or cache service outage causes a flood of requests to the database.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaCacheredisbloom-filtercache-avalanchecache-breakdowncache-penetration
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.