Understanding Redis Cache Issues: Penetration, Breakdown, and Avalanche
This article explains the three major Redis cache problems—cache penetration, cache breakdown, and cache avalanche—describes their causes, illustrates solutions such as empty-object caching, Bloom filters, locking, and provides practical code examples and mitigation strategies.
After a break from Redis, the author revisits the three major cache issues in Redis: cache penetration, cache breakdown, and cache avalanche.
Cache Penetration
Cache penetration occurs when a request queries data that does not exist in both the cache and the database, causing repeated database hits and high load.
Typical example: a user queries an ID that is never present (e.g., id = -1); the database returns nothing, leading to continuous queries.
Common solutions include caching a null object (an empty placeholder) or using a Bloom filter to pre‑filter nonexistent keys.
Empty Object Caching
An empty object is stored in the cache when the database returns no data, allowing subsequent requests to hit the cache and avoid database load.
To prevent the cache from filling with many empty objects, an expiration time can be set.
setex key seconds value // set key with an expiration time (seconds)In Java, this can be done via the API:
redisCache.put(Integer.toString(id), null, 60) // expires after 60 secondsBloom Filter
A Bloom filter is a probabilistic data structure that quickly checks whether an element may exist in a set; it can produce false positives but never false negatives.
Key characteristics:
A large binary bit array (0/1 only)
Multiple hash functions
High space and query efficiency
No delete operation, making maintenance harder
When adding a key, each hash function maps the key to a position in the bit array and sets that bit to 1.
False positives happen when different keys set the same bits, causing a non‑existent key to appear present.
Improving accuracy depends on hash function quality, bit array size, and the number of hash functions.
Cache Breakdown
Cache breakdown happens when a hot key expires or a cold key suddenly receives a massive number of requests, causing a surge of database queries.
Two main causes:
A rarely accessed (cold) key receives a flood of requests.
A popular key expires exactly when many users request it.
Typical mitigation is to use locking: when a key expires, only the first request acquires a lock to query the database and repopulate the cache, while other requests wait for the cache to be refreshed.
In a single‑node setup, simple locks (e.g., synchronized, ReentrantLock) suffice; in distributed environments, use distributed locks based on databases, Redis, or Zookeeper.
Cache Avalanche
Cache avalanche occurs when many cached items expire simultaneously, leading to a massive influx of database queries that can overwhelm the database.
Common causes include Redis crashes or a large number of keys expiring at the same time.
Mitigation strategies:
Deploy Redis in high‑availability mode (master‑slave or multi‑master clusters).
Apply rate limiting and degradation, using locks or queues to limit concurrent database reads.
Pre‑warm data by loading likely hot keys into the cache before traffic spikes.
Set staggered expiration times so that keys expire at different moments.
END
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
