Handling Cache Penetration, Breakdown, and Avalanche in Redis with Bloom Filters
This article explains the concepts of cache penetration, cache breakdown, and cache avalanche in Redis, and provides practical mitigation strategies including null caching, distributed locks, random expiration times, and the use of Bloom filters to reduce database load.
Redis is often used as a cache in projects, which can lead to issues such as cache penetration, cache breakdown, and cache avalanche.
Cache Penetration
Cache penetration occurs when requests for non‑existent data bypass the cache and hit the database, potentially overwhelming it. A simple mitigation is to store a null value for missing keys with a short TTL, but this can lead to many null entries.
A better solution is to use a Bloom filter, which can quickly determine whether a key definitely does not exist or may exist, reducing unnecessary database queries.
Cache Breakdown
Cache breakdown happens under high concurrency when many requests query the same key that has expired or become unavailable, causing a surge of database traffic.
Using a distributed lock (e.g., a mutex) allows the first request to fetch from the database while others wait, preventing a thundering herd on the database.
Cache Avalanche
Cache avalanche refers to large‑scale cache failures, such as when many keys expire simultaneously or the cache server crashes, leading to massive database load.
Mitigation includes assigning random expiration times to keys to avoid synchronized expiry and employing cache clustering for high availability.
Bloom Filter Details
A Bloom filter is a probabilistic data structure that can tell you whether an element definitely does not exist or possibly exists.
It works by hashing the element with multiple hash functions and setting the corresponding bits in a bit array. When querying, the bits are checked; if any are 0, the element is definitely absent, otherwise it may be present.
Examples with "AliPay" and "WechatPay" illustrate how bits can be set and how false positives may occur.
Understanding Bloom filters helps in designing efficient cache‑miss handling strategies.
< END >
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.