← back to stream

Redis memory management

#backend#redis

Redis lives in RAM, so memory strategy determines behaviour under load. You set maxmemory (hard cap) and maxmemory-policy (what to do when full). The policies that matter in practice: noeviction — refuse new writes (use when data is precious and you'd rather break writes than lose old keys); allkeys-lru — evict the least-recently-used key across the whole dataset (the standard choice for a pure cache); volatile-lru — same but only considers keys with TTL (use when mixing cache and persistent data in the same instance); allkeys-lfu — Least-Frequently-Used, better than LRU for real access patterns (hot keys stay). Optimising memory is mostly about picking the right data type — a Hash with many small fields uses less RAM than many individual String keys, thanks to internal "ziplist" encoding. MEMORY USAGE key tells you what each key costs; MEMORY DOCTOR diagnoses common problems. If you ever see OOM errors, the first question is: is this actually a cache, and if so, why isn't maxmemory-policy set?