# Redis Cache: An Anatomy

How a Redis cache holds keys, hits and misses, and evicts data once maxmemory is reached, including approximated LRU, LFU counters and TTL expiry\.

By Numa

Topic: Computing, AI &amp; Electronics

[Open the interactive Jig](https://ask.jig.com/j/q0GOCxlf9pX3)

## Transcript

### 1. Redis Cache: An Anatomy

A Redis cache instance as a bench model: the keyspace tray, a client port, the origin database, the used\-memory gauge, the eviction pool and its bin\. Each step isolates one mechanism\. Click a key block to lift it; drag elsewhere to turn the model\.

Modelled on the open\-source server source \(unstable branch, read October 2026\) and redis\.io documentation\.

### 2. Anatomy of one key

Every value is reached through a 16\-byte redisObject header\. Its first 64 bits pack type:4, encoding:4, refcount:23, iskvobj:1, metabits:8 and a 24\-bit lru field; the last 64 bits are the pointer to the payload\. Strings of 44 bytes or less use EMBSTR and share one allocation with the header\.

src/object\.h\. The lru field holds a 24\-bit clock at 1000 ms resolution \(LRU\_CLOCK\_MAX = 2^24 \- 1\)\.

### 3. Cache\-aside read path

Redis does not read through to the origin\. On a hit the GET returns the value and keyspace\_hits increments; on a miss keyspace\_misses increments and the application loads from the database, then writes back with SET key value EX ttl\. Switch between the two paths\.

Hit ratio = keyspace\_hits / \(keyspace\_hits \+ keyspace\_misses\), from INFO stats\.

### 4. maxmemory

Before each command that adds data, Redis compares used memory with maxmemory\. This write fills the last free cell and the gauge needle reaches the limit stop\. The next write must evict first, according to maxmemory\-policy\.

maxmemory 0 means no limit on 64\-bit builds\. Replication and AOF buffers are excluded from the comparison \(mem\_not\_counted\_for\_evict\)\.

### 5. Approximated LRU eviction

Redis does not keep a global LRU list\. It samples maxmemory\-samples keys \(default 5\), compares their idle times, and merges them into a 16\-entry eviction pool sorted by idle time; the right\-hand entry is evicted\. Peg height is idle time\. Run a cycle, then click keys to touch them before the next one\.

EVPOOL\_SIZE 16 in src/evict\.c\. A sample of 10 approaches true LRU at higher CPU cost\. Ten policies exist; allkeys\-lru is the documented general default for skewed access\.

### 6. LFU in the same 24 bits

Under an LFU policy the lru field is reused: the high 16 bits store the last decrement time in minutes and the low 8 bits a logarithmic Morris counter\. A new key starts at 5, an access increments with probability 1/\(\(counter \- 5\) x lfu\-log\-factor \+ 1\), and the counter saturates at 255\. Slide the access count to see the counter height\.

redis\.io eviction table, lfu\-log\-factor 10: about 10 at 100 hits, 18 at 1K, 142 at 100K, 255 at 1M; intermediate heights are interpolated\. lfu\-decay\-time default 1 minute\.

### 7. Expiry is not eviction

Pinned keys carry a TTL stored as an absolute Unix time in milliseconds\. Expired keys are removed passively on access and actively: each cycle samples 20 keys with a TTL and repeats while more than 10% of the sample is stale\. Here three pinned keys reach their deadline and are deleted\.

src/expire\.c: slow cycle capped at 25% CPU, hz 10 by default\. Expiry and eviction both propagate DEL to replicas and the AOF; expired\_keys and evicted\_keys are separate counters\.

### 8. Reading the instance

INFO stats reports keyspace\_hits, keyspace\_misses, evicted\_keys and expired\_keys; INFO memory reports used\_memory\_dataset and mem\_not\_counted\_for\_evict\. OBJECT FREQ reads the LFU counter only when an LFU policy is set\. Leave RAM headroom above maxmemory for buffers outside the comparison\.

MEMORY USAGE key reports a single key; the eviction behaviour shown applies to Redis 8\.x defaults\.
