Eviction Policies: LRU, LFU, FIFO & TTL
Cache memory is finite. When the cache is full and a new entry arrives, something must be removed — that decision is the eviction policy. The right policy depends on your access patterns. LRU (Least Recently Used) evicts the entry that has not been accessed for the longest time. It works well for most workloads because recently used data tends to be used again — temporal locality. LFU (Least Frequently Used) evicts the entry with the fewest accesses over its lifetime, better when some items are consistently popular. FIFO evicts the oldest inserted entry regardless of usage — simple but often suboptimal. Random eviction picks any entry arbitrarily; surprisingly effective and zero bookkeeping overhead. TTL (time-to-live) expiry removes entries after a fixed duration regardless of usage — essential for data that becomes stale on a schedule.