Cache Design

LRU & LFU
Cache

A cache with O(1) get and O(1) put — including eviction. The secret: pair a hash map with a doubly-linked list so every operation is a constant-time pointer swap.

How do you implement an LRU cache?

An LRU (Least Recently Used) cache evicts the item that hasn't been used for the longest time when it's full. The classic O(1) design combines a hash map (key → node) with a doubly linked list ordered by recent use. In JavaScript, a Map already keeps insertion order, so you can delete and re-insert a key on each access and evict map.keys().next().value.

class LRUCache {
  constructor(capacity) { this.capacity = capacity; this.map = new Map(); }
  get(key) {
    if (!this.map.has(key)) return -1;
    const value = this.map.get(key);
    this.map.delete(key); this.map.set(key, value);   // mark as most recent
    return value;
  }
  put(key, value) {
    this.map.delete(key);
    this.map.set(key, value);
    if (this.map.size > this.capacity) this.map.delete(this.map.keys().next().value);   // evict oldest
  }
}

LRU cache — live demo

LRU cache (capacity=2)step 1/4

// Array state

1:A
MRU
→ Cache empty. put(1, 'A'): insert at head. [1:A]

// Pseudocode

1// LRU: evict the Least Recently Used item
2structure LRUCache(capacity):
3 map = HashMap() // key → node
4 dll = DoublyLinkedList // MRU at head, LRU at tail
5 
6function get(key):
7 if key not in map: return -1
8 node = map[key]
9 moveToFront(dll, node) // now most-recently-used
10 return node.value

How it works

1 / 4
1

LRU — the data structure

Combine a doubly-linked list (for O(1) move-to-front and remove-tail) with a hash map (for O(1) node lookup by key). The list maintains recency order: head = most recent, tail = least recent.

1 of 4

LRU pseudocode

lru-cache.pseudo
1// LRU: evict the Least Recently Used item
2structure LRUCache(capacity):
3 map = HashMap() // key → node
4 dll = DoublyLinkedList // MRU at head, LRU at tail
5 
6function get(key):
7 if key not in map: return -1
8 node = map[key]
9 moveToFront(dll, node) // now most-recently-used
10 return node.value
11 
12function put(key, value):
13 if key in map:
14 map[key].value = value
15 moveToFront(dll, map[key])
16 else:
17 if size == capacity:
18 evict = removeTail(dll) // remove LRU
19 delete map[evict.key]
20 node = new Node(key, value)
21 insertHead(dll, node)
22 map[key] = node

LFU pseudocode

lfu-cache.pseudo
1// LFU: evict the Least Frequently Used item
2structure LFUCache(capacity):
3 keyMap = HashMap() // key → (value, freq)
4 freqMap = HashMap() // freq → LinkedHashSet(keys)
5 minFreq = 1
6 
7function get(key):
8 if key not in keyMap: return -1
9 updateFreq(key) // increment frequency
10 return keyMap[key].value
11 
12function put(key, value):
13 if capacity == 0: return
14 if key in keyMap:
15 keyMap[key].value = value
16 updateFreq(key)
17 else:
18 if size == capacity:
19 evict key from freqMap[minFreq]
20 insert key with freq=1
21 minFreq = 1

Complexity — both O(1)

LRU get — hash map lookup + DLL moveO(1)

Constant time

LRU put — hash map insert + DLL insert + optional evictO(1)

Constant time

LFU get — hash map + freq updateO(1)

Constant time

LFU put — hash map + freq bucket updateO(1)

Constant time

Space — n = cache capacityO(n)

Linear time

💡When to use LRU vs LFU
Use LRU when access patterns are temporal — recently used items are likely needed again soon (web pages, database pages). Use LFU when access patterns are frequency-based — some items are always popular regardless of recency (CDN hot content, DNS records).

🎯 Quick Check

In an LRU cache of capacity 3 with items [A, B, C] (A=MRU, C=LRU), you call get(C). What is the new order?

LRU and LFU Caches: summary

Key points

  • LRU suits most caches; LFU (Least Frequently Used) suits workloads with stable "hot" items.
  • LFU needs frequency counts plus per-frequency lists to stay O(1).
  • Browsers, CDNs and databases all use LRU-like eviction.

Frequently asked questions

›What is the difference between LRU and LFU?

LRU evicts the item unused for the longest time. LFU evicts the item used the fewest times overall.