Sustaining Hot Dataset Caching in Redis for Large-Scale MySQL Under Heavy Load

When managing databases containing tens of millions of records, routing all incoming queries directly to the storage layer can rapidly exhaust system resources. Deploying an in-memory caching layer intercepts repetitive requests and shields the primary database from collapse. The central engineering challenge lies in guaranteeing that frequently accessed records remain resident in memory despite strict capacity constraints.

Memory management in caching systems typically relies on two foundational eviction strategies: Least Recently Used (LRU) and Least Frequently Used (LFU). LRU tracks temporal recency, discarding entries that have not been referenced for longest duration. LFU tracks request volume, maintaining a usage counter alongside a timestamp, and removes keys with the lowest hit counts.

Under LRU mechanics, a bounded queue promotes recently accessed items to the front. When capacity is reached and a new record arrives, the item at the rear of the queue is purged. Consider a sequence where items A, B, C, and D occupy the cache. Accessing item C shifts it to the head. If item E arrives next, the cache must evict an existing key; LRU removes the least recently touched item, regardless of its historical popularity.

LFU alters this behavior by prioritizing frequency over recency. Each cached key stores a hit_counter and a last_touched_ts. When memory limits are approached, the algorithm scans for the minimum hit_counter. If multiple keys share identical low counts, the tie is broken by removing the one with the oldest last_touched_ts. This ensures that heavily requested keys survive temporary access gaps.

Redis exposes a range of built-in eviction directives to handle memory saturation:

Policy Directive Operational Behavior
noeviction Blocks write operations once the memory limit is reached. Serves as the default configuration.
allkeys-random Removes arbitrary keys from the entire keyspace without evaluating expiration or access patterns.
volatile-random Removes arbitrary keys exclusively from those with an assigned time-to-live (TTL).
volatile-ttl Evicts keys approaching their expiration timestamp first, prioritizing the nearest deadline.
allkeys-lru Applies the LRU algorithm across every stored key.
allkeys-lfu Applies the LFU algorithm across every stored key.
volatile-lru Applies LRU only to keys configured with a TTL.
volatile-lfu Applies LFU only to keys configured with a TTL.

In high-throughput environments dominated by repetitive access patterns, LRU can produce counterproductive outcomes. A critical dataset might experience thousands of hits followed by a brief idle period. If the cache fills during this window, LRU may purge the high-value record simply because another key was touched milliseconds earlier. Subsequent traffic spikes targeting the evicted record will cascade into the relational database, creating a severe bottleneck.

LFU mitigates this vulnerability by preserving records based on cumulative demand rather than temporal proximity. It naturally filters out cold data while retaining frequently queried entries, making it the optimal strategy for sustaining hot datasets under concurrent load. Selecting between allkeys-lfu and volatile-lfu depends on whether the application architecture already enforces expiration policies.

Configuration adjustments can be applied through runtime commands or persistent configuraton files. The following demonstrates a structured deployment approach:

# cache-instance.conf
# Set the memory boundary for the daemon process
maxmemory 2gb

# Enable frequency-based eviction across the global keyspace
maxmemory-policy allkeys-lfu

For dynamic environments, administrators can modify the policy without restarting the service:

# Apply frequency tracking only to keys with expiration windows
redis-cli CONFIG SET maxmemory-policy volatile-lfu
redis-cli CONFIG SET maxmemory 1610612736

While frequency-driven eviction dramatically improves retention accuracy, edge cases emerge when multiple keys accumulate identical hit counts. The system resolves ties by falling back to timestamp evaluation. In rare instances where a freshly evicted key immediately experiences a traffic surge, the database layer should implement request serialization or lightweight advisory locks to absorb the transient load and maintain data integrity.

Tags: redis-eviction lfu-algorithm mysql-caching high-concurrency-architecture cache-policy

Posted on Sun, 02 Aug 2026 16:34:44 +0000 by .Stealth