Learn Labs
Expiration & Memory

Memory Optimization

Big keys, hot keys, fragmentation, and reading INFO memory

Memory is Redis's scarcest resource and its most common source of incidents. This lesson covers reading where it goes, and the three problems you'll meet in production: big keys, hot keys, and fragmentation.

Reading INFO memory

used_memory_human:1.79Mbytes Redis allocated (dataset + overhead)used_memory_rss_human:9.2Mbytes the OS says the process holdsused_memory_peak_human:3.4Mhigh-water markused_memory_dataset:...the part that's your dataused_memory_overhead:...dictionaries, client buffers, replication backlogmaxmemory_human:256.00Mmem_fragmentation_ratio:5.14rss / used_memorymem_clients_normal:...client query and output buffersmem_replication_backlog:...

MEMORY STATS breaks overhead down further, and MEMORY DOCTOR gives a human-readable diagnosis.

Fragmentation

mem_fragmentation_ratio = RSS ÷ used memory.

RatioMeaning
< 1.0The OS has swapped part of Redis to disk. Latency emergency.
1.0 - 1.5Healthy
> 1.5 on a large datasetFragmentation: freed memory jemalloc can't return to the OS
Very high on a tiny datasetNormal. A 2 MB dataset in a process with fixed allocator overhead.

Fragmentation builds up when values of varying sizes are created and deleted, leaving holes. Redis can defragment live:

CONFIG SET activedefrag yes
CONFIG GET active-defrag-*     # thresholds and CPU limits for the defrag cycle
INFO memory | grep allocator_frag

Big keys

A big key is a single key holding a huge value, such as a 100 MB string or a hash with 5 million fields. They cause:

  • Latency spikes: any O(N) command on it (HGETALL, DEL, SMEMBERS) blocks everyone.
  • Uneven memory in a cluster: one slot, one node, much bigger.
  • Slow replication and failover: the whole value is transferred at once.
  • Network saturation: reading it repeatedly fills the NIC.

Find them without blocking the server (both use SCAN):

redis-cli --user default --pass redis123 --bigkeys     # largest key per type, by element count
redis-cli --user default --pass redis123 --memkeys     # largest keys by memory
MEMORY USAGE app:feed:recent_orders SAMPLES 0          # exact size of one key (samples all elements)

Fixes: split the key (user:42:events:2026-10 per month instead of one forever-growing list), cap it (LTRIM, XTRIM), or move it out of Redis. Delete big keys with UNLINK, not DEL, so the memory is freed in the background.

Hot keys

A hot key is read or written far more than others, for example a global feature flag read on every request, or a celebrity's profile. It isn't a memory problem but a throughput one: one key lives on one thread (and in a cluster, on one node), so it caps out no matter how many nodes you add.

redis-cli --user default --pass redis123 --hotkeys   # requires an LFU eviction policy (this repo uses volatile-lfu)

Fixes, from simplest:

  1. Client-side caching: cache the value in app memory for a few seconds. Redis 6+ supports tracking (CLIENT TRACKING) so the server tells clients when a cached key changes.
  2. Replicate the key: write copies to flag:x:1 … flag:x:8 and read a random one, spreading load across cluster slots.
  3. Read from replicas for data that tolerates slight staleness.

Reducing memory

In rough order of impact:

  1. Set TTLs on everything that's a cache, and check INFO keyspace for expires=.
  2. Remove data that shouldn't be there: --memkeys often finds one forgotten debug key or unbounded list holding half the memory.
  3. Use compact encodings: small hashes instead of many keys, integer IDs in sets (Encodings).
  4. Compress large values client-side.
  5. Use probabilistic structures where exact answers aren't needed (HyperLogLog instead of a set of IDs).
  6. Shorten key names, only at very large key counts.

Swap and Transparent Huge Pages

Two Linux settings matter on real servers:

  • Swap: if any of Redis is swapped out, accessing it takes milliseconds instead of nanoseconds, and the single thread stalls. Set vm.swappiness low and make sure the host has enough RAM.
  • Transparent Huge Pages (THP) make copy-on-write after fork() copy 2 MB pages instead of 4 KB ones, causing latency spikes and memory blowups during saves. Redis logs a warning at startup if THP is enabled. Disable it on Redis hosts.
  • vm.overcommit_memory = 1 lets fork() succeed for background saves even when the host doesn't have enough free memory for a full copy. Redis warns at startup if it isn't set.
docker logs redis 2>&1 | grep -i warning     # look for overcommit and THP warnings

Lab

  1. Read INFO memory and MEMORY DOCTOR.
  2. Create a big key: for i in $(seq 1 200000); do echo "RPUSH app:big $i"; done | docker exec -i redis redis-cli --user app --pass app123 >/dev/null.
  3. Find it with --bigkeys and --memkeys. Check MEMORY USAGE app:big.
  4. Time LRANGE app:big 0 -1 with redis-cli --latency running in another terminal. Watch latency spike.
  5. Delete it with UNLINK app:big.

On this page