Redis Tutorial 0/42 lessons ~6 min read Lesson 38

    Memory Optimization

    Redis memory is your primary bill and failure mode — maxmemory triggers eviction or OOM.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Redis memory is your primary bill and failure mode — maxmemory triggers eviction or OOM. Right-sizing types (hash not JSON blob), eviction policy choice, and active defrag can cut ElastiCache costs 20–30% without code rewrites.

    Run redis-cli --bigkeys and MEMORY DOCTOR monthly. Fragmentation ratio above 1.5 means RSS exceeds logical memory — activedefrag or restart during maintenance.

    Every key without TTL is a future incident.

    Understanding the topic

    Key concepts

    • used_memory vs used_memory_rss — fragmentation.
    • maxmemory + maxmemory-policy.
    • allkeys-lru vs volatile-lru vs noeviction.
    • Encodings — intset, listpack, embstr savings.
    • activedefrag — online defragmentation.
    • MEMORY USAGE key — bytes per key.

    Step-by-step explanation

    1. Allocator (jemalloc) tracks logical usage.
    2. Updates cause holes — RSS grows.
    3. maxmemory hit triggers eviction algorithm.
    4. noeviction returns errors on write.
    5. Defrag relocates keys compacting memory.

    Syntax reference

    Common commands

    • --bigkeys off-peak only.
    • noeviction for queue workloads needing no silent loss.
    • allkeys-lru when most keys should be evictable.
    bash
    INFO memory
    MEMORY DOCTOR
    MEMORY USAGE session:abc
    redis-cli --bigkeys -i 0.1
    CONFIG SET maxmemory 4gb
    CONFIG SET maxmemory-policy allkeys-lru

    Informative example

    Find top memory consumers and compare hash vs JSON string:

    bash
    redis-cli --bigkeys
    # Sample output shows biggest string/hash/zset
    MEMORY USAGE user:42:string_json
    MEMORY USAGE user:42:hash_fields
    # Hash often smaller when multiple fields updated

    Migrate hot JSON keys to hash field-by-field in background job. Monitor used_memory drop.

    Real-world use

    Real-world use cases

    • Reduce AWS ElastiCache bill.
    • Prevent OOM before product launch.
    • Choose HLL over SET for UV.
    • Eviction tuning for pure cache tier.
    • Fragmentation fix after big delete.

    Best practices

    • Set maxmemory 75–80% RAM on self-hosted.
    • TTL every ephemeral key.
    • allkeys-lru for cache; noeviction for job queues.
    • Enable activedefrag when supported.
    • Right type and encoding awareness.
    • Alert at 80% used_memory.

    Common mistakes

    • noeviction + no maxmemory on cache cluster.
    • Giant keys never split or trimmed.
    • Ignoring fragmentation until OOM kill.
    • volatile-lru but keys lack TTL — never evict.

    Advanced interview questions

    Q1Beginnermaxmemory purpose?
    Cap Redis RAM; trigger eviction policy.
    Q2Beginnerallkeys-lru vs volatile-lru?
    allkeys evicts any key; volatile only keys with TTL set.
    Q3IntermediateMemory fragmentation?
    RSS > used_memory from allocator holes; defrag or restart.
    Q4IntermediateReduce session memory?
    Store IDs not blobs; hash fields; TTL; trim attributes.
    Q5AdvancedEviction policy for job queue?
    noeviction — errors on full better than dropping jobs silently.

    Summary

    Memory = cost and reliability boundary.

    Ready to mark this lesson complete?Track your journey across the entire course.