A cache has limited memory, so when it is full it must decide which entries to remove to make room for new ones. That rule is the eviction policy. The goal is to keep the entries most likely to be requested again, maximising the cache hit ratio. LRU — Least Recently Used - Evicts the entry that has not been accessed for the longest time. - Assumes recently used data will be used again soon (temporal locality). - Implementation: a hash map plus a doubly linked list gives O(1) get and put — on access, move the entry to the front; on eviction, remove from the back. In C# you can combine Dictionary<TKey, LinkedListNode<T>> with LinkedList<T>; in Java, LinkedHashMap with access order and removeEldestEntry. - Strengths: simple, effective for most workloads. - Weakness: a single large scan (for example a batch job reading many items once) can push out frequently used items. - Redis offers allkeys-lru and volatile-lru (an approximated LRU using sampling). LFU — Least Frequently Used - Evicts the entry with the fewest accesses. - Keeps popular items even if they were not accessed very recently. - Weaknesses: old items that were popular long ago can stay forever unless counts decay over time; new items start with low counts and may be evicted too quickly. - Practical implementations use decaying counters (Redis allkeys-lfu and volatile-lfu use a probabilistic logarithmic counter with decay). FIFO — First In, First Out - Evicts the oldest inserted entry, regardless of how often or recently it was used. - Very simple, low overhead, but ignores access patterns, so the hit ratio is often worse than LRU. Other policies - MRU (Most Recently Used): evicts the most recently used item; useful for some scan-heavy patterns. - Random replacement: evicts a random entry; surprisingly decent and cheap at scale. - TTL-based expiry: entries expire after a time to live regardless of memory pressure — used together with eviction to keep data fresh (Redis volatile-ttl evicts keys closest to expiry). - ARC (Adaptive Replacement Cache) and W-TinyLFU: adaptive policies that balance recency and frequency. Caffeine, the high-performance Java cache, uses W-TinyLFU and achieves near-optimal hit rates. - Size-aware policies: consider entry size, evicting large rarely used items first. Eviction vs expiration vs invalidation - Eviction: removing entries because the cache is full. - Expiration: removing entries because their TTL has passed. - Invalidation: removing entries because the underlying data changed (for example after an update in the database). Choosing a policy - General-purpose application caching: LRU (or W-TinyLFU where available). - Workloads with stable popular items (product catalogues, configuration, trending content): LFU. - Simple buffers and queues: FIFO. - Always combine with sensible TTLs and invalidation on writes. In .NET - IMemoryCache supports SizeLimit, priorities, absolute and sliding expiration; when compacting, it removes expired items, then lower-priority items, then least recently used items. - Redis (via IDistributedCache or HybridCache) uses its configured maxmemory-policy. Monitoring - Track hit ratio, evictions per second and memory usage. A high eviction rate with a low hit ratio means the cache is too small or the policy does not fit the access pattern. Interview summary: "LRU evicts the least recently used item, LFU the least frequently used, and FIFO the oldest inserted. LRU is the common default, LFU suits stable hot sets, and modern caches use adaptive policies like W-TinyLFU. Eviction works alongside TTL expiry and explicit invalidation."
Ravindra Wadile · · 0 likes