Caching and Performance Engineering · Caching Fundamentals
Redis in Practice
Data structures, eviction, persistence and pitfalls of running Redis as a cache.
Redis is an in-memory data store with rich structures: strings (cached values, counters), hashes (objects), sets/sorted sets (leaderboards, rate limits), lists/streams (queues). As a cache, set maxmemory and an eviction policy (allkeys-lru or allkeys-lfu), always use TTLs, avoid huge values and KEYS * in production, watch hot keys, and decide whether persistence (RDB/AOF) is needed. Redis Cluster shards keys across nodes by hash slot.
# redis.conf (cache use case)
maxmemory 4gb
maxmemory-policy allkeys-lfu
# commands
SET product:v2:42 '{"name":"Mug","price":1200}' EX 300
INCR rl:user:42:2026101009 # counter for rate limiting
ZADD leaderboard 1500 player:7 # sorted set
SCAN 0 MATCH session:* COUNT 1000 # instead of KEYS *
Common pitfalls
- No maxmemory → Redis grows until the OS kills it.
KEYS *blocking the single-threaded server.- Multi-key operations across cluster slots (use hash tags
{user:42}).
Key points
- Pick the right data structure
- maxmemory + eviction policy + TTLs
- Avoid blocking commands; watch hot keys
Further reading: Redis: Key eviction
Practice questions
What's the difference between allkeys-lru and allkeys-lfu?
Answer
LRU evicts keys not used recently; LFU evicts keys used least frequently, which better protects consistently popular keys from one-off scans.
Why is KEYS * dangerous in production?
Answer
It scans the entire keyspace in one blocking operation, stalling all other clients on the single-threaded server; use incremental SCAN.
Open in the app to mark lessons complete, take notes and track your progress.