Redis Memory Management and Eviction Policies

A
Author
17 Apr, 2026 1 min read 409 views

Redis lives in RAM, so memory is the resource you'll manage most. Without limits, a growing cache can exhaust the server and get Redis killed.

Set a limit

# redis.conf
maxmemory 2gb
maxmemory-policy allkeys-lru

Leave headroom on the machine for the OS, client buffers and background saves (which fork the process).

Eviction policies

  • noeviction - reject writes when full. Right when Redis holds data you can't lose (queues, sessions).
  • allkeys-lru - evict the least recently used keys. The usual choice for a pure cache.
  • allkeys-lfu - evict the least frequently used; better when some keys are consistently popular.
  • volatile-lru / volatile-ttl - only evict keys that have a TTL, protecting keys without one.

If one Redis instance mixes cache data and must-keep data, use volatile-* and set TTLs on all cache keys, or better, split them into separate instances.

Find what's using memory

redis-cli INFO memory          # used_memory_human, fragmentation ratio
redis-cli --bigkeys            # largest key per type (samples the keyspace safely)
redis-cli --memkeys            # keys by memory usage
redis-cli MEMORY USAGE user:42

Save memory

  • Always set TTLs on cache entries.
  • Prefer a hash per object (HSET user:42 ...) over many separate string keys; small hashes are stored very compactly.
  • Compress large values (JSON blobs) in the application.
  • Avoid unbounded lists and sets; trim them (LTRIM, XTRIM).
  • Keep key names reasonably short at very large key counts.

Big keys and slow commands

A single key holding millions of elements makes commands like DEL or HGETALL block Redis. Use UNLINK (non-blocking delete), iterate with SCAN/HSCAN instead of KEYS, and check SLOWLOG GET regularly.

Was this article helpful?

Comments

No comments yet. Have a question or a tip? Be the first to share it.

Leave a comment

Comments are reviewed before they appear.