System Design · Module 6 · Lesson 2 of 4 · 40 min
TTL LRU LFU and Cache Memory Limits
Trace exact LRU by hand, distinguish age-based expiration from capacity eviction, and practice a namespaced Redis entry lifecycle.
See hits, misses and eviction →01 The idea
The problem this lesson solves
Both miss, so both are loaded from the database. Ordered least-recent to most-recent, the cache is [A,B].
Prerequisite: earlier lessons introduce the vocabulary and shared examples used here. Follow the module order.
02 Worked example
Follow one worked example
Trace the example in order. Follow the same identities, units and state transitions through the diagram and the events below.
Expiration bounds entry lifetime; eviction chooses what to remove when memory is needed, and neither alone guarantees fresh data.
03 The method
Mechanisms decisions and a guided tutorial
Work through the explanation in order, then apply it in the tutorial. Each comparison names a cost as well as a benefit.
| Choice or concept | Purpose | Boundary or cost |
|---|---|---|
| Expiration | Lifetime ended | Can occur with free memory |
| LRU eviction | Oldest recent use | A hot stale value can stay |
| LFU eviction | Frequency signal | Policy and decay details matter |
| Memory limit | Capacity boundary | Miss pressure affects the origin |
Separate eviction from expiry
Eviction removes entries because capacity is needed; expiration removes entries because their lifetime ended. LRU evicts the least recently used entry, LFU considers frequency, and FIFO follows insertion order. Redis offers approximate policies with implementation-specific behavior; our console is an exact small LRU teaching model. A TTL does not mean the cache is always fresh until it expires. An upstream write can make the value stale one moment after insertion.
Use a tiny exact model
Our sequence is A,B,A,C,A,B with capacity two. Keep an order from least to most recent. Reading an existing key removes it from its old position and places it at the recent end. Loading a missing key into a full cache first removes the least recent entry.
A and B miss, A hits, C misses and evicts B, A hits, and B misses and evicts C. There are two hits and four misses. With capacity three, no eviction is needed for these three distinct keys, so the final B is also a hit. This is an exact educational model; Redis policy implementations may approximate recency or frequency.
Age and capacity answer different questions
A value with a sixty-second TTL can be evicted after one second if the memory policy needs room. A value can also expire when there is plenty of memory. LRU does not make a rapidly changing database value fresh; frequently accessed stale data can remain highly recent.
Choose TTL from the permitted staleness and recovery behavior, then choose an eviction policy for the workload and memory limit. Some Redis policies consider all keys, while others target keys with expiration. Inspect the selected policy rather than assume the name “cache” guarantees one universal removal rule.
Observe the Redis contract
SET key value EX 60 stores an expiring value. TTL reports remaining seconds, not time since the source was last updated. In Redis, -2 means the key is missing and -1 means an existing key has no expiry. A plain overwrite can change expiry behavior unless the chosen command options preserve it.
Use a lab namespace and avoid changing a shared instance’s memory configuration as part of learning commands. Production sizing should measure entry cost and observe hit rate, eviction, expiration and origin demand together. A rising hit rate can coexist with wrong stale responses.
Guided tutorial
1. Trace capacity two
Write the least-to-most-recent order after every request.
2. Compare capacity three
Predict the final lookup before changing the preset.
3. Run the Redis lifecycle
Use the namespaced commands below on an isolated instance.
4. Interpret missing
Explain how an app reacts to nil and why Redis does not fetch the database automatically.
redis-cli SET sd:lab:link:a7 https://example.com EX 60
redis-cli GET sd:lab:link:a7
redis-cli TTL sd:lab:link:a7
redis-cli DEL sd:lab:link:a7
redis-cli GET sd:lab:link:a7
# Expected final result: nil (cache miss)
# TTL: -2 means missing; -1 means an existing key has no expiry.05 Cheat sheet
Review the decisions
Use this table to explain the mechanism and its limit without memorizing a product name.
| Choice or concept | Remember | Check |
|---|---|---|
| Expiration | Lifetime ended | Can occur with free memory |
| LRU eviction | Oldest recent use | A hot stale value can stay |
| LFU eviction | Frequency signal | Policy and decay details matter |
| Memory limit | Capacity boundary | Miss pressure affects the origin |
06 Where & why
Check the primary references
Use official references for protocol and implementation details. The questions here are teaching prompts.
07 Interview questions
Explain the reasoning out loud
Answer before opening the explanation. The questions progress from vocabulary to decisions and failure behavior.
What does TTL mean?
What does LRU mean?
What does LFU mean?
Does LRU guarantee freshness?
Can an unexpired key be removed?
Does TTL report the source’s last modification time?
Are Redis LRU and the console identical algorithms?
Why inspect memory and origin together?
08 Practice problems
Practice a new case
Solve the new cases before revealing hints. Complete worked solutions follow the set.
Hit rate
EasyShow the hint
Name the removal
EasyShow the hint
LRU trace
MediumShow the hint
LRU capacity
MediumShow the hint
TTL interpretation
MediumShow the hint
Eviction review
HardShow the hint
Worked solutions
1. Hit rate
18 / (18 + 6) = 0.75, or 75 percent.
2. Name the removal
The first is expiration. The second is eviction. Neither alone tells you whether the backing value changed.
3. LRU trace
After A,B,C the order is [A,B,C]. A hits, producing [B,C,A]. D misses and evicts B. Final order: [C,A,D].
4. LRU capacity
All four requests miss. After C the order is [B,C]; the last A evicts B, leaving [C,A].
5. TTL interpretation
12 is about twelve seconds remaining; -1 means an existing key has no expiry; -2 means the key does not exist.
6. Eviction review
Measure representative entry memory, resident key count, hit/miss rate and origin load. Compare the old and new value shapes under the same workload and test memory/policy changes against the freshness and recovery contract.