Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 42 ยท Caching and Application Patterns

Memory Limits and Eviction

Learn how Redis maxmemory works, what each eviction policy does (LRU, LFU, volatile, allkeys, noeviction), approximated LRU, MEMORY USAGE and encodings.

Redis keeps its data in RAM, and RAM is finite. If you never tell Redis how much it may use, a growing dataset eventually fills the machine and the operating system kills the process or starts swapping, which is worse. The maxmemory setting gives Redis a ceiling, and the eviction policy says what to do when the ceiling is reached: refuse writes, or throw old keys away to make room.

For a cache, eviction is not a failure but the normal way the system works: the cache holds the most useful data that fits. For a primary datastore it is the opposite: silently losing keys would be a disaster, so you choose a policy that refuses writes instead. This lesson explains the policies, how Redis chooses victims cheaply, how to measure memory, and why the shape of your data (its encoding) matters.

Real Redis cannot run on this page, so redis-cli and redis.conf snippets are read-only. The runnable examples are JavaScript simulators of the algorithms. They count capacity in keys, whereas real Redis counts bytes, so treat them as models of the logic, not of the byte-exact numbers.

Setting the limit

TEXT
# redis.conf (or CONFIG SET at runtime)
maxmemory 2gb
maxmemory-policy allkeys-lru
maxmemory-samples 5
TEXT
127.0.0.1:6379> CONFIG SET maxmemory 100mb
OK
127.0.0.1:6379> CONFIG SET maxmemory-policy allkeys-lfu
OK
127.0.0.1:6379> INFO memory
# used_memory:1043512
# used_memory_human:1019.05K
# maxmemory:104857600
# maxmemory_policy:allkeys-lfu

On 64-bit systems maxmemory 0 means no limit (the default). Each time a client sends a command that could add data, Redis checks whether it is over the limit and, if so, evicts keys until it fits again. Memory used by data counts; leave generous headroom for everything outside it: replication and client buffers, the fork used by snapshots (copy-on-write pages can add up when many keys change during a save, see persistence) and allocator fragmentation. A common rule is to set maxmemory to well under the machine's RAM, often around 60 to 75 percent, depending on the workload.

Pick the candidates

allkeys-* Any key can be evicted.
volatile-* Only keys that have a TTL.
noeviction Refuse writes instead.

volatile-* with no expiring keys behaves like noeviction.

1 / 2

The eight policies

Policy Evicts
noeviction Nothing. Writes that need memory fail with an OOM error; reads still work. This is the default.
allkeys-lru The least recently used key among all keys.
allkeys-lfu The least frequently used key among all keys.
allkeys-random A random key.
volatile-lru The least recently used key among keys that have an expiry.
volatile-lfu The least frequently used key among keys with an expiry.
volatile-random A random key among keys with an expiry.
volatile-ttl The key with the shortest remaining time to live.

Which one to pick:

  • A pure cache: allkeys-lru is the usual choice; allkeys-lfu is better when a few keys are much more popular than the rest and you do not want a one-time scan to flush them.
  • Cache and durable data in the same instance: volatile-*, so only keys you gave a TTL can be evicted. Keys without a TTL are safe, but you must give every cache key a TTL or the cache cannot make room. Mixing roles in one instance is usually worse than two separate instances.
  • Queues, sessions you cannot lose, primary data: noeviction, and monitor memory so you can add capacity before writes start failing.

The simulator below keeps four keys (capacity 4) and then tries to add a fifth under each policy. Keys a and b have expiry times, c and d do not. The access history is chosen so the policies disagree: a was used often but not lately.

JS Which key does each policy evict?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

allkeys-lru throws out a (used a lot, but longest ago), while allkeys-lfu keeps a and sacrifices a key with few hits. The volatile-* policies never touch c and d. And when no key has a TTL, volatile-lru has nothing to evict and fails like noeviction, a classic production surprise.

Policies Redis does not have

General caching theory lists other policies. FIFO evicts the oldest inserted entry regardless of use; it is trivial to implement but throws out popular data just because it is old. Random eviction picks any key (Redis offers allkeys-random and volatile-random). MRU evicts the most recently used entry, which suits scans that never revisit what they just read. TTL-based eviction removes whatever expires soonest (Redis has volatile-ttl). Redis ships only the random, TTL, LRU and LFU families because they can be approximated cheaply with sampling, as described next. In a distributed cache the keys are also partitioned: Redis Cluster uses hash-based partitioning with 16384 slots (see Cluster), while products such as Hazelcast or Apache Ignite use their own partitioning and are outside this tutorial.

Approximated LRU

Exact LRU needs every key in a linked list ordered by recent use, which costs extra memory per key and work on every read. Redis avoids that. Each key stores a small last-access clock (24 bits, about one-second resolution). To evict, Redis samples a few random keys (maxmemory-samples, default 5), picks the least recently used of the sample, and keeps a small pool of the best candidates seen so far across attempts to improve the choice. It is an approximation of LRU that gets close for little cost. Raising the sample size makes it closer to true LRU at the price of more CPU per eviction.

The next simulator runs the same skewed workload (a few popular keys, a long tail of rare ones) against a cache of 100 slots, 1000 possible keys, and compares sample sizes with a true LRU scan.

JS Approximated LRU versus exact LRU

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Sample size 1 is random eviction. With 5 samples the hit rate is already within a fraction of a point of the exact scan on this workload (the exact numbers are specific to this seed), which is why Redis can afford the approximation.

LFU: counting instead of timing

LRU asks "when was it last used?" LFU asks "how often is it used?", which protects a popular key from a burst of one-off reads. A real counter per key would need a lot of bits, so Redis stores an 8-bit logarithmic counter (maximum 255) plus a minute-resolution timestamp. A hit raises the counter only with a probability that shrinks as the counter grows: 1 / ((counter - 5) * lfu-log-factor + 1). New keys start at 5 so they are not evicted instantly. The counter also decays by one for every lfu-decay-time minutes (default 1) without access, so an old favourite eventually loses its protection. The default lfu-log-factor is 10; a bigger factor makes the counter climb more slowly.

JS The logarithmic LFU counter

Press Run (or Ctrl + Enter) to see the console output here.

No output.

With factor 0 the counter climbs by one per hit and is saturated at 255 by the time a key has 1,000 hits. With the default factor of 10 it is still only around 53 after 10,000 hits and reaches 255 at about a million, so it keeps telling apart keys with very different popularity.

Measuring memory

  • INFO memory shows used_memory (what the allocator handed to Redis), used_memory_rss (what the operating system reports for the process) and mem_fragmentation_ratio, which is RSS divided by used_memory.
  • MEMORY USAGE key returns the bytes one key takes, including internal overhead. For big collections it samples (SAMPLES 0 checks everything).
  • MEMORY STATS and MEMORY DOCTOR give a breakdown and plain-language hints; redis-cli --bigkeys and --memkeys scan for the largest keys.
  • Fragmentation: a ratio near 1 is healthy. A ratio well above 1 (for example 1.5 or more) means the allocator holds free pages that Redis cannot use, often after many deletes of mixed-size values. Below 1 it means parts of the dataset were swapped out, which is an emergency. Recent builds can defragment online (CONFIG SET activedefrag yes), or you can restart a replica and promote it.

Encodings: why small data is cheap

Redis stores small collections in compact, contiguous encodings and converts them to pointer-heavy structures when they grow past a limit. OBJECT ENCODING key tells you which one is in use. Typical defaults in recent versions (check CONFIG GET on your server):

  • Strings: int for integers, embstr for short strings (up to 44 bytes), raw otherwise.
  • Hashes and sorted sets: listpack up to 128 entries with every field and value at most 64 bytes, then hashtable or skiplist.
  • Sets: intset for up to 512 integers, listpack for small non-integer sets (Redis 7.2 and later), then hashtable.

A listpack is a packed array: no per-entry pointers, so it is far smaller, but lookups scan it, which is fine for 128 items. This is why many small hashes can use much less memory than one key per field, and why one huge value that crosses a limit can suddenly cost several times more.

JS Which encoding would Redis choose?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Real Redis converts automatically and never converts back when the collection shrinks again (apart from specific cases), so a big collection that was trimmed may keep its larger encoding.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the default maxmemory-policy?

    By default Redis evicts nothing: once the limit is reached, commands that need memory fail with an OOM error while reads continue to work.
  2. 2. You use volatile-lru, but none of your keys has a TTL. What happens when memory is full?

    volatile-* policies only consider keys that have an expiry. With none available, Redis has no candidates and refuses the write.
  3. 3. Why does Redis sample keys (maxmemory-samples) instead of keeping an exact LRU list?

    An exact list costs memory per key and time per read. Picking the oldest of a handful of random keys gives close to exact LRU at a fraction of the cost.
  4. 4. When is allkeys-lfu a better choice than allkeys-lru?

    LFU protects frequently used keys from being flushed by a burst of keys read only once, which LRU would treat as recently used.
  5. 5. A server shows mem_fragmentation_ratio of 0.8. What does that most likely mean?

    The ratio is RSS divided by used memory. Below 1 the operating system holds less in RAM than Redis thinks it uses, so pages were swapped out and latency will suffer.

Summary

  • maxmemory caps Redis memory (0 means no limit); maxmemory-policy decides what happens at the cap.
  • noeviction (the default) refuses writes; allkeys-* may evict any key; volatile-* only keys with a TTL.
  • LRU evicts the least recently used, LFU the least frequently used, volatile-ttl the nearest to expiry.
  • Redis approximates LRU by sampling (maxmemory-samples, default 5) and uses an 8-bit logarithmic counter with decay for LFU.
  • Watch INFO memory, MEMORY USAGE and the fragmentation ratio; leave headroom for buffers, forks and fragmentation.
  • Small collections use compact encodings (listpack, intset, embstr) and switch at thresholds you can inspect with OBJECT ENCODING.

Next, two everyday uses that depend on expiry and atomic counters: Sessions and Rate Limiting.