This episode covers how to keep Redis performant: analyzing memory usage with INFO memory and MEMORY USAGE, understanding fragmentation with used_memory vs used_memory_rss, Redis's internal encodings, and latency analysis with SLOWLOG and LATENCY.

Redis stores everything in RAM — which means memory is its most valuable resource, and latency is the core promise that must be kept. Episode 17 equips you with analysis and tuning: understanding memory numbers, optimizing data representation, and finding the sources of latency.
In production, the first questions when something goes wrong are always: "how much memory is used, and what is slowest?" The two tool sets in this episode — INFO/MEMORY and SLOWLOG/LATENCY — answer both. Let's start with memory.
redis-cli INFO memoryredis-cli INFO memory shows all memory metrics. Two numbers whose difference is most important to understand:
used_memory: the memory Redis allocated for data (the logical size of the data).used_memory_rss: the physical memory actually held from the OS.If used_memory_rss is much larger than used_memory, fragmentation has occurred — the OS gives more memory than the data needs. If the ratio is high (mem_fragmentation_ratio > 1.5), consider CONFIG SET activedefrag yes or a scheduled restart.
redis-cli MEMORY USAGE user:1
redis-cli MEMORY DOCTORMEMORY USAGE user:1 computes the memory used by a single key — the tool for hunting memory-hungry keys. MEMORY DOCTOR analyzes memory health and gives recommendations: high-fragmentation conditions, many un-cleaned expired keys, or inefficient storage.
Without maxmemory, Redis can consume the entire server's RAM. Set a limit and choose an eviction policy (episode 11):
maxmemory 4gb
maxmemory-policy allkeys-lfu
maxmemory-samples 10maxmemory 4gb caps usage, allkeys-lfu chooses to evict cold data, and maxmemory-samples controls LFU/LRU sampling accuracy — higher values are more accurate but more CPU-expensive.
This is Redis's efficiency secret: large data structures are stored in a compact encoding automatically while small, then switch to the full encoding as they grow:
| Encoding | Structure | When Used |
|---|---|---|
| listpack / ziplist | List, Hash, ZSet | Small & few elements |
| intset | Set | All members are integers |
| hashtable | Set, Hash | Large size |
| embstr / raw | String | Based on length |
redis-cli OBJECT ENCODING small_listOBJECT ENCODING small_list shows a key's active encoding. Letting Redis choose automatically is almost always right — you don't need to intervene unless analysis shows waste, and intervention is usually through list-max-listpack-size and friends in redis.conf.
redis-cli --latencyredis-cli --latency sends PING continuously and shows latency statistics — a quick reference for whether the network and server are healthy. To find slow commands, use SLOWLOG:
redis-cli SLOWLOG GET 10
redis-cli CONFIG GET slowlog-log-slower-thanSLOWLOG GET 10 shows the last 10 slow commands with their duration and arguments. slowlog-log-slower-than (default 10000 microseconds) sets the threshold for recording slow commands. Commands that appear repeatedly in SLOWLOG are prime suspects for latency problems.
Redis has a latency monitoring system to detect systematic spikes:
redis-cli LATENCY LATEST
redis-cli LATENCY HISTORY command
redis-cli LATENCY RESETLATENCY LATEST shows the latest latency events per type. LATENCY HISTORY gives history per type — for example fork, command, aof-write. LATENCY RESET clears old data. Frequent fork events indicate BGSAVE/AOF rewrite happening too often.
Remember the single-threaded model (episode 2): expensive commands block everything. The prime suspects:
KEYS * on a large keyspace — always replace with SCAN.SMEMBERS on a giant set — replace with SSCAN or SINTERCARD.LRANGE 0 -1 / HGETALL on large structures — scan incrementally.The discipline of using SCAN variants and limiting results will avoid almost all the latency spikes that hit production Redis.
Episode 17 equipped you with memory management and performance tuning: INFO memory and MEMORY USAGE for analysis, understanding used_memory vs used_memory_rss fragmentation, internal encodings that Redis chooses automatically, and SLOWLOG and LATENCY to find the sources of latency.
Key takeaways:
used_memory vs used_memory_rss: a high ratio signals memory fragmentation.MEMORY USAGE finds memory-hungry keys; MEMORY DOCTOR gives diagnostic advice.maxmemory + the right eviction policy from the start.redis-cli --latency and SLOWLOG GET are the first latency debugging tools.LATENCY LATEST catches systematic spikes like fork.KEYS, giant SMEMBERS, and LRANGE 0 -1 on large structures.In the next episode, episode 18, we cover Monitoring & Observability — watching Redis comprehensively. You'll learn to read INFO, warnings about MONITOR, mapping Redis to Prometheus with redis_exporter, visualization in Grafana, and building proper alerting. Let's continue!