allkeys-lru vs allkeys-lfu for high-churn workloads
0 reputation · 01 Apr 2022, 02:38 UTC
0 reputation · 01 Apr 2022, 02:38 UTC
When Redis reaches its maxmemory limit, the choice of eviction policy determines which data is purged to accommodate new writes. For workloads where memory is the primary binding constraint, two primary documented approaches exist for the entire keyspace: allkeys-lru and allkeys-lfu.
The allkeys-lru policy is designed for patterns where the most recently accessed data is the most likely to be reused. Conversely, allkeys-lfu (introduced in Redis 4.0) targets workloads with a stable "hot set" that must be protected from being flushed by a sudden burst of one-off keys that would otherwise appear "recent" to an LRU algorithm.
Redis implements these policies approximately by sampling a subset of keys defined by maxmemory-samples. Increasing this value improves the accuracy of the eviction candidate but increases CPU utilization during write operations under memory pressure.
Given a workload with a mix of long-term hot keys and high-frequency short-lived keys:
lfu-decay-time be calibrated to prevent legacy hot keys from blocking new high-frequency data?maxmemory-samples outweigh the gain in cache hit ratio?35775 reputation · 01 Apr 2022, 10:03 UTC
For a workload that mixes long‑term hot keys with a burst of short‑lived, high‑frequency keys, the eviction policy that best protects the hot set while admitting the new traffic is allkeys‑lfu (available from Redis 4.0).
The LFU counter ages according to lfu‑decay‑time. To let legacy hot keys yield to new high‑frequency entries, set the decay time so that a key’s counter drops significantly during its typical idle period.
lfu‑decay‑time ≈ ½ × that idle duration. For example, if hot keys are idle for about 2 minutes, start with 60 seconds.INFO fields keyspace_hits, keyspace_misses and evicted_keys. If the hit ratio for new traffic rises without a noticeable increase in misses for the hot set, the decay time is appropriate.Missing diagnostic detail: What is the typical idle period of your legacy hot keys? Knowing this lets you pick a decay time that matches the workload; without it you may over‑ or under‑age the counters.
Redis approximates LRU/LFU by sampling maxmemory‑samples keys on each write that triggers eviction. Accuracy improves with more samples, but each extra sample adds CPU work.
maxmemory‑samples 10.redis‑benchmark -t set,get) that reproduces your churn rate and record the hit ratio and CPU usage (used_cpu_sys + used_cpu_user from INFO).These steps give you a concrete, repeatable way to tune both parameters without guesswork.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.