Handling Redis Cache Issues: Breakdown, Penetration, and Avalanche Strategies
This article explains how Redis cache can suffer from key expiration, eviction, and high‑concurrency problems such as cache breakdown, penetration, and avalanche, and presents practical lock‑based, filter‑based, and timing‑aware solutions to maintain backend stability and performance.
Introduction: The article explains that I/O bottlenecks lead to using caches like Redis to store hot data in memory, reducing database load, but issues such as cache breakdown, penetration, and avalanche can occur when keys expire under high concurrency.
Problem causes: (1) Key expiration; (2) Cache eviction due to limited memory. Expired keys cause a surge of requests to the database, potentially crashing it; eviction can remove rarely accessed keys before an event.
Handling cache breakdown: Use a lock when a key is missing. The process is (1) request reaches Redis, discovers an expired key and checks for a lock; (2) set a lock with setnx() (not set() ) if none exists; (3) acquire the lock, fetch data from the database, return the result, and release the lock. Additional measures include setting an expiration on the lock and using a monitoring thread to extend the lock if the data‑fetching thread hangs.
Handling cache penetration: Since many requests may query non‑existent data, add a filtering layer such as a Bloom filter, enhanced Bloom filter, or Cuckoo filter, and optionally validate request parameters (e.g., reject negative IDs) to prevent unnecessary database hits.
Handling cache avalanche: Random expiration is not always appropriate; first determine whether key expiration is time‑point related. If not, randomize expiration times; if it is (e.g., a bank changes an interest rate at a specific moment), use a strong dependency strategy: update all related hot keys in the background and delay incoming requests briefly to spread the load.
Conclusion: The author also shares a set of interview questions from major tech companies and invites readers to join a community for further discussion.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.