Understanding Redis Cache Breakdown, Penetration, and Avalanche (Java Examples)

The article explains the definitions, typical scenarios, and practical Java solutions for Redis cache breakdown, cache penetration, and cache avalanche, showing how mutex locks, Bloom filters, and staggered expirations can prevent database overload and improve system reliability.

java1234
java1234
java1234
Understanding Redis Cache Breakdown, Penetration, and Avalanche (Java Examples)

Cache Breakdown

Definition

Cache breakdown occurs when a hot key expires under high concurrency, causing a massive number of requests to hit the database simultaneously and putting huge pressure on it.

Scenario

If a user's data is missing from the cache and many requests query the database at once, the database may become overwhelmed or crash.

Solution

Use a mutex lock (e.g., Redis SETNX) so that only one request queries the database while others wait.

Java example

import redis.clients.jedis.Jedis;

public class CacheTest {
    private Jedis jedis = new Jedis("localhost");

    public String getData(String key) throws InterruptedException {
        String data = jedis.get(key);
        if (data == null) {
            String lockKey = "lock:" + key;
            String lockValue = String.valueOf(System.currentTimeMillis() + 10000); // 10‑second lock
            if (jedis.setnx(lockKey, lockValue) == 1) {
                try {
                    data = queryDatabase(key);
                    jedis.set(key, data);
                } finally {
                    jedis.del(lockKey); // release lock
                }
            } else {
                Thread.sleep(100);
                return getData(key);
            }
        }
        return data;
    }

    private String queryDatabase(String key) {
        // Simulated DB query
        return "data_for_" + key;
    }
}

Cache Penetration

Definition

Cache penetration happens when a request queries data that does not exist in both the cache and the database, causing every request to hit the database and burden it.

Scenario

Attackers may send a large number of requests for nonexistent IDs, forcing the database to process many useless queries.

Solution

Introduce a Bloom filter to pre‑filter invalid requests, or cache a null object for missing data.

Java example

import redis.clients.jedis.Jedis;

public class CacheTest {
    private Jedis jedis = new Jedis("localhost");

    public String getData(String key) {
        // Simulated Bloom filter check
        if (!isKeyValid(key)) {
            return null; // filter out invalid request
        }
        String data = jedis.get(key);
        if (data == null) {
            data = queryDatabase(key);
            if (data == null) {
                // Cache empty placeholder for missing data
                jedis.set("empty:" + key, "");
            } else {
                jedis.set(key, data);
            }
        }
        return data;
    }

    private boolean isKeyValid(String key) {
        // Simulated Bloom filter validity check
        return key.matches("^[a-zA-Z0-9]+$"); // allow only alphanumerics
    }

    private String queryDatabase(String key) {
        // Simulated DB query
        return "data_for_" + key;
    }
}

Cache Avalanche

Definition

Cache avalanche refers to a situation where a large number of cached entries expire at the same moment, making the cache unavailable and forcing all requests to go directly to the database.

Scenario

If many caches share the same expiration time, they may all become invalid simultaneously, causing a sudden surge of database traffic.

Solution

Assign different expiration times to cached items, often using randomization, to avoid simultaneous expiry.

Java example

import redis.clients.jedis.Jedis;

public class CacheTest {
    private Jedis jedis = new Jedis("localhost");

    public String getData(String key) {
        String data = jedis.get(key);
        if (data == null) {
            data = queryDatabase(key);
            if (data != null) {
                int randomExpireTime = 60 + (int)(Math.random() * 60); // 60‑120 seconds
                jedis.setex(key, randomExpireTime, data);
            }
        }
        return data;
    }

    private String queryDatabase(String key) {
        // Simulated DB query
        return "data_for_" + key;
    }
}

These examples demonstrate practical techniques—mutex locks, Bloom filters, empty‑object caching, and staggered expirations—that help build a more stable and efficient Redis caching layer.

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.

cachingcache avalanchecache breakdowncache penetration
java1234
Written by

java1234

Former senior programmer at a Fortune Global 500 company, dedicated to sharing Java expertise. Visit Feng's site: Java Knowledge Sharing, www.java1234.com

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.