Design an LRU cache
medium 85 minDesign an in-memory Least Recently Used cache with O(1) lookups, updates and evictions.
Watch lesson4:49
Check yourself
One question at a time. Each answer explains why.
Question 1 of 14
What is the time complexity of get in this design?
What is the main benefit of a doubly linked list over a singly linked one here?
Why is the key stored in the node?
Capacity 2. put(1), put(2), get(1), put(3). Which key is gone?
Capacity 2, holding a and b, with a at the back. put(a, 9) comes. What happens?
Which structures give O(1) for get and put?
get runs under a read lock, many readers at once. What breaks?
In Java a thread holds the read lock of a ReentrantReadWriteLock and calls writeLock.lock(). What happens?
A production cache has high contention on get, spread over many keys. What helps most?
A layer checks the deadline with get and not with peek. What goes wrong?
Which access pattern makes plain LRU miss every time?
`if map.size() > capacity: evict()` runs before the insert. What is the largest size?
In push to the front, head.next = node runs before head.next.prev = node. Result?
Why should a loading cache not load under its lock?
Pick an answer to see why.
Not quite. One hash lookup and a fixed number of pointer writes. The list is never searched.
Not quite. Unlinking needs the predecessor. `prev` gives it in one step, otherwise you walk from the head.
Not quite. Eviction starts at the end of the list. The node must say which map entry leads to it.
Not quite. get(1) moved 1 to the front, so 2 was the least recent when 3 arrived.
Not quite. The key is known, so nothing is added and nobody has to go.
Not quite. One finds, the other orders and removes. Both in constant time.
Not quite. A hit unlinks and links a node. It is a write, and shared readers do it at the same time.
Not quite. The write lock waits until all readers are gone, and this thread is one of them. Upgrading is not supported.
Not quite. Threads that use different shards do not meet. If one key were hot, shards would not help.
Not quite. Looking would count as use. The order should change only for a living entry.
Not quite. Each key is evicted exactly one step before it is needed again.
Not quite. At size equal to capacity nothing is evicted, and one more is added. Use `>=` or `==` before an insert.
Not quite. After the first line head.next is the node, so the second line sets the node's own prev to itself.
Not quite. Note the load under the lock, do it outside, and let callers of that key wait for the one result.
Flashcards
Say your answer, then reveal it.
Card 1 of 15
Why a map and a list?
The map finds a key in O(1), the list knows the order and gives the oldest in O(1). Neither can do both.
Say your answer, then reveal it.
Why doubly linked?
A hit removes a node from the middle. Its left neighbour must be changed, and `prev` leads there without a walk.
Say your answer, then reveal it.
Why must list nodes store their own key?
Eviction finds the victim at `tail.prev`. To delete the map entry in O(1), the node must know which entry that is.
Say your answer, then reveal it.
What do the two sentinels do?
Every real node always has two neighbours. No branches for empty, first, last or only.
Say your answer, then reveal it.
The order of writes in push to the front?
node.prev, node.next, then head.next.prev = node, and last head.next = node.
Say your answer, then reveal it.
The three cases of put, in order?
Known: update and touch. Full: evict. Then insert at the front.
Say your answer, then reveal it.
Why does a read and write lock fail here?
A hit moves its node. Shared readers would write the same pointers at the same time.
Say your answer, then reveal it.
What is the ghost of a concurrent map?
A node found without a lock, evicted meanwhile, then moved to the front. Check under the lock that the map still leads to it.
Say your answer, then reveal it.
What do shards cost?
The global order, an exact size, and early evictions in a crowded shard. A single hot key is not helped.
Say your answer, then reveal it.
Which notes may a buffered cache lose?
Read notes. Never write notes: an entry the policy does not know can never be evicted.
Say your answer, then reveal it.
Eviction or expiry?
Eviction: no room. Expiry: too old. Different reasons, different classes.
Say your answer, then reveal it.
When does LRU lose?
On scans of one-time keys, and on loops longer than the capacity.
Say your answer, then reveal it.
How does Redis approximate LRU?
A stamp for each key. At eviction a small random sample, five by default, and the one idle longest goes.
Say your answer, then reveal it.
Single flight in one sentence?
The first caller of a missing key loads it without the lock, all others wait for that one result.
Say your answer, then reveal it.
How do you test an LRU cache?
Random calls on it and on a slow list, few keys, fixed seeds, comparison after every step.
Say your answer, then reveal it.

These cards come back in Today, just before you would forget them.