Skip to content
StackDecode
System Design
7. Caching ArchitectureAnimated

Least Recently Used (LRU)

Evict the entry unused for the longest time.

The idea

Assumes recently used items will be used again. Keeps a recency order and removes the oldest.

Watch it work

Press play, pause, or step through. Some animations have settings you can change.

request streamABFBFADADADFAADAACBDAFcache (4 slots)Alast use 1MISS A is fetched from the databasehit rate 0% (0 of 1)
  • hit
  • current request

A is missing: fetch it from the database and store it.

step:
1
hits:
0
1/22

Eviction policy: least recently used.

Step by step

  1. 1On access move the item to the front
  2. 2When full evict from the back
  3. 3New items go to the front
  4. 4Hot items stay

Where it is used

General purpose caches.

Watch out for

A one-time scan of many keys can flush useful entries.

Related