1. Gyanastra
  2. Computer Organisation and Architecture
  3. Cache Performance and Write Policies
  4. Cache Replacement Policies: Random, FIFO, LRU, LFU
Free lesson, no account needed 6 min read 9 practice questions after it
Asked in past papers8 times
  • GATE CSE2024201420052004
  • ISRO20162007
  • UGC NET20222018

Computer Organisation and Architecture · Cache Performance and Write Policies

Cache Replacement Policies: Random, FIFO, LRU, LFU

Overview

A replacement policy is the rule that decides which existing cache block is evicted when a new main-memory block must be brought into a full cache.
Such policies matter only for associative and set-associative caches, where a block has several possible homes, and their goal is to minimize the miss penalty for future references.

Key concepts

  • Replacement policy: the rule used to decide which existing cache block is sent out so that a new main-memory block can be brought into its place when the cache is already full.
  • Where it is needed: only for associative mapping and set-associative mapping, not for direct mapping.
    • Direct mapping needs no policy because, for a given main-memory block, there is exactly one possible cache block it can occupy, so there is no choice to make.
    • Associative / set-associative mapping needs a policy because a main-memory block can go into any one of several (or all) cache blocks, so a decision must be taken about which block to evict.
  • Aim of every replacement policy: minimize the miss penalty for future references.
  • Guiding idea: when evicting a block, choose one that will not be needed for the longest possible time. If an evicted block is requested again immediately, the eviction was wasted effort.
    This cannot always be achieved, but the policy tries to optimize it as far as possible.
  • Relation to OS: these are the same kind of algorithms studied as page replacement algorithms in Operating Systems; here they are called cache replacement policies because the context is caching.
  • The four policies:
    • Random: no specific criterion; any one of the resident blocks can be chosen and removed. It is not really used in operating systems.
    • FIFO (First In First Out): the block that entered the cache first is the candidate for replacement; whichever block came in first is replaced first.
    • LRU (Least Recently Used): the block that has not been used (referred) for the longest time is replaced.
    • LFU (Least Frequently Used): the number of times each block has been used (its reference count) is examined; the block with the fewest references is replaced.
Memory diagram: Cache Replacement Policies: Random, FIFO, LRU, LFU
Figure. Cache Replacement Policies: Random, FIFO, LRU, LFU

How it works

  1. Take a small cache that holds only 4 blocks, numbered 0, 1, 2, 3, and a main memory of 16 blocks.
  2. In direct mapping, main-memory blocks 0, 1, 2, 3, … map in a fixed way, so no decision is required.
  3. In associative / set-associative mapping, assume the cache is completely full — say blocks 1, 3, 7, 9 currently occupy the four cache blocks.
  4. A request arrives for a new block, say block number 4. A decision must be made: which of 1, 3, 7, 9 is sent out and replaced by 4.
  5. The eviction should be done as effectively as possible: if a block that will be used immediately next is evicted, it will have to be fetched back anyway, so the replacement gained nothing.
  6. Apply the chosen policy to pick the victim: Random picks any resident block; FIFO picks the earliest-entered block; LRU picks the longest-unreferenced block; LFU picks the block with the fewest references.

Comparison

PolicySelection criterionBasis of decision
RandomNo criterion; pick any resident blockNone (arbitrary)
FIFOBlock that entered the cache firstTime of entry (load order)
LRUBlock not referred for the longest timeRecency of last reference
LFUBlock with the fewest referencesFrequency (count) of references
MappingReplacement policy needed?Reason
Direct mappingNoOnly one possible cache block per main-memory block; no choice
Associative mappingYesBlock may go into any cache block
Set-associative mappingYesBlock may go into any block of its set

Revision summary

  • Replacement decisions arise only when the cache (or the target set) is completely full and a new block must be brought in.
  • Direct mapping: no replacement policy; associative and set-associative: replacement policy required.
  • Goal of every replacement policy: minimize miss penalty for future references by evicting a block unlikely to be needed soon.
  • Evicting a block that is requested again immediately wastes the replacement effort.
  • Cache replacement policies are analogous to OS page replacement algorithms.
  • Random has no criterion and is not really used in operating systems.
  • FIFO replaces the first-entered block; LRU replaces the longest-unreferenced block; LFU replaces the block with the fewest references.

Previous-year exam insights

  • LRU is a stack algorithm: the set of blocks resident in a cache of size m under LRU is always a subset of the set resident in a cache of size m+1 for the same reference string, so LRU suffers no Belady's anomaly (FIFO can).
    Asked inUGC NET 2022
  • For a reference string of length N containing n distinct block addresses, if the number of distinct addresses (mapping to the same set) between two consecutive references to the same address is at most k and associativity A ≥ k+1 (A > k), LRU never evicts a block before its reuse, giving exactly n compulsory misses, so the miss ratio is n/N.
    Asked inGATE 2014
  • In a set-associative cache, the set index is (block address) mod (number of sets); a block can be placed only in the blocks of that set, and the replacement policy chooses the victim within that set only.
    Asked inGATE 2005GATE 2004ISRO 2007
  • In a fully associative cache any block may occupy any cache line, so the replacement policy alone decides the line; the number of misses equals the number of distinct blocks requested when the cache is large enough to hold them all.
    Asked inGATE 2004
  • A dirty (modified) block must be written back to main memory when it is evicted under write-back, whereas write-through caches never need a write-back on eviction; a read miss may therefore cause a write-back only in a write-back cache.
    Asked inGATE 2024
  • LRU/FIFO/LFU page-replacement algorithms are the same algorithms as cache replacement policies; LFU is a page/cache replacement policy, stack algorithms are a class of page-replacement algorithms (e.g. LRU, OPT), the elevator algorithm belongs to disk scheduling, and Havender's algorithm belongs to deadlock prevention.
    Asked inUGC NET 2022UGC NET 2018ISRO 2016
Pages 1–2 of 2