Voiced by Amazon Polly |
Overview
Caching helps improve performance by storing frequently accessed data, and when data is needed, it is quickly retrieved. It is useful, especially when fetching data or performing time-consuming computations. But the question that comes to mind is, what will happen if the cache runs out of space? On what basis will we determine which data to keep and which must be cleared? The answer to this problem is a Least Recently Used (LRU) cache.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
LRU Cache
An LRU (Least Recently Used) Cache is a data structure created to handle a restricted quantity of data by discarding the least recently accessed elements when space is insufficient. The core idea is simple: when the cache is full and new data needs to be added, the system removes the item that hasn’t been used for the longest time.
Each time data is accessed, it gets tagged as “recently used” and is relocated to the front of the cache. Conversely, items that haven’t been accessed in a while are considered “least recently used” and will be the first to be evicted when the cache hits its capacity.
Why Use an LRU Cache?
Imagine you are building a web app that frequently queries a database to retrieve user data. If users keep asking for the same data repeatedly, it would be inefficient to query the database every time. Instead, you could store this data in a cache, so the app doesn’t have to repeatedly make the same expensive database calls.
But what if the cache starts to get full? You can’t store everything forever. This is where an LRU cache becomes handy. When the cache reaches capacity, it will remove the least recently accessed data to make room for new information, keeping the most frequently used data easily accessible.
Key Operations in an LRU Cache
There are two essential operations that an LRU cache supports:
- Get(key): This retrieves the value associated with the given key. If the key is found, the cache updates the access order to mark it as recently used. If not, it returns a special value like
None
or1
. - Put(key, value): This adds a new key-value pair to the cache or updates an existing key. When the cache reaches capacity, the item not used for the longest time is deleted to allow for the new data.
How Does an LRU Cache Work?
An LRU cache relies on two key data structures to function efficiently:
- Doubly Linked List
The doubly linked list helps maintain the order of data access. Every element in the list contains two pointers: one that directs to the previous element and another to the next. This structure allows the cache to quickly move items around (i.e., mark them as recently used or evict them) without shifting everything.
The front of the list consistently contains the latest used item. The tail contains the item that was used the least recently. When an item is retrieved or modified, it is shifted to the front (head) to indicate it has been recently used. When the cache reaches its capacity, the item at the end is deleted, as it was accessed least recently.
- Hash Map (Dictionary)
The hash map (or dictionary) maps the cache keys to the corresponding nodes in the doubly linked list. This provides O(1) time complexity for both lookups and updates, making the cache efficient.
By combining these two data structures, the LRU cache can efficiently manage memory while providing fast access to data.
Time Complexity of LRU Cache Operations
One of the key advantages of an LRU cache is that both the get
and put
operations are O (1), meaning they take constant time no matter how large the cache is. This is important because it ensures that accessing or updating data is fast, even as the cache grows.
- Get(key): Accessing the value associated with a key happens constantly. After accessing, the item is moved to the front of the list, which also takes O(1) time.
- Put (key, value): Inserting or updating an item also takes O(1) time. If the cache is full, evicting the least recently used item happens in O(1) time.
Common Use Cases for an LRU Cache
LRU caches are widely used in scenarios with limited memory, but fast data retrieval is critical. Here are some examples of where they come in handy:
- Web Browsers: Caches recently accessed web pages, images, and other resources. The most frequently visited pages are kept in memory for quick access, while older ones are evicted when necessary.
- Databases: Caches frequently queried data or database blocks. This reduces the number of disk reads and speeds up queries.
- Operating Systems: Used for managing memory, especially with virtual memory. The OS keeps frequently accessed memory pages in RAM, while less recently used pages are swapped out to disk.
- Content Delivery Networks (CDNs): Caches popular content (like videos or API responses) to ensure that users can quickly access it, even if it’s geographically far from the original server.
- Machine Learning: Stores intermediate results, model parameters, or training data. This helps speed up processes like model training or inference, especially with large datasets or real-time applications.
Benefits of LRU Caching
- Memory Efficiency: An LRU cache ensures that only the most relevant data stays in memory, helping to avoid unnecessary memory usage.
- Fast Data Retrieval: With O(1) time complexity for both
get
andput
, LRU caches allow quick access to stored data, making them ideal for high performance applications. - Easy to Implement: Implementing an LRU cache is relatively simple, especially with Python’s built-in dictionaries and the concept of doubly linked lists.
Conclusion
Drop a query if you have any questions regarding LRU Cache and we will get back to you quickly.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront and many more.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.
FAQs
1. What happens if the cache size is set too small in an LRU cache?
ANS: – If the cache size is too small, useful data may be evicted too frequently, leading to cache thrashing. This means the cache will constantly evict and reload items, which reduces its effectiveness and increases latency. Setting the cache size according to the application’s workload is important to minimize evictions.
2. Can an LRU cache be used in multithreaded applications?
ANS: – Yes, you can use an LRU cache in multithreaded applications. However, you need to ensure thread safety to prevent issues like race conditions. This can be done using locks or other threadsafe data structures. Proper synchronization is crucial for ensuring data consistency in concurrent environments.
WRITTEN BY Hridya Hari
Hridya Hari works as a Research Associate - Data and AIoT at CloudThat. She is a data science aspirant who is also passionate about cloud technologies. Her expertise also includes Exploratory Data Analysis.
Click to Comment