A Close Look at Common Caching Strategies UPDATING 

The Three Cache Brothers Cache Penetration What it is: a flood of requests querying data that doesn’t exist in the database. The cache has nothing, so it keeps hammering the database. Prevention: Cache null values Even when the database finds nothing, put an empty marker in the cache (with a short TTL, e.g. 2 minutes; only after that TTL expires can the database be queried again) The next time the same id comes in, it’s blocked directly Pros: effectively intercepts large numbers of penetration requests Cons: A malicious attack with many different non-existent keys fills the cache with useless data and wastes memory It delays data consistency; new data has to wait for the cache to expire Bloom filter (recommended) At startup, put all legitimate ids into the Bloom filter If the Bloom filter says no, it really doesn’t exist Pros: No high-memory-usage risk No data-consistency risk Cons: There’s a tiny false-positive rate Insertions and deletions need maintenance; when data updates, the Bloom filter must be updated in sync Rate limiting and validation at the interface layer Intercept at the API gateway or Controller layer Parameter validation, rate limiting, and degradation Mutex-based rebuild A mutex mainly solves cache breakdown (a hot key expiring), but in the cache-empty-object scenario, if there are many concurrent requests for a non-existent key, a lock can be used Each access lets one thread query the DB while the others wait Big companies’ practice ...

2026-05-05 10:46:44 PM · 3 min

A Close Study of Redis's LFU Memory Eviction Policy

Redis’s LFU (Least Frequently Used) eviction policy is an “upgraded” approximate algorithm built on top of LRU. It reuses the same 24-bit lru field in the object header and, through clever encoding and logarithmic counting, achieves an approximate tracking of access frequency at a tiny memory cost. Below we examine it point by point: from the storage structure, to counter increment/decrement, to the eviction decision, to parameter configuration. 1. The bit layout of the 24-bit field Every Redis object has an lru attribute (24 bits). In LFU mode it no longer holds a second-level timestamp but is split into two parts: ...

2024-07-19 01:10:54 PM · 6 min