Skip to content

Lesson 5 of 9 · 24m

Memory Fragmentation, Large Keys, and Practical Audits

Diagnose where Redis memory went using INFO, MEMORY STATS, object inspection, scan-based audits, and careful deletion strategies.

Redis memory incidents are confusing because the numbers do not line up the way application developers expect. Your objects may total 2 GB while Redis uses 4 GB. Deleting keys may not return memory to the operating system. A restart "fixes" fragmentation while erasing the evidence. You need an audit method before you need a restart.

Learn the memory vocabulary

redis-cli info memory
redis-cli memory stats
redis-cli memory doctor

Important fields include:

used_memory             bytes Redis allocated through its allocator
used_memory_rss         resident memory the operating system reports
mem_fragmentation_ratio used_memory_rss / used_memory
allocator_allocated     allocator's view of allocated application memory

Fragmentation is not automatically bad. A ratio slightly above 1 is normal. A very high ratio means retained pages contain enough free holes that RSS stays large. A ratio below 1 can indicate swapping or allocator accounting oddities — usually worse news, not better news.

Audit keys without blocking production

Never inventory production with KEYS *. Scan incrementally:

redis-cli --scan --pattern 'session:*' | head
redis-cli --bigkeys
redis-cli --memkeys --memkeys-samples 1000

--bigkeys and --memkeys still scan the keyspace, so run them during lower traffic and understand that they sample aggregate statistics differently. In application code, use SCAN with a small count and preserve the cursor.

Inspect individual objects honestly

TYPE leaderboard:weekly
STRLEN payload:job-901
HLEN session:01JRYC
SCARD entitlement:team_77
ZCARD jobs:delayed
MEMORY USAGE session:01JRYC
OBJECT ENCODING session:01JRYC

MEMORY USAGE includes key and value overhead but not every allocator nuance. It is excellent for comparisons, imperfect for accounting. OBJECT ENCODING matters because small hashes and lists may use compact encodings until thresholds push them into more expensive structures.

Deleting safely is an operation

Deleting one giant key synchronously can stall Redis. Use UNLINK rather than DEL for large objects:

UNLINK stream:legacy-events hash:analytics-rollup

For huge collections, shrink incrementally before unlinking: SPOP batches, ZREMRANGEBYSCORE, HSCAN plus targeted HDEL, or stream trimming. The goal is to avoid copying or freeing hundreds of megabytes in one event-loop turn.

Restart is not diagnosis

A restart can reduce fragmentation, but it also changes persistence state, replication relationships, client connections, and cache warmth. Capture INFO, MEMORY STATS, key samples, and slowlog first. Otherwise you have traded an explainable capacity problem for a clean dashboard and no learning.

Field notes

  • Many small keys have disproportionate per-key overhead. A million "tiny" keys are not tiny.
  • Client output buffers can consume memory when slow consumers subscribe or request huge responses.
  • Replication and persistence both add memory pressure beyond data size; capacity planning must include fork behavior.
  • Transparent huge pages can worsen Redis memory behavior and latency. Verify host settings instead of guessing.

Was this lesson helpful?

Sign in to rate