What is lru cache
Last updated: April 1, 2026
Key Facts
- LRU stands for Least Recently Used, a cache eviction policy that removes the least frequently accessed items when cache capacity is exceeded
- Typically implemented using a combination of a hash map (for O(1) lookups) and doubly linked list (for O(1) insertion/deletion)
- Provides O(1) time complexity for both get and put operations, making it highly efficient
- Widely used in CPU caches, web browsers, databases, CDNs, and operating systems for memory management
- Tracks item access time, ensuring the most recently used items remain in cache while older items are discarded
What is an LRU Cache?
An LRU (Least Recently Used) cache is a data structure that stores a limited collection of frequently accessed data with a constraint: when the cache reaches maximum capacity, it automatically removes the item that was accessed least recently. This approach optimizes both memory usage and data retrieval speed by keeping the most relevant data readily available.
How LRU Cache Works
The LRU cache operates on a simple principle: the most recently used items remain in the cache, while the least recently used items are evicted when space is needed. Every time an item is accessed (either read or written), its position is updated to mark it as recently used. When a new item needs to be added and the cache is full, the item that was accessed longest ago is removed to make room.
Data Structure Implementation
An efficient LRU cache implementation combines two complementary data structures:
- Hash Map: Enables O(1) constant-time lookups to find cached items by key
- Doubly Linked List: Maintains the order of item access, allowing O(1) insertion and deletion operations
- The hash map stores references to linked list nodes for quick access
- Moving a node to the front marks it as recently used; removing from the back evicts the least recently used item
Time and Space Complexity
LRU caches provide optimal performance characteristics. Get operations take O(1) time by using the hash map to locate the item. Put operations also take O(1) time, updating the linked list and hash map. Space complexity is O(capacity), where capacity is the maximum number of items the cache can store.
Real-World Applications
LRU caching is fundamental to modern computing systems:
- CPU Caches: Processors use LRU policies to manage L1, L2, and L3 caches
- Web Browsers: Store recently visited pages and assets for faster loading
- Database Systems: Cache frequently accessed data and query results
- Content Delivery Networks: Maintain frequently requested content at edge servers
- Operating Systems: Manage page replacement in virtual memory systems
Advantages and Limitations
LRU caches offer significant advantages including simple implementation, predictable behavior, and optimal performance for workloads with temporal locality. However, they assume that recently used data will likely be needed again soon, which may not hold true for all applications. Alternative policies like LFU (Least Frequently Used) or ARC (Adaptive Replacement Cache) may perform better for certain access patterns.
Related Questions
What's the difference between LRU and LFU cache?
LRU (Least Recently Used) evicts items based on when they were last accessed, prioritizing recency. LFU (Least Frequently Used) evicts items based on access frequency, prioritizing popularity. LFU can be more efficient for certain workloads but is harder to implement efficiently.
How is LRU cache different from a regular cache?
A regular cache may not have a specific eviction policy or could use random removal. LRU provides a systematic, predictable approach by always removing the least recently accessed item, making it more effective for applications with temporal locality patterns.
Can you implement an LRU cache in Python?
Yes, Python provides OrderedDict or you can implement it using a dictionary with a doubly linked list. The collections.OrderedDict from Python 3.7+ maintains insertion order, and the functools.lru_cache decorator provides a built-in LRU cache for function results.
More What Is in Daily Life
Also in Daily Life
More "What Is" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- Wikipedia - Cache Replacement PoliciesCC-BY-SA-4.0
- Wikipedia - Locality of ReferenceCC-BY-SA-4.0