Skip to content

Redis memory tuning — sizing maxmemory, fragmentation, and overhead

Size maxmemory correctly by accounting for COW, TLS buffers, replication overhead and fragmentation; read INFO memory; tune jemalloc and active defrag.

Redis memory accounting is more nuanced than “data plus a bit of overhead”. The dataset itself is one cost; replication backlog, client output buffers, COW pages during BGSAVE, TLS handshake buffers, and the persistent overhead of jemalloc fragmentation add up to surprising totals. This article walks through INFO memory line by line, gives the sizing math we use in production, and covers the levers — jemalloc tuning, active defrag, and maxmemory budget — that keep memory predictable.

How to verify

# All the relevant memory metrics
redis-cli INFO memory | grep -E '^(used_memory|used_memory_human|used_memory_rss|used_memory_peak|used_memory_overhead|used_memory_dataset|maxmemory|maxmemory_human|mem_fragmentation_ratio|allocator_frag_ratio|allocator_rss_ratio|active_defrag_running)'

# How much is the replication backlog using?
redis-cli INFO replication | grep repl_backlog

# Per-key memory cost
redis-cli MEMORY USAGE somekey

# Memory by data type, top 50 keys
redis-cli --bigkeys

What’s happening

INFO memory reports several numbers that mean different things:

  • used_memory — bytes allocated by Redis through jemalloc, as Redis sees them. Includes the dataset, output buffers, replication backlog, client structs, AOF rewrite buffer, and metadata.
  • used_memory_rss — resident set size from the OS. Includes everything used_memory does plus internal jemalloc fragmentation and COW pages still held from forks.
  • used_memory_overhead — everything that is NOT dataset: client buffers, repl backlog, hash table overhead, AOF buffers.
  • used_memory_dataset — used_memory - used_memory_overhead. The actual data.
  • mem_fragmentation_ratio — used_memory_rss / used_memory. Above ~1.5 indicates real fragmentation; below 1 means the OS has paged Redis memory to disk (very bad).
  • allocator_frag_ratio — jemalloc’s internal view of fragmentation. Cleaner than mem_fragmentation_ratio for diagnosing whether to enable active defrag.

The sizing math: maxmemory is enforced against used_memory, NOT used_memory_rss. That means RSS can exceed maxmemory significantly — fragmentation alone can be 20-30%, and a BGSAVE fork during peak load can double RSS temporarily through COW. A host with 16 GB of RAM should typically set maxmemory 10gb for a comfortable buffer (60-65%). On hosts with strict cgroup limits or kubernetes pod memory requests, that headroom is critical — RSS hitting the cgroup limit gets the daemon OOM-killed.

Replication overhead is repl-backlog-size + (per-replica output buffer). A 512 MB backlog and three replicas with 256 MB buffers each is 1.3 GB of overhead before any data.

The procedure

  1. Read current memory usage:

    redis-cli INFO memory

    Note used_memory_dataset (real data), used_memory_overhead (replication, clients, AOF), and used_memory_rss (OS RSS).

  2. Size maxmemory as a budget. For a 16 GB host:

    • 60-65% for maxmemory (10 GB)
    • 5-10% for fragmentation (1.5 GB)
    • 10-20% for BGSAVE COW headroom (3 GB)
    • 5% for OS, SSH, monitoring agents (0.8 GB)
    maxmemory 10gb
    maxmemory-policy allkeys-lru
  3. Tune jemalloc background work. Active defrag reduces fragmentation by moving keys between memory blocks:

    activedefrag yes
    active-defrag-ignore-bytes 100mb
    active-defrag-threshold-lower 10
    active-defrag-threshold-upper 100
    active-defrag-cycle-min 5
    active-defrag-cycle-max 75

    The thresholds say: do nothing if fragmentation is under 10%; run flat-out at 100% CPU allowance if it exceeds 100%. The CPU cycles 5 to 75 cap how much CPU defrag can take.

  4. Trigger defrag manually when fragmentation has crept up:

    redis-cli DEBUG MALLOC-STATS  # jemalloc internal stats
    redis-cli MEMORY PURGE        # ask jemalloc to release unused arena
  5. Audit big keys that dominate memory:

    redis-cli --bigkeys
    redis-cli --memkeys -i 0.01

    A single 500 MB list or hash is usually a design smell — split it into smaller keys.

  6. Watch fragmentation over time with Prometheus:

    redis_mem_fragmentation_ratio{instance="redis-01:6379"}

    Alert when it exceeds 1.5 for 30 minutes.

  7. Force a sample for sizing analysis:

    redis-cli MEMORY DOCTOR
    redis-cli MEMORY STATS

    MEMORY DOCTOR prints human-readable advice based on the current state.

Common pitfalls

  • Setting maxmemory to the full physical RAM of the host leaves no room for COW, fragmentation, or other processes. RSS will overshoot and the OOM killer takes Redis down.
  • A growing used_memory_overhead with stable used_memory_dataset usually means client output buffers are buffering — investigate slow clients or replicas.
  • mem_fragmentation_ratio below 1.0 means the kernel has paged Redis memory to swap. Latency goes through the floor. Disable swap on Redis hosts or set vm.swappiness = 1.
  • Active defrag uses CPU — under load on a CPU-bound master it makes latency worse. Tune the upper cycle bound (active-defrag-cycle-max) down or schedule defrag during off-peak.
  • A workload of many small keys with 5-character names has shockingly high overhead — pointer plus hash table metadata for each key. The same data in fewer larger Hashes can use 50% less memory. Profile with MEMORY USAGE.

In the engagements we run, every Redis instance comes with maxmemory documented as a percentage of host RAM, fragmentation alarms wired to Prometheus, and a quarterly MEMORY DOCTOR review on every shard. We operate that under Managed Operations — and the partner to memory sizing is eviction policy choice, which decides what happens when memory does fill.