Prompt
How do I choose cache eviction policies for app data?
Latest observation
Choosing a cache eviction policy depends on what kind of data you cache, how it’s accessed, and what “misses” cost you. There isn’t one best policy for everything, so the goal is to match the policy to your workload.
1) Start with the cache’s purpose
Ask:
- Is the data read-heavy or write-heavy?
- Are requests repeated often, or mostly one-time?
- Is access pattern predictable?
- Is stale data acceptable, or must it be very fresh?
- Is memory tight, or can you afford some waste?
These answers determine whether you should optimize for:
- recency
- frequency
- time-to-live freshness
- size/space efficiency
2) Common eviction policies and when to use them
LRU (Least Recently Used)
Evicts the item that hasn’t been used for the longest time.
Best for:
- general-purpose caches
- workloads with temporal locality
- UI/app data that users revisit often
Pros:
- simple and effective in many cases
- good default choice
Cons:
- can perform poorly for scans or one-off bursts
- doesn’t consider access frequency
Use if: recently used data is likely to be used again soon.
LFU (Least Frequently Used)
Evicts the item with the fewest accesses.
Best for:
- hotspots where some items are repeatedly accessed a lot
- long-lived caches with stable popularity patterns
Pros:
- retains truly popular items
Cons:
- can keep outdated “old favorites”
- more complex to implement
- needs aging/decay to avoid stale popularity
Use if: a small set of keys is accessed very often over time.
TTL / Expiration-based
Items expire after a fixed time.
Best for:
- app data that must stay fresh
- API responses
- config/state that changes regularly
Pros:
- easy to reason about freshness
- good for correctness
Cons:
- not adaptive to actual usage
- may evict valuable items too early or too late
Use if: freshness matters more than perfect hit rate.
FIFO / First In, First Out
Evicts oldest inserted items.
Best for:
- very simple systems
- some streaming/batch workloads
Pros:
- easy to implement
Cons:
- usually worse than LRU/LFU
- ignores access behavior
Use if: simplicity matters more than performance.
Random eviction
Evicts a random item.
Best for:
- very high-speed, low-overhead systems
- approximate caches where simplicity matters
Pros:
- very cheap to implement
- can be surprisingly decent under some workloads
Cons:
- unpredictable
- often worse than smarter policies
Use if: you need low overhead and can tolerate variance.
ARC / Adaptive policies
Adaptive Replacement Cache and similar policies balance recency and frequency.
Best for:
- mixed workloads
- unpredictable access patterns
Pros:
- often better than pure LRU or LFU
Cons:
- more complex
- not always available in standard libraries
Use if: workload shifts between recency-heavy and frequency-heavy behavior.
3) Match policy to workload
If your app data is user-session-like or navigation-like:
Use LRU or TTL + LRU.
Example:
- recent profile views
- recent search results
- recent API responses
If your cache stores “top” items accessed repeatedly:
Use LFU or LFU with aging.
Example:
- popular products
- frequently requested metadata
- repeated lookup tables
If freshness is critical:
Use TTL or stale-while-revalidate patterns.
Example:
- auth tokens
- pricing
- inventory
- feature flags
If access pattern is unpredictable:
Use adaptive policies like ARC, or default to LRU.
4) Consider these practical factors
Cache entry cost
If some items are much larger than others, evicting by count alone can be misleading.
You may want policies based on:
- bytes used
- cost to recompute
- importance/priority
Sometimes a small number of big objects should be evicted before many small ones.
Eviction cost
If recomputing an item is expensive, keep it longer.
Good candidates to retain:
- expensive DB queries
- heavy transformations
- pre-rendered content
Freshness vs hit rate
A cache that serves stale data is often worse than a smaller cache with correct data.
If data changes often:
- use short TTLs
- invalidate on write
- combine TTL with recency/frequency policies
Scan resistance
Some workloads read a large dataset once, which can destroy LRU effectiveness.
If this happens:
- use segmented LRU
- use adaptive policies
- separate “hot” and “cold” caches
5) A good default strategy
If you’re unsure, start with:
LRU + TTL
- LRU handles recency
- TTL ensures freshness
Then measure:
- hit rate
- stale hit rate
- eviction frequency
- latency impact
If you see:
- many repeated popular accesses → consider LFU
- lots of one-time scans → consider adaptive/segmented policies
- freshness problems → shorten TTL or add invalidation
6) A simple decision guide
Choose:
- LRU if data is reused soon after access.
- LFU if a small set of items is repeatedly popular.
- TTL if freshness is the main concern.
- LRU + TTL if you want a solid general-purpose cache.
- ARC/adaptive if workloads vary a lot.
- Random/FIFO only if simplicity or speed matters more than optimality.
7) Measure before and after
The best cache policy is workload-dependent, so benchmark with real traffic if possible.
Track:
- cache hit rate
- average latency
- stale data rate
- memory usage
- CPU overhead of eviction logic
A policy with a slightly lower hit rate may still be better if it’s much cheaper to run.
If you want, I can also give you:
- a decision tree,
- a policy comparison table, or
- recommendations for a specific cache type like in-memory app cache, Redis, browser cache, or mobile offline data.