Redis Cache Issue

Cache Penetration: The requested data doesn’t exist at all — it’s not in the cache, and it’s not in the database either. An attacker can make up a non-existent ID and hammer it with requests, and every single one goes straight through to the database.

Solution: Cache null values: if the database confirms the data truly doesn’t exist, still write an empty placeholder into the cache with a short TTL (e.g., 2–5 minutes). This stops the same non-existent key from repeatedly passing through to the database.

Cache Breakdown: The moment a hot key expires, a flood of requests pours in at once and all of them hit the database. For example, the cache for a flash-sale item happens to expire, and tens of thousands of requests slam into the database all at once.

Solution: Never expire hot data, no expiration time is set for hot data

Cache Avalanche: A large batch of keys expires at the same time, or Redis goes down entirely, and all requests rush to the database. Breakdown involves one key; an avalanche involves a whole swath of keys.

Solution: The fix is to add a random value to the expiration time. For example, use a base TTL of 1 hour plus a random 0–10 minutes, so expirations are spread out: