Designing a Thread-Safe LRU Cache
A low-level-design walkthrough of an O(1) LRU cache in Java — HashMap plus an intrusive doubly linked list — and how to make it correct under concurrency. Includes the full, browsable implementation.
An LRU (Least Recently Used) cache evicts the entry that hasn't been touched for the longest time when it hits capacity. The classic interview ask is: make every operation O(1). The follow-up that separates senior candidates: make it correct under concurrency. The O(1) structure Two data structures working together: A HashMap from key to node — O(1) lookup. An intrusive doubly linked list ordered by recency — O(1) move-to-front and O(1) eviction from the tail. On get, look up the node and move it to the head. On put, insert at the head; if over capacity, drop the tail. Because the map stores…
What’s inside
Full access unlocks the rest
You’ve reached the end of the free preview. Keep reading Designing a Thread-Safe LRU Cache — and every article, plus the full downloadable code — with one-time lifetime access. Or keep exploring the free articles.