Database / ValKey Interview questions
Explain the internal working of Valkey's LRU and LFU eviction algorithms?
Tracking exact least-recently-used order for every key would be too expensive at scale, so Valkey approximates it: each key carries a compact last-access timestamp field, and on eviction the server samples a small random set of keys, controlled by maxmemory-samples (default 5), and evicts whichever one in that sample was accessed longest ago, repeating as needed.
LFU mode instead tracks a probabilistic, logarithmic access-frequency counter per key that increments with decreasing probability as it grows and decays gradually over time, so an old burst of activity doesn't keep a key "hot" forever; eviction then targets the lowest-frequency key among a sampled set.
LFU tends to suit workloads with a genuinely persistent hot subset of keys better than LRU, which is more sensitive to short-term recency.
More Related questions...