TTL LRU LFU and Cache Memory Limits

Caching and Failure Recovery · 40 min

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 →
Expiration bounds entry lifetime; eviction chooses what to remove when memory is needed, and neither alone guarantees fresh data.

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.

TTLA configured remaining lifetime after which an entry expires.
LRULeast recently used: evict the entry with the oldest recent access under the policy.
LFULeast frequently used: prefer eviction based on observed access frequency under the implementation.

02 Worked example

Follow one worked example

TTL LRU LFU and Cache Memory Limits worked diagram01A, BTwo initial misses02Read AOrder: B, A03Read CEvict B → A, C04Read A, B2 hits / 4 missesTTL LRU LFU and Cache Memory Limits compact diagram01A, BTwo initial misses02Read AOrder: B, A03Read CEvict B → A, C04Read A, B2 hits / 4 misses
A, B: Two initial misses • Read A: Order: B, A • Read C: Evict B → A, C • Read A, B: 2 hits / 4 misses.

Trace the example in order. Follow the same identities, units and state transitions through the diagram and the events below.

1
A, then BBoth miss, so both are loaded from the database. Ordered least-recent to most-recent, the cache is [A,B].
2
Read AA hits and becomes most recent. The order is [B,A].
3
Read C, then AC misses and evicts B, giving [A,C]. A then hits and the order becomes [C,A].
4
Read BB misses and evicts C. The final order is [A,B]. There were 2 hits and 4 misses out of 6 reads, a 33.3% hit rate.

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 conceptPurposeBoundary or cost
ExpirationLifetime endedCan occur with free memory
LRU evictionOldest recent useA hot stale value can stay
LFU evictionFrequency signalPolicy and decay details matter
Memory limitCapacity boundaryMiss 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 conceptRememberCheck
ExpirationLifetime endedCan occur with free memory
LRU evictionOldest recent useA hot stale value can stay
LFU evictionFrequency signalPolicy and decay details matter
Memory limitCapacity boundaryMiss pressure affects the origin
TTLA configured remaining lifetime after which an entry expires.
LRULeast recently used: evict the entry with the oldest recent access under the policy.
LFULeast frequently used: prefer eviction based on observed access frequency under the implementation.

06 Where & why

Check the primary references

Use official references for protocol and implementation details. The questions here are teaching prompts.

Primary reference
Redis eviction policies

Read Redis eviction policies

Primary reference
Redis SET

Read Redis SET

Primary reference
Redis TTL

Read Redis TTL

Primary reference
HTTP stale-while-revalidate

Read HTTP stale-while-revalidate

State the implementation version and the workload assumptions when defending your own design.

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?
A configured remaining lifetime after which an entry expires.
What does LRU mean?
Least recently used: evict the entry with the oldest recent access under the policy.
What does LFU mean?
Least frequently used: prefer eviction based on observed access frequency under the implementation.
Does LRU guarantee freshness?
No. It tracks use rather than source updates. Frequently read old data can be recent and stale at the same time.
Can an unexpired key be removed?
Yes, an applicable capacity eviction policy can remove it to make room. The TTL is not a reservation of memory.
Does TTL report the source’s last modification time?
No. It reports the key’s remaining expiry lifetime. Source freshness is a separate property.
Are Redis LRU and the console identical algorithms?
The console uses exact LRU for a tiny trace. Redis policies can use approximate sampling and implementation-specific behavior.
Why inspect memory and origin together?
Eviction raises misses, which can increase database work. A cache-only metric can hide the downstream cost of a memory decision.

08 Practice problems

Practice a new case

Solve the new cases before revealing hints. Complete worked solutions follow the set.

Hit rate

Easy
Compute the hit rate for 18 hits and 6 misses.
Follow-up
Use total lookups as the denominator.
Show the hint
Hits plus misses equals the total.

Name the removal

Easy
Classify removing an entry after 30 seconds and removing an unexpired entry to free memory.
Follow-up
Both produce a later miss but have different causes.
Show the hint
Age and capacity are separate triggers.

LRU trace

Medium
Trace A,B,C,A,D with capacity 3 and list the evicted key and final least-to-most-recent order.
Follow-up
A hit changes recency.
Show the hint
Move A to the recent end before inserting D.

LRU capacity

Medium
For A,B,C,A with capacity two, count hits and show the final order.
Follow-up
State the assumption or edge case that makes the answer correct.
Show the hint
A was evicted before its last request.

TTL interpretation

Medium
Interpret TTL values 12, -1 and -2.
Follow-up
State the assumption or edge case that makes the answer correct.
Show the hint
Distinguish remaining life from no expiry and absence.

Eviction review

Hard
A cache’s eviction count rises after new metadata fields are added, while external QPS is unchanged; explain a test plan.
Follow-up
State the assumption or edge case that makes the answer correct.
Show the hint
Entry size can change the effective working-set capacity.

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.