Skip to the page

Redis and caching · senior

Redis memory, eviction and slow commands

Redis keeps data in memory and runs commands one at a time. You must know what happens at maxmemory and which commands block everyone.

The notes

What you must know

  • At maxmemory Redis applies the eviction policy. With noeviction, writes fail with 'OOM command not allowed'.
  • volatile-lru evicts only keys that have a TTL. If only sessions have TTLs, sessions are evicted (users are logged out). If no key has a TTL, a volatile policy behaves like noeviction.
  • allkeys-lru and allkeys-lfu evict any key. They suit a pure cache, not sessions.
  • Keep sessions and cache on separate instances: sessions with noeviction and persistence, the cache with an allkeys policy. Put a TTL on every cache key.
  • Look with INFO memory, INFO stats (evicted_keys), INFO keyspace, redis-cli --bigkeys and --memkeys, and MEMORY USAGE.
  • Commands run one at a time on the main thread, so one slow command makes every client wait. Find slow work with SLOWLOG GET, LATENCY DOCTOR and INFO commandstats.
  • KEYS walks every key: use SCAN with COUNT. Deleting a huge key frees memory on the main thread: use UNLINK. Background saves fork the process, and on a large instance with transparent huge pages the fork can stall Redis.
  • Block dangerous commands with ACLs for application users.

The red pen

Common traps: mistake → fix

  • Mistake: Switch to allkeys-lru on the shared instance

    Fix: it now evicts sessions too.

  • Mistake: Redis is in memory, so it cannot be slow

    Fix: one KEYS or one big DEL can freeze it for seconds.

  • Mistake: FLUSHALL fixes it

    Fix: it deletes everything and causes a stampede.

Easy to hard

Questions interviewers ask, easy to hard

  1. Users are logged out and Redis returns OOM errors. What do you check?
  2. What does volatile-lru do?
  3. Redis latency jumps every night. Where do you look?
  4. Why separate sessions from the cache?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview