Memory & Eviction
Always set an explicit maxmemory ceiling in valkey.conf so Valkey cannot exhaust host RAM and trigger the Linux OOM killer.
Configuring maxmemory and eviction policy#
maxmemory 4gb
maxmemory-policy allkeys-lruCommon eviction policies#
| Policy | Behavior when maxmemory is reached | Best for |
|---|---|---|
noeviction (default) | Returns errors on write commands; reads continue | Persistent data stores where dropping keys is unacceptable |
allkeys-lru | Evicts least-recently-used keys across the entire dataset | General-purpose caching |
allkeys-lfu | Evicts least-frequently-used keys | Skewed cache workloads with hot keys |
volatile-lru / volatile-ttl | Evicts only keys that have an EXPIRE TTL set | Mixed cache + persistent keys in the same instance |
Inspecting memory & fragmentation#
valkey-cli INFO memory
Key fields to watch:
used_memory_human— Memory allocated by Valkey for data.used_memory_rss_human— Resident memory seen by the OS.mem_fragmentation_ratio—used_memory_rss / used_memory. Values around1.0–1.5are normal; consistently> 1.5indicates fragmentation (consideractivedefrag yes);< 1.0means Valkey is swapping to disk.
Finding large keys safely#
Scan for big keys without blocking the server:
valkey-cli --bigkeys -i 0.1
Inspect exact memory usage of a specific key:
valkey-cli MEMORY USAGE my:large:key