Databases 16 min read

Preventing Redis Cache Failures: Avalanche, Penetration, and Breakdown Solutions

This article explains the three main Redis cache anomalies—cache avalanche, cache penetration, and cache breakdown—detailing their symptoms, root causes, and practical mitigation strategies with Java code examples and architectural recommendations.

Programmer DD
Programmer DD
Programmer DD
Preventing Redis Cache Failures: Avalanche, Penetration, and Breakdown Solutions

1 Introduction

Redis is the most popular NoSQL database used primarily as a cache to improve query efficiency and protect underlying databases, but various abnormal scenarios can cause Redis to lose its caching role.

2 Abnormal Types

Cache anomalies mainly include cache avalanche, cache penetration, and cache breakdown.

2.1 Cache Avalanche

Phenomenon: A large number of requests miss the cache and directly hit the database, increasing database pressure and potentially causing a system-wide outage, similar to an avalanche.

Causes:

Cache service unavailable.

Cache service available but many keys expire simultaneously.

Solutions:

Use high‑availability deployment modes such as Sentinel or Cluster to avoid single‑point failures.

Set keys to never expire (PERSIST) when appropriate, though this increases memory usage.

Assign random expiration times to keys to prevent simultaneous expiration.

Implement secondary caches with different TTLs.

Update cache expiration asynchronously using background tasks.

Cache avalanche illustration
Cache avalanche illustration

2.2 Cache Penetration

Phenomenon: Queries for non‑existent data miss both cache and database, causing repeated database hits and high load.

Cause: Illegal or malicious calls requesting data that does not exist.

Solutions:

Cache empty values to reduce repeated database queries.

Use a Bloom filter to pre‑check whether a key might exist.

Cache penetration illustration
Cache penetration illustration

2.3 Cache Breakdown

Phenomenon: When a hot key expires, a surge of requests simultaneously miss the cache and hit the database, potentially overwhelming it.

Cause: Expiration of hot keys under high concurrency.

Solutions:

Set hot keys to never expire or use random expiration times.

Employ secondary caches with staggered TTLs.

Use distributed locks so only one request accesses the database while others wait.

Refresh cache asynchronously after serving a request.

Cache breakdown illustration
Cache breakdown illustration

3 Code Samples

/**
 * Randomly set an expiration time less than 30 minutes
 */
private void setRandomTimeForReidsKey(String redisKey, String value) {
    Random rand = new Random();
    int times = rand.nextInt(1800); // seconds
    redisClient.setNxEx(redisKey, value, times);
}
public static void main(String[] args) {
    CacheTest test = new CacheTest();
    String value = test.queryByOneCacheKey("key");
    if (StringUtils.isBlank(value)) {
        value = test.queryBySecondCacheKey("key");
        if (StringUtils.isBlank(value)) {
            System.out.println("Data not found!");
        } else {
            test.secondCacheSave("key", value);
            test.oneCacheSave("key", value);
            System.out.println("Data returned from DB!");
        }
    } else {
        System.out.println("Data returned from first cache!");
    }
}
public class CacheRunnable implements Runnable {
    private ClusterRedisClientAdapter redisClient;
    public String key;
    public CacheRunnable(String key) { this.key = key; }
    @Override
    public void run() { redisClient.expire(this.key, 1800); }
    public String getKey() { return key; }
    public void setKey(String key) { this.key = key; }
}
public String queryForMessage(String key) throws InterruptedException {
    String result = queryByOneCacheKey(key);
    if (StringUtils.isNotBlank(result)) return result;
    if (lockByBusiness(key)) {
        result = getFromDb(key);
        if (StringUtils.isNotBlank(result)) oneCacheSave(key, result);
    } else {
        Thread.sleep(500);
        return queryForMessage(key);
    }
    return result;
}

4 Conclusion

Redis caching is crucial for system performance. This article presented common cache anomalies and practical mitigation techniques with sample code. While not exhaustive, the provided solutions can guide developers in handling cache‑related issues effectively.

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.

Distributed SystemsJavaCacheRediscache avalanchecache breakdowncache penetration
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.