Cache algorithm — eviction policies and practical trade-offs
Overview of cache algorithms: goals, common eviction policies (LRU, LFU, MRU, ARC, PLRU), practical considerations, history, use cases and how to choose an appropriate scheme.
A cache algorithm is the set of rules software or hardware uses to decide which stored items to keep and which to remove when a cache is full. Caches exist to speed up access to data: they hold copies of information expected to be reused so that requests can be served more quickly than fetching the original source. Two common performance metrics are the hit rate — the fraction of requests satisfied from the cache — and latency — the time it takes to retrieve a cached item. Cache algorithms seek to maximize hit rate and minimize latency and other costs, but different designs favor different workloads, hardware constraints and implementation complexity.
Image gallery
7 ImagesCore concepts and measurements
Understanding cache algorithms requires a few basic terms. A cache hit is when the requested item is present in the cache; a miss occurs when it is not. Misses may be compulsory (first-time requests), capacity-related (cache too small), or conflict-related (placement rules force eviction). Eviction policy is the rule used to select which entry to remove on a miss when space is needed. Metadata such as timestamps, counters or small bits may be stored for each entry to support a policy, but maintaining metadata has time and space overheads. Some caches also use admission policies (deciding whether a new item should enter the cache) and prefetching (proactively loading likely future items).
Common eviction policies
- Belady’s optimal (clairvoyant) algorithm: The theoretical optimum is to evict the item whose next use is farthest in the future. This policy yields the smallest possible miss rate for a given trace, but it requires perfect knowledge of future requests and so is not implementable in practice. It is useful as a lower bound for comparing real algorithms.
- Least Recently Used (LRU): Evict the item that has not been used for the longest time. LRU works well for workloads with strong temporal locality, and it is conceptually simple. Exact LRU can be expensive in hardware or large software caches because updating and tracking recency for many items costs time and memory. Variants and approximations exist to reduce overhead.
- Most Recently Used (MRU): Evict the most recently used item. MRU can be effective when recently referenced items are less likely to be reused soon — for example in some database access patterns — and it has the benefit of very simple implementation in specific contexts.
- Least Frequently Used (LFU): Evict items accessed least often over some time window. LFU captures long-term popularity but may keep stale items that were once very popular. Implementations often combine aging or decay mechanisms to avoid unfairly favoring old hot items.
- Pseudo-LRU (PLRU): An approximation of LRU that uses very little metadata (often a single bit per item or a small tree of bits). PLRU is commonly used in high-speed CPU caches where true LRU overhead would be too high. It favors implementation speed over perfect recency tracking.
- Set-associative and direct-mapped arrangements: In hardware caches the address of an item determines a restricted set of locations. A two-way set-associative cache allows two possible slots and evicts the less desirable of those two; direct-mapped caches map each address to exactly one slot and simply overwrite it. These are placement strategies combined with simple eviction rules that trade flexibility for access speed and simplicity.
- Adaptive and hybrid policies: Algorithms such as Adaptive Replacement Cache (ARC) and Multi-Queue (MQ) try to combine the strengths of recency-based and frequency-based approaches. ARC, for example, dynamically balances LRU and LFU behavior to adapt to changing workloads and often provides better practical performance than either approach alone.
Practical considerations and variants
Real-world cache design mixes eviction policy with other concerns. Items differ in size, fetch cost and validity lifetime; a good cache may prefer to keep large but cheap-to-fetch objects short-term while retaining small or expensive-to-generate objects longer. Size-aware eviction policies, cost-aware ranking and weighted replacement score entries by a composite metric instead of simple recency or frequency.
Time-to-live (TTL) or explicit expiration is common in networked caches: entries are valid only for a defined time and are discarded when stale regardless of recency. Admission policies decide whether a new item should enter the cache at all; for example, tiny or one-off objects may be excluded to avoid displacing reusable data. Maintenance overhead, concurrency support and predictability under adversarial patterns are also important: some policies can be forced into very poor performance by particular request sequences (a phenomenon known as thrashing).
Distributed caches and coherence
When multiple caches store copies of overlapping data — such as in content delivery networks, database clusters or multi-core CPUs — maintaining a consistent view of data adds complexity. Cache coherency mechanisms coordinate updates, invalidations and write propagation. Distributed caches must also consider network latency, replication, partitioning and consistency models when choosing eviction and replication strategies. Some systems combine local caches with a shared central store and use policies that respect both local access patterns and global cost metrics.
History, applications and examples
Cache research has deep roots in both operating systems and computer architecture. The page replacement problem studied in virtual memory systems produced many early algorithms, and Belady’s work established the optimal baseline. As hardware evolved, approximations like PLRU and set-associative layouts became standard in CPU caches to meet tight timing constraints. In software systems, LRU and variants dominate web, database and object-caching libraries, while LFU and hybrid schemes appear when long-term popularity matters. Modern content delivery networks, in-memory databases, web browsers, DNS resolvers and operating system buffers all use cache algorithms tuned to their specific workloads.
How to choose and tune a cache algorithm
- Measure the workload: collect traces or representative request patterns to learn about locality, frequency and object sizes.
- Define objectives: prioritize hit rate, tail latency, fairness, cost of misses or predictability according to application needs.
- Select a candidate policy: use simple LRU or approximations where recency dominates; choose LFU or hybrid ARC for long-tail popularity; use size- and cost-aware rules if objects vary widely.
- Implement instrumentation and test: compare against a baseline, and consider offline simulation with Belady’s optimal as a lower bound.
- Iterate and adapt: workloads change, so adaptive policies or runtime-configurable settings can maintain good performance over time.
For additional technical detail on eviction policies and formal analysis see external resources: eviction policy overview, algorithmic foundations and models of caching performance; latency and workload characterization, guidance on measuring and modeling cache latency and miss costs; and database and application caching, practical advice for tuning caches in server and application environments.
In summary, cache algorithms are fundamental to system performance and resource efficiency. There is no one-size-fits-all solution: the best policy depends on access patterns, hardware constraints and the relative cost of misses. Understanding the trade-offs, measuring real workloads and choosing either a well-known policy or an adaptive hybrid typically yields the best results.
Questions and answers
Q: What is a Cache Algorithm?
A: A Cache algorithm is an algorithm used to manage a cache or group of data. It decides which item should be deleted from the cache when it is full.
Q: What is hit rate?
A: Hit rate describes how often a request can be served from the cache.
Q: What does latency describe?
A: Latency describes for how long a cached item can be obtained.
Q: What is Belady's optimal algorithm?
A: Belady's optimal algorithm, also known as the clairvoyant algorithm, is an efficient caching algorithm that always discards the information that will not be needed for the longest time in the future. This result cannot generally be implemented in practice because it requires predicting what will be needed far into the future.
Q: How does Least Recently Used (LRU) work?
A: LRU deletes the least recently used items first and requires keeping track of what was used when by using "age bits" for each cache-line and tracking which one was least recently used based on age-bits. Every time a cache-line is used, all other cache-lines' ages are changed accordingly.
Q: How does Most Recently Used (MRU) work?
A: MRU deletes the most recently used items first and this caching mechanism is commonly used for database memory caches.
Q: What other algorithms exist to maintain cache coherency?
A; Various algorithms exist to maintain cache coherency when multiple independent caches are being used for shared data, such as multiple database servers updating a single shared data file.
Related articles
Author
AlegsaOnline.com Cache algorithm — eviction policies and practical trade-offs Leandro Alegsa
URL: https://en.alegsaonline.com/art/15882
Sources
- usenix.org : usenix.org/legacy/events/fast03/tech/full_papers/megiddo/megiddo.pdf
- usenix.org : usenix.org