How to Prevent Redis Cache Avalanche, Breakdown, and Penetration
This article explains the three major Redis cache issues—cache avalanche, cache breakdown, and cache penetration—how they can overload databases, and provides practical solutions such as high‑availability deployment, appropriate key expiration, local caches, mutex locks, empty‑object caching, request validation, and Bloom filters.
Redis has three major cache problems—cache avalanche, cache breakdown, and cache penetration—that can cause requests to bypass Redis and hit the database directly, increasing load and potentially crashing the system.
1. Cache Avalanche
A cache avalanche occurs when many keys expire simultaneously, causing a sudden surge of database requests that may overwhelm and crash the database.
Solutions:
Deploy Redis in a high‑availability architecture (master‑slave + sentinel, cluster) to avoid full‑stack failures.
Use local caches, Hystrix rate limiting, Nginx IP limiting, etc., to protect MySQL and return default or degraded data when the cache is unavailable.
Set reasonable expiration times for keys, such as randomizing TTLs, to prevent simultaneous expiration.
2. Cache Breakdown
Cache breakdown happens when a hot key expires and a large number of concurrent requests simultaneously query the database, causing a sharp increase in load that can crash MySQL.
Solutions:
Assign appropriate expiration times, possibly making hot keys never expire or renewing them promptly.
Use a mutex lock (implemented with Redis or Zookeeper) so that only the first request rebuilds the cache while others wait.
Pre‑load hot data and refresh the cache periodically to avoid expiration.
3. Cache Penetration
Cache penetration occurs when clients repeatedly request data that does not exist in the cache or the database, causing every request to hit the database directly.
Solutions:
Cache empty objects for non‑existent keys with a TTL, preventing repeated database hits.
Validate request parameters at the front‑end or application layer to filter out obviously invalid or malicious requests.
Employ Bloom filters to quickly determine if a key likely exists in Redis or MySQL, blocking requests for non‑existent keys (acknowledging a false‑positive rate).
Lobster Programming
Sharing insights on technical analysis and exchange, making life better through technology.
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.